Translation of the meaning of problems
Meaning of the questions: Given a N-node tree, three colors, where K nodes have stained requirements of any two adjacent nodes different color, find a few legitimate programs staining. Previous Topic: Il_ItzABC_lI
Title Description
Farmer John has a large farm with NN barns (1 \le N \le 10^51≤N≤105), some of which are already painted and some not yet painted. Farmer John wants to paint these remaining barns so that all the barns are painted, but he only has three paint colors available. Moreover, his prize cow Bessie becomes confused if two barns that are directly reachable from one another are the same color, so he wants to make sure this situation does not happen.
It is guaranteed that the connections between the NN barns do not form any 'cycles'. That is, between any two barns, there is at most one sequence of connections that will lead from one to the other.
How many ways can Farmer John paint the remaining yet-uncolored barns?
Input Format
The first line contains two integers NN and KK (0 \le K \le N0≤K≤N), respectively the number of barns on the farm and the number of barns that have already been painted.
The next N-1N−1 lines each contain two integers xx and yy (1 \le x, y \le N, x \neq y1≤x,y≤N,x≠y) describing a path directly connecting barns xx and yy.
The next KK lines each contain two integers bb and cc (1 \le b \le N1≤b≤N, 1 \le c \le 31≤c≤3) indicating that barn bbis painted with color cc.
Output Format
Compute the number of valid ways to paint the remaining barns, modulo 10^9 + 7109+7, such that no two barns which are directly connected are the same color.
Sample input and output
4 1 1 2 1 3 1 4 4 3
8
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #include<queue> #define p 1000000007 #define LL long long using namespace std; inline int read() { int sum=0; char ch =getchar(); while(ch<'0'||ch>'9') ch=getchar(); while(ch>='0'&&ch<='9') { sum=sum*10+ch-'0'; ch=getchar(); } return sum; } int n,m,tot=0; int Head[100005],col[100005];//Head用于邻接表 col记录颜色 LL dp[100005][4]; bool visit [100005]; // Since the memory points have been gone through FIG undirected graph, so to prevent infinite loop recording Tree struct { int Next, Node; } H [200010]; inline void the Add (int U, V int) // adjacency table stored FIG { H [++ TOT] Head .next = [U]; H [TOT] .node V =; Head [U] = TOT; } void DFS (int POS) // DFS traversal { Visit [POS] =. 1; IF (COL [POS]) // if you have been painted, and the other two colors program number is 0. DP [POS] [COL [POS]] =. 1; the else // the three colors can be ♂ { DP [POS] [. 1] =. 1; DP [POS] [2] =. 1; DP [POS] [ . 3] =. 1; } for (int I = Register Head [POS]; I; I = H [I] .next) // find all the child nodes of the current point { int V = H [I] .node; IF ( ! Visit [V]) { DFS (V); // leaf node has been traversed downward until return DP [POS] [. 1] = DP [POS] [. 1] * ((DP [V] [2] + DP [V] [. 3])% P)% P; DP [POS] [2] = DP [ POS] [2] * ((DP [V] [. 1] + DP [V] [. 3])% P)% P; DP [POS] [. 3] = DP [POS] [. 3] * ((DP [ v] [2] + dp [ v] [1])% p)% p; // remember modulo transfer! } } } Int main () { int X, Y; n-= Read (); m = Read (); for (int I = Register. 1; I <n-; I ++) { X = Read (); Y = Read (); the add (X, Y); the add (Y, X); // bordered } for (int I = Register. 1; I <= m; I ++) { X = Read (); Y = Read (); COL [X] = Y; // color recording } DFS (. 1); // just a point when the roots like cout<<(dp[1][1]+dp[1][2]+dp[1][3])%p<<endl; return 0; }