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