poj1703 经典并查集
2016-11-17 20:50
246 查看
一道非常经典的并查集,建议还可以做下hdu5971,这样我感觉一般的并查集就并没有神马威胁了。。。
祭奠我为此付出的好几天时间。。。
懒得加注释,真的要想搞懂的话建议花时间自己看懂,毕竟ac代码已经写出来了。
但是实在不会的话详询请加qq1933160466。
2333333333333333333333333333333
两份代码,看懂哪个算哪个。#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=2*1e5+5;
int par[maxn];
int n;
int find(int num)
{
if(par[num]<0)
return num;
return par[num]=find(par[num]);
}
int main()
{
int t,m;
scanf("%d",&t);
while(t--&&scanf("%d%d",&n,&m)!=EOF)
{
memset(par,-1,sizeof(par));
while(m--)
{
int a,b;
char s[5];
scanf("%s%d%d",s,&a,&b);
if(s[0]=='D')
{
if(find(a)!=find(b+n))
{
par[find(a)]=find(b+n);
par[find(b)]=find(a+n);
}
}
else
{
if(find(a)!=find(b)&&find(a)!=find(b+n))
printf("Not sure yet.\n");
else if(find(a)==find(b))
printf("In the same gang.\n");
else
printf("In the different gangs.\n");
}
}
}
return 0;
}
祭奠我为此付出的好几天时间。。。
懒得加注释,真的要想搞懂的话建议花时间自己看懂,毕竟ac代码已经写出来了。
但是实在不会的话详询请加qq1933160466。
2333333333333333333333333333333
两份代码,看懂哪个算哪个。#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=2*1e5+5;
int par[maxn];
int n;
int find(int num)
{
if(par[num]<0)
return num;
return par[num]=find(par[num]);
}
int main()
{
int t,m;
scanf("%d",&t);
while(t--&&scanf("%d%d",&n,&m)!=EOF)
{
memset(par,-1,sizeof(par));
while(m--)
{
int a,b;
char s[5];
scanf("%s%d%d",s,&a,&b);
if(s[0]=='D')
{
if(find(a)!=find(b+n))
{
par[find(a)]=find(b+n);
par[find(b)]=find(a+n);
}
}
else
{
if(find(a)!=find(b)&&find(a)!=find(b+n))
printf("Not sure yet.\n");
else if(find(a)==find(b))
printf("In the same gang.\n");
else
printf("In the different gangs.\n");
}
}
}
return 0;
}
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn=1e5+5; int fa[maxn],s[maxn],n; void init() { for(int i=1;i<=n;i++) fa[i]=i,s[i]=0; } int find(int num,int &rel) { int relf,sa; if(fa[num]==num) { rel=0; return num; } sa=find(fa[num],relf); s[num]=s[num]^relf; fa[num]=sa; rel=s[num]; return sa; } void update(int x,int y) { int relx,rely,rel,fx,fy; fx=find(x,relx); fy=find(y,rely); if(fx!=fy) { rel=relx^rely^1; s[fx]=rel; fa[fx]=fy; } } void ask(int x,int y) { int sx,sy,relx,rely; sx=find(x,relx); sy=find(y,rely); if(sx!=sy) printf("Not sure yet.\n"); else if(relx^rely) printf("In different gangs.\n"); else printf("In the same gang.\n"); } int main() { int t,m,x,y; char ss[5]; scanf("%d",&t); while(t--&&scanf("%d%d\n",&n,&m)!=EOF) { init(); while(m--&&scanf("%s",ss)!=EOF) { if(ss[0]=='D') { scanf("%d%d",&x,&y); update(x,y); } else { scanf("%d%d",&x,&y); ask(x,y); } } } return 0; }
相关文章推荐
- POJ 1703 Find them,Catch them ----种类并查集(经典)
- poj--1703/2492 (经典并查集)
- POJ-1703 Find them, Catch them 经典并查集
- POJ 1703 Find them, Catch them(经典并查集)
- POJ 1703 Find them,Catch them ----种类并查集(经典)
- poj 1611:The Suspects(并查集,经典题)
- openoj的一个小比赛(J题解题报告)poj1703(并查集)
- 并查集:POJ No1703 Find them, Catch them
- poj 1703 Find them, Catch them(种类并查集)
- poj 1703 Find them, Catch them(并查集)
- POJ 1703 并查集
- POJ 1182食物链(经典的并查集)
- 带权并查集,经典-食物链,poj-1182
- poj1703 并查集
- POJ 2912 Rochambeau【并查集经典应用同食物链+枚举】
- poj1703 并查集
- POJ 1703 Find them, Catch them (数据结构-并查集)
- poj 1703(带权并查集)
- POJ 1703 Find them, Catch them (并查集)
- poj 1703 Find them, Catch them 并查集