日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

P4084 [USACO17DEC]Barn Painting

發(fā)布時(shí)間:2023/12/8 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 P4084 [USACO17DEC]Barn Painting 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題意翻譯

題意:給定一顆N個(gè)節(jié)點(diǎn)組成的樹(shù),3種顏色,其中K個(gè)節(jié)點(diǎn)已染色,要求任意兩相鄰節(jié)點(diǎn)顏色不同,求合法染色方案數(shù)。 翻譯貢獻(xiàn)者:Il_ItzABC_lI

題目描述

Farmer John has a large farm with?NN?barns (1 \le N \le 10^51N105), 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 N0KN), 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 y1x,yN,xy) describing a path directly connecting barns?xx?and?yy.

The next?KK?lines each contain two integers?bb?and?cc?(1 \le b \le N1bN,?1 \le c \le 31c3) 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];//由于存圖為無(wú)向圖,所以要記錄下已經(jīng)走過(guò)的點(diǎn)防止死循環(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)上過(guò)色了,其他兩種顏色的方案數(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)前點(diǎn)所有的子節(jié)點(diǎn){int v=h[i].node;if(!visit[v]){dfs(v);//一直向下遍歷直到葉子節(jié)點(diǎn)返回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);//隨便把一個(gè)點(diǎn)當(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的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。