POJ1611 The Suspects 并查集
来源:互联网 发布:淘宝十周年宣传片 编辑:程序博客网 时间:2024/06/04 18:32
没什么好说的,建立n的并查集,把每个group的人都连起来,最后遍历一遍即可。
#include<iostream>#include<cstdio>#include<cstring>#include<ctime>#include<algorithm>#include<cstdlib>#include<cmath>#include<set>#include<bitset>#include<map>#include<stack>#include<queue>#include<vector>#include<utility>#define INF 0x3f3f3f3f#define inf 2*0x3f3f3f3f#define llinf 1000000000000000000#define pi acos(-1.0)#define mod 1000000007#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define lrt rt<<1#define rrt rt<<1|1#define rep(i,a,b) for(int i=(a);i<(b);i++)#define per(i,a,b) for(int i=(b)-1;i>=(a);i--)#define mem(a,b) memset(a,b,sizeof(a))#define lowbit(x) (x&-x)#define gi(x) scanf("%d",&x)#define gll(x) scanf("%lld",&x)#define gc(x) scanf("%c",&x)using namespace std;typedef long long ll;typedef unsigned long long ull;typedef pair<int,int>P;/***********************************************/int par[30005],ranks[30005],sta[30005],up,n,m;void init(int n){ rep(i,0,n)par[i]=i,ranks[i]=0;}int finds(int x){ if(par[x]==x)return x; return par[x]=finds(par[x]);}void unite(int x,int y){ x=finds(x);y=finds(y); if(x==y)return ; if(ranks[x]<ranks[y])par[x]=y; else par[y]=x; if(ranks[x]==ranks[y])ranks[x]++;}bool same(int x,int y){ return finds(x)==finds(y);}int main(){ while(scanf("%d%d",&n,&m)) { init(n); if(n==0&&m==0)break; int k,x; rep(i,0,m) { gi(k);up=0; rep(j,0,k) { gi(x); if(up!=0)unite(sta[up-1],x); sta[up++]=x; } } int cnt=0; rep(i,0,n)if(same(i,0))cnt++; printf("%d\n",cnt); } return 0;}
0 0
- POJ1611 The Suspects 并查集求解
- POJ1611 The Suspects 并查集
- poj1611 The Suspects (并查集)
- poj1611-The Suspects--并查集
- poj1611 The Suspects(并查集)
- POJ1611 The Suspects (并查集)
- 并查集 poj1611 The Suspects
- POJ1611 The Suspects 并查集
- poj1611 The Suspects(基础并查集)
- poj1611 The Suspects 并查集
- POJ1611:The Suspects【并查集】
- poj1611 The suspects 并查集
- 【并查集】:poj1611,The Suspects
- POJ1611 The Suspects(并查集)
- poj1611 the-suspects 并查集
- POj1611:The Suspects(并查集)
- POJ1611 The Suspects 种类并查集
- POJ1611 The Suspects(并查集)
- 手机app首次进入首界面,广告界面,和主界面
- 3128 递归基础之放苹果
- 带赖子的麻将胡牌及其听牌算法研究
- freemaker日期格式化处理
- R语言回归分析
- POJ1611 The Suspects 并查集
- 高斯金字塔的思考
- J2EE:JUnit--Java单元测试框架
- 第四届省赛原题 走迷宫
- Android中Activity的四种启动模式
- 一起Talk Android吧(第十九回:Java常用类之Date)
- Unity 3D
- 从JAVA身边路过,他却没有看我,我也没有抓住
- sql中操作数据累加