AtCoder DP Contest - Independent Set

Author: Andrew Wang

Time Complexity:

Root the tree at node , allowing us to define the subtree of each node. Let represent the number of ways the subtree can be painted such that is painted white. Similarily, let represent the number of ways the subtree can be painted such that is painted black.

Painted White

The number of ways to paint a subtree such that the root node is painted white is the product of the ways to paint the child subtrees. The number of ways to paint a child subtree is the sum of how to paint it white and how to paint it black or, . So the transition is:

Painted Black

Since no two adjacent nodes can both be painted black, if the root node of the subtree is painted black, none of its children can be painted black. This leads us to the conclusion that the number of ways to paint a subtree such that the root node is painted black is the product of all the ways the child subtrees can be painted white.

1#include <iostream>
2#include <vector>
3using namespace std;
4
5int MOD = 1000000007;
6long dp[2][100000];
7vector<int> adj[100000];
8void dfs(int i, int p){
9 dp[0][i] = 1;
10 dp[1][i] = 1;

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!

Give Us Feedback on AtCoder DP Contest - Independent Set!