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 **********************************************************************/
相关文章推荐
- CSU 1908: The Big Escape
- C(1908)The Big Escape
- hdu1598 find the most comfortable road(并查集)
- Online algorithms for uploading deferrable big data to the cloud文章阅读笔记
- Think big while you’re doing the small things
- CSU 1855: Shut the Box 二进制状态压缩
- POJ 1611 The Suspects 并查集裸题
- Draw a big picture of the future.
- 【iOS Programming: The Big Nerd Ranch Guide】【笔记】1
- 文章标题 POJ 1611 : The Suspects (并查集)
- poj—The Suspects(并查集)
- iOS Programming The Big Nerd Ranch Guide (4th Edition)
- Code Forces 592 C. The Big Race(数论)
- hdu 1598 find the most comfortable road (并查集)
- Open Source Big Data for the Impatient, Part 1
- HDU 3974 Assign the task 并查集
- POJ---1611 The Suspects【并查集】
- POJ1611:The Suspects【并查集】
- find the most comfortable road(并查集与图论)
- HDU 1598--find the most comfortable road【并查集 + 枚举】