C(1908)The Big Escape
来源:互联网 发布:电脑游戏录制视频软件 编辑:程序博客网 时间:2024/06/04 19:43
Description
There is a tree-like prison. Expect the root node, each node has a prisoner, and the root node is the only exit. Each node can accommodate a large number of prisoners, but each edge per minute only one prisoner can pass.
Now, the big escape begins, every prisoner wants to escape to the exit.Do you know when the last one escapes from the prison.
Input
There are lots of case.
For each test case.The first line contains two integers n,m(n<=100000, 1<=m<=n), which indicates the number of nodes and the root node.
The next n-1 lines describe the tree.
Output
For each test case, you output one line “Case #%d:%d”
Sample Input
10 21 22 32 42 51 65 73 82 92 10
Sample Output
Case #1:2
Hint
题意:有一个树形监狱,除了根节点,其他每个节点都有一个人,大家都要逃到根节点才能出去,每条边每分钟只能有一个人通过,问最后逃出的人的时间。
思路:最后逃出去的人应该是处在最深的子树中的一个,所以时间应该为最大的子树的大小。
#include<iostream>using namespace std;int fa[100005],sum[100005];int findfa(int u){ return fa[u]==u?u:findfa(fa[u]);}int main(){ int n,root,cas=0; while(cin>>n>>root){ for(int i=1;i<=n;i++) fa[i]=i,sum[i]=1; for(int u,v,i=1;i<n;i++){ cin>>u>>v; if(u==root||v==root) continue; int uf=findfa(u),vf=findfa(v); if(uf!=vf) sum[uf]+=sum[vf],fa[vf]=uf; } int ans=0; for(int i=1;i<=n;i++){ if(i==root||fa[i]!=i) continue; ans=max(ans,sum[i]); } cout<<"Case #"<<++cas<<":"<<ans<<endl; }}
0 0
- C(1908)The Big Escape
- CSU 1908: The Big Escape
- CSU1908-The Big Escape-并查集
- codeforces #328 C. The Big Race
- cf C. The Big Race (简单模拟)
- Codeforces 592C The Big Race 【GCD && LCM】
- Codeforces Round #328 (Div. 2) C. The Big Race
- Codeforces Round #328 (Div. 2)C. The Big Race
- Codeforces Round #328 (Div. 2)C. The Big Race
- Codeforces Round #328 (Div. 2) C. The Big Race (math)
- Code Forces 592 C. The Big Race(数论)
- Codeforces Round #328 (Div. 2) 592 C. The Big Race
- Codeforces Round #328 (Div. 2)C. The Big Race
- Codeforces 592C The Big Race【数学啊】
- 10967 - The Great Escape
- c#escape和unescape
- Linux: the big picture
- The big bang theory!
- HDU3966 树链剖分
- Android setPixel抛出java.lang.IllegalStateException
- Codeforces Round #400 (Div. 1 + Div. 2, combined)C. Molly's Chemicals
- reactNative底部导航菜单栏实现
- 16山东省赛B题SDNU1504
- C(1908)The Big Escape
- java -- 我眼中的面向对象_1
- RandomForest新手自学笔记(多个网站博客整理)
- java基础--IO
- Java字符串创建与存储的机制
- HTML基础(一)
- nyoj 1249 物资调度(DP)
- 基本字符串压缩
- Android学习(4)