您的位置:首页 > 其它

CSU1908-The Big Escape-并查集

2017-06-27 00:40 190 查看

V: The Big Escape

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 2
1 2
2 3
2 4
2 5
1 6
5 7
3 8
2 9
2 10


Sample Output

Case #1:2


该题等价于求一棵树的各子树中含节点最多的节点个数,推理就自己想吧

#include <bits/stdc++.h>
#define N 101000
#define INF 0x3f3f3f3f
#define LL long long
#define mem(a,n) memset(a,n,sizeof(a))
#define fread freopen("in.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
int par
,ans
,root;
void init(int n)
{
for(int i=0;i<=n;++i){
ans[i]=0;
par[i]=0;
}
}
int find(int x)
{
if(par[x]==x){
return x;
}
return find(par[x]);
}
void unite(int x,int y)
{
if(x==root){
par[y]=y;
ans[y]+=1;
}else if(y==root){
par[x]=x;
ans[x]+=1;
}else if(par[x]){
par[y]=par[x];
ans[par[x]]++;
}else{
par[x]=par[y];
ans[par[y]]++;
}
//  int fx=find(x);
//  int fy=find(y);
//  if(fx==fy){
//      return;
//  }else{
//      if(fy==root){
//          par[fx]=fy;
//          if(rak[fx]==rak[fy]){
//              ++rak[fy];
//          }
//      }else{
//          par[fy]=fx;
//          if(rak[fx]==rak[fy]){
//              ++rak[fx];
//          }
//      }
//  }
}
bool same(int x,int y)
{
return find(x)==find(y);
}
int main()
{
ios::sync_with_stdio(false);
int n,a,b,kase=0;
while(cin>>n>>root){
init(n);
for(int i=1;i<n;++i){
cin>>a>>b;
unite(a,b);
//          for(int i=1;i<=n;++i){
//              cout<<i<<' '<<par[i]<<' '<<ans[i]<<endl;
//          }
}
int an=0;
for(int i=1;i<=n;++i){
//          cout<<i<<' '<<par[i]<<' '<<ans[i]<<endl;
if(ans[i]>an){
an=ans[i];
}
}
cout<<"Case #"<<++kase<<':'<<an<<endl;
}
return 0;
}

/**********************************************************************
Problem: 1908
User: CSUzick
Language: C++
Result: AC
Time:216 ms
Memory:2476 kb
**********************************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: