CSU 1045: 并查集(带权并查集)
来源:互联网 发布:rundll32调用js 编辑:程序博客网 时间:2024/05/23 01:06
CSU 1045: 并查集 带权并查集
Description
大一的学一下,大二以上还不会并查集的统统去面壁。
Input
多组数据,每组第一行两个正整数n,m,表示有1~n这n个编号,m个关系。
接下来m行,每行两个数i, j, 1 <= i, j <= n,表示i和j是一组的。
每个编号自己和自己是一组的。
1 < n < 1000000 ,1 < m < 100000 。
Output
每组数据输出一行,一个数,表示组员最多的组的组员个数。
Sample Input
10 51 23 52 64 79 6
Sample Output
4
Hint
Source
中南大学2012年暑期集训队选拔赛思路: 带权并查集
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int f[1000005];int n,m;int a,b;int father(int x){ if(f[x] <=0 ) return x; else return f[x] = father(f[x]);}void union_(int a,int b){ int root1 = father(a); int root2 = father(b); if(root1 == root2) return; f[root1] += f[root2]; f[root2] = root1;}int main(){ while(scanf("%d%d",&n,&m) != EOF) { memset(f,-1,sizeof(f)); for(int i = 0 ; i < m ; i++) scanf("%d%d",&a,&b),union_(a,b); int minx = 0; for(int i = 1 ; i <= n ; i++) minx = min(minx,f[i]); printf("%d\n",-minx); } return 0;}
阅读全文
0 0
- CSU 1045: 并查集(带权并查集)
- CSU 1601(并查集)
- csu 1601 War(并查集)
- CSU 1601 War (并查集)
- 并查集--带权
- 带权并查集
- 带权并查集
- 带权并查集
- 带权并查集
- 带权并查集
- 带权并查集
- 带权并查集
- 带权并查集
- 带权并查集(hdu2818)
- 食物链(带权并查集)
- 食物链(带权并查集)
- Rochambeau(带权并查集)
- 带权并查集(模板)
- Spring任务调度之Spring-Task--应用实例:实现邮件(带附件)定时发送功能
- Java NIO框架Netty教程(一)
- 【APIGateway】微服务,ApiGateway
- qt 静态编译
- 如何培养面向对象的编程思想(转)
- CSU 1045: 并查集(带权并查集)
- JAVA MemCache 史无前例的详细讲解!看完包精通MEMCACHE!
- iframe使用
- poj3686 KM算法+拆点
- 你月薪8000还是月薪20000,又能怎样?
- 使用人工客服的注意事项
- 【codevs 1082】线段树练习三(区间修改+区间和)
- opencv的 threshold
- HDU6130-Kolakoski