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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Tree Cutting POJ - 2378(树形DP)

發布時間:2023/12/4 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Tree Cutting POJ - 2378(树形DP) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題意:有n個谷倉有n-1條路連接,問最少刪除哪幾個點才能使得刪除點后得到的連通圖的加點數不大于n/2.

分析:求樹的重心的變形題,poj3107的簡單版,一遍dfs從葉子到根轉移找出找到以每個節點為根的子樹的結點數,f[u]={ f[v1]+f[v2]+.....+f[vn] }+1;使得每棵子樹節點數小于n/2,并且父節點得那個連通圖節點數小于等于n/2,即n-f[u]<=n/2.

如果沒有這樣的點,輸出NONE

Time limit? ? ?1000 ms

Memory limit? ? ?65536 kB

OS? ? ?Linux

Source? ? ??USACO 2004 December Silver

After Farmer John realized that Bessie had installed a "tree-shaped" network among his N (1 <= N <= 10,000) barns at an incredible cost, he sued Bessie to mitigate his losses.?

Bessie, feeling vindictive, decided to sabotage Farmer John's network by cutting power to one of the barns (thereby disrupting all the connections involving that barn). When Bessie does this, it breaks the network into smaller pieces, each of which retains full connectivity within itself. In order to be as disruptive as possible, Bessie wants to make sure that each of these pieces connects together no more than half the barns on FJ.?

Please help Bessie determine all of the barns that would be suitable to disconnect.

Input

* Line 1: A single integer, N. The barns are numbered 1..N.?

* Lines 2..N: Each line contains two integers X and Y and represents a connection between barns X and Y.

Output

* Lines 1..?: Each line contains a single integer, the number (from 1..N) of a barn whose removal splits the network into pieces each having at most half the original number of barns. Output the barns in increasing numerical order. If there are no suitable barns, the output should be a single line containing the word "NONE".

Sample Input

10 1 2 2 3 3 4 4 5 6 7 7 8 8 9 9 10 3 8

Sample Output

3 8

Hint

INPUT DETAILS:?

The set of connections in the input describes a "tree": it connects all the barns together and contains no cycles.?

OUTPUT DETAILS:?

If barn 3 or barn 8 is removed, then the remaining network will have one piece consisting of 5 barns and two pieces containing 2 barns. If any other barn is removed then at least one of the remaining pieces has size at least 6 (which is more than half of the original number of barns, 5).

題意:

給定一棵無向樹,節點為n(n<=10000),問刪除那些節點可以使得新圖中的每一個連通分支的節點數都不超過小于n/2

思路:

樹形dp,任意定跟,求出每個節點的兒子節點,每個點為根的子樹的節點數dp[i],然后考察每一個點,如果n-dp[i]<=n/2,以及以i的每一個兒子為根的子樹的節點數都dp[u]<=n/2,那么這個點就滿足條件

/*題意:給了一棵節點數為n的樹的對應關系,讓判斷去掉哪些 節點能使子數的大小小于等于n/2,并將符合條件的節點從小到 大輸出,如果沒有符合條件的節點,則輸出 “NONE”。 思路:既然是讓求哪些ji節點符合條件,可以用鄰接表構建一個 圖,然后用dfs找出每一個節點下連的圖的da'x大小些節點符合 條件,將節點存入ans數組中。*/ #include<cstdio> #include<cstring> #include<algorithm> #define N 10010 using namespace std; int n,s,e,first[N],book[N],ans[N]; struct node {int x,y; }que[2*N]; void add(int x,int y) //構建鄰接表的函數 {que[e].x=x;que[e].y=first[y];first[y]=e++; } int dfs(int x) {int num=0,sum=0;book[x]=1;int k=first[x],flag=0;while(k!=-1){if(!book[que[k].x]){num=dfs(que[k].x);sum+=num;if(num>n/2) //x節點下的圖的大小超過了n/2,不符合條件flag=1;}k=que[k].y;}if(n-sum-1>n/2) //另一半是否小于等于n/2flag=1;if(!flag)ans[s++]=x;return sum+1; //加上自己 } int main() {while(~scanf("%d",&n)){memset(first,-1,sizeof(first));//初始化memset(book,0,sizeof(book));s=e=0; //有s個節點符合條件,e用鄰接表表示無向圖的大小int a,b;for(int i=1;i<n;i++){scanf("%d%d",&a,&b);add(a,b); //構建鄰接表add(b,a); //無向圖}a=dfs(1); //從第一個點開始嘗試if(s) //有s個節點符合條件{sort(ans,ans+s); //從小到大排序,輸出for(int i=0;i<s;i++)printf("%d\n",ans[i]);}else //沒有符合條件的節點printf("NONE\n");} }

