P4084 [USACO17DEC]Barn Painting
題意翻譯
題意:給定一顆N個節(jié)點組成的樹,3種顏色,其中K個節(jié)點已染色,要求任意兩相鄰節(jié)點顏色不同,求合法染色方案數(shù)。 翻譯貢獻(xiàn)者:Il_ItzABC_lI
題目描述
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?
輸入格式
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.
輸出格式
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.
輸入輸出樣例
輸入 #1復(fù)制 4 1 1 2 1 3 1 4 4 3 輸出 #1復(fù)制 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];//由于存圖為無向圖,所以要記錄下已經(jīng)走過的點防止死循環(huán) struct tree {int next,node; }h[200010]; inline void add(int u,int v)//鄰接表存圖 {h[++tot].next=Head[u];h[tot].node=v;Head[u]=tot; } void dfs(int pos)//dfs遍歷 {visit[pos]=1;if(col[pos])//如果已經(jīng)上過色了,其他兩種顏色的方案數(shù)為0。dp[pos][col[pos]]=1;else//三種顏色都可以被♂上{dp[pos][1]=1;dp[pos][2]=1;dp[pos][3]=1;}for(register int i=Head[pos];i;i=h[i].next)//找到當(dāng)前點所有的子節(jié)點{int v=h[i].node;if(!visit[v]){dfs(v);//一直向下遍歷直到葉子節(jié)點返回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;//轉(zhuǎn)移 記得取模!}} } int main() {int x,y;n=read();m=read();for(register int i=1;i<n;++i){x=read();y=read();add(x,y);add(y,x);//加邊}for(register int i=1;i<=m;++i){x=read();y=read();col[x]=y;//記錄顏色}dfs(1);//隨便把一個點當(dāng)根就好了cout<<(dp[1][1]+dp[1][2]+dp[1][3])%p<<endl;return 0; }
轉(zhuǎn)載于:https://www.cnblogs.com/xiongchongwen/p/11243553.html
總結(jié)
以上是生活随笔為你收集整理的P4084 [USACO17DEC]Barn Painting的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Unity3D手游开发实践
- 下一篇: 开怀大笑有助于使心中的郁闷情绪得到疏导