More is better (并查集)
来源:互联网 发布:淘宝网信鸽鸽具用品 编辑:程序博客网 时间:2024/05/18 20:10
Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be. Of course there are certain requirements.
Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang's selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.
Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang's selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.
41 23 45 61 641 23 45 67 8
42
AC代码:
#include<cstdio>
#include<algorithm>#include<cstring>
int pre[10000000+10],vis[10000000+10];
int find(int x)
{
int r=x;
while(r!=pre[x])
{
r=pre[x];
}
int i=x,j;
while(i!=r)
{
j=pre[i];
pre[i]=r;
i=j;
}
return r;
}
void join(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx!=fy)
{
pre[fy]=fx;
vis[fx]+=vis[fy];
}
}
int main()
{
int n,max=0,big;
while(~scanf("%d",&n))
{
int a,b,max=1;
for(int i=1;i<=10000002;i++)
{
pre[i]=i;
vis[i]=1;
}
while(n--)
{
scanf("%d%d",&a,&b);
join(a,b);
big=a>b?a:b;
}
for(int i=1;i<=big;i++)
if(vis[i]>max)
max=vis[i];
printf("%d\n",max);
}
return 0;
}
阅读全文
0 0
- More is better(并查集)
- more is better(并查集)
- More is better (并查集)
- More is better (并查集)
- More is better(并查集)
- More is better (并查集)
- More is better hdu1856 并查集
- hdu1856 ,More is better,并查集
- 并查集C - More is better
- HDOJ1856 More is better 【并查集】
- Hdu1856 - More is better - 并查集
- HDU1856 More is better 并查集
- 并查集 HDOJ1856 More is better
- HDU1856 More is better 并查集
- More is better---并查集实践
- More is better(并查集)
- hdu1856 More is better 并查集
- HDU1856 More is better(并查集)
- 电脑重启后nagios报错Unable to get process status
- C++类与对象
- ue4 fatal error LNK1181 Engine\Binaries\Win64\UE4-UMGEditor.lib
- 移动端去除横向滚动条
- [RK3288][Android6.0] SystemUI加载过程(状态栏和导航栏)
- More is better (并查集)
- Java 8系列之重新认识HashMap
- Struts2的表单标签使用
- 使用StaggeredGridLayoutManager实现交错式网格布局
- 使用JavaMail进行邮件的发送
- svn 常用操作命令
- nagios报错 It appears as though you do not have permission to view information for any of the hosts yo
- JAVA-prim 最小生成树
- Android应用内切换应用语言