?

總結

以上是生活随笔為你收集整理的Tree Cutting POJ - 2378(树形DP)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: av先锋影音 | 国产91精品欧美 | 久久亚洲精少妇毛片午夜无码 | 琪琪伦伦影院理论片 | 加勒比精品 | 春意影院福利社 | 香蕉视频一区二区三区 | 亚洲一区二区天堂 | 国产鲁鲁视频在线观看免费 | 久久久久成人精品免费播放动漫 | 青青草免费看 | 色婷婷导航 | 极品少妇xxxx| www.99视频| 99精品亚洲 | 国产五十路 | a∨色狠狠一区二区三区 | 狠狠干五月天 | 天天撸天天操 | 免费男女乱淫真视频免费播放 | 天天舔日日操 | 国产男女无套 | 国产美女裸体无遮挡免费视频 | 亚洲综合日韩 | 国产视频一区二区不卡 | 韩国伦理电影免费在线 | 欧美日韩亚 | 97人人澡人人爽人人模亚洲 | 午夜激情在线观看视频 | 国产视频观看 | 色版视频 | 国产日韩欧美中文 | 日本欧美中文字幕 | 日操干 | 久久精品无码人妻 | 婷婷色伊人| 国产精品无码免费在线观看 | 日本三级在线视频 | 男女视频在线观看免费 | mm1313亚洲国产精品无码试看 | 亚洲成人一区二区 | 日韩精品在线观看中文字幕 | 欧美xxxxx视频 | 日韩中文字幕免费在线观看 | 亚洲天堂网视频 | 久久久久久久久免费看无码 | 日韩字幕 | 欧美a级肉欲大片xxx | 中文字幕在线观看网 | 自拍偷拍色综合 | 欧美sm凌虐视频网站 | 国产一级二级在线观看 | 男女啪啪无遮挡 | 男同精品 | 亚洲久久久久久 | 在线美女av| 国产aaaaaaa| 午夜精品福利在线 | 国产粉嫩在线观看 | 夫妻性生活自拍 | 久久久久久久久久久福利 | 第四色视频 | 日韩av中文字幕在线播放 | 91色区| 久久作爱 | 国产做爰免费视频观看 | 美丽姑娘免费观看在线观看 | 金鱼妻日剧免费观看完整版全集 | 国产乱码一区二区三区 | 欧美国产一区二区在线观看 | 国产毛片a | 国产精品女同 | 丁香综合激情 | 欧美一级片观看 | 麻豆91在线观看 | 中文字幕在线观看免费视频 | 久久免费片 | 欧美a级黄色片 | 超碰人人人人 | 图片区 小说区 区 亚洲五月 | 狼人色综合 | 国产字幕在线观看 | 日韩成人无码 | 超碰人人射 | 国产亚洲精品久久久久丝瓜 | 台湾佬美性中文娱乐 | 日韩国产亚洲欧美 | 中国大陆一级毛片 | 伊人9999| 日本草逼视频 | 色臀 | 91精品视频在线播放 | 一区二区三区黄色录像 | 精品久久久久久久久久久久久久久久 | 激情区 | 蜜臀一区二区三区精品免费视频 | 2020av在线 | 自拍偷拍欧美激情 | 少妇视频在线观看 |