POJ 1703 Find them, Catch them
来源:互联网 发布:ppt 图标 知乎 编辑:程序博客网 时间:2024/05/29 14:50
种类并查集,高端大气上档次吧~~
题目大意:
一共有N个人,给出M个操作分为两种:
1、A a b :提问a和b是否是同一个帮派的。有三种答案:是,不是和不确定
2、D a b :a和b不是同一个帮派的。
解题思路:
种类并查集,在一个集合里的证明他们之间有关系,种类里有0,和1两种。0代表着同父节点相同势力,1代表不同。
下面是代码:
#include <stdio.h>int n,q;int fath[100005];int status[100005];void init(){ for(int i=1; i<=n; i++) { fath[i]=i; status[i]=0; }}int findd(int x){ if(fath[x]==x)return x; else { int y=findd(fath[x]); status[x]=(status[x]+status[fath[x]])&1;//x的阵营与fath[x]不同 fath[x]=y; return y; }}void SetUnion(int a ,int b){ int x=findd(a); int y=findd(b); if(x==y)return ; fath[x]=y; if(status[b]) { status[x]=status[a]; } else { status[x]=1-status[a]; }}void judge(int a,int b){ int x=findd(a); int y=findd(b); if(x!=y)printf("Not sure yet.\n"); else { if(status[a]==status[b])printf("In the same gang.\n"); else printf("In different gangs.\n"); }}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&q); init(); char s[3]; int a ,b; for(int i=0; i<q; i++) { scanf("%s%d%d",s,&a,&b); if(s[0]=='D') { SetUnion(a,b); } else if(s[0]=='A') { judge(a,b); } } } return 0;}
0 0
- POJ 1703 Find them, Catch them
- poj 1703 Find them, Catch them
- POJ-1703 Find them, Catch them
- Poj 1703 Find them, Catch them
- poj 1703 Find them, Catch them
- POJ 1703 Find them, Catch them
- poj 1703 Find them, Catch them
- poj 1703 Find them, Catch them
- POJ 1703 - Find them, Catch them
- POJ 1703 Find them, Catch them
- POJ 1703(Find them, Catch them)
- POJ 1703 Find them, Catch them
- poj 1703 Find them, Catch them
- poj 1703 Find them, Catch them
- POJ 1703: Find them, Catch them
- poj 1703 find them,catch them
- POJ--1703--Find them, Catch them
- POJ 1703 Find them, Catch them
- Cadence 学习错误记载
- LeetCode OJ --问题与解答 Binary Tree Preorder Traversal
- MD5加密
- Silverlight 8.1 关键变化
- CSS的background属性的缩写顺序
- POJ 1703 Find them, Catch them
- SQL SERVER中求上月、本月和下月的第一天和最后一天
- NPOI 导入不了2007以上版本Excel
- 图解SQL的inner join、left join、right join、full outer join、union、union all的区别
- iOS开发-UIButton中title位置设置
- Android开发,常用的终端命令
- java构建简单的ftp服务
- UML类图几种关系的总结
- 2014记首