Given a tree with N nodes, find the number of ways to divide the nodes into two sets, such that every node has at least one node that is connected to it and in the same set with it.
Solution
This problem can be solved with DP on trees.
Let f[i,0] be the number of ways to divide the subtree with node i as root, into two sets, when i is in different set with its parent. Similarly, let f[i,1] be the number of ways to divide the subtree when i is in the same set with its parent.
If node i has no child, then f[i,0]=0, f[i,1]=1. If node i have M children, c[1],c[2],…,c[M], f[i,1] = sum { f[c[1],k[1]] × f[c[2],k[2]] × … × f[c[M],k[M]]}, k[j] = 0,1. f[i,0] = f[i,1] - f[c[1],0] × f[c[2],0] × … × f[c[M],0].
So we only need to choose a node whose degree is 1 as the root, and use DFS to calculate f. The answer is 2 × f[root,0], due to symmetry.