poj 1703 Find them, Catch them
来源:互联网 发布:linux snmp 流量监控 编辑:程序博客网 时间:2024/05/21 15:49
题目意思:判断给定的2个人是否是同一团伙,或者是不同团伙。
集合的概念,不用多想,并查集。
这道题有2种不同的并查集。
第一种 一个集合,用一个数组保存和根节点的团伙关系0,代表同一团伙,1表示不同团伙。
第二种 2个集合 。 i 和 j+n j 和 i+n 建立 2个集合 就可以判断是属于那个集合里面了,,第二种似乎好理解点。
我用第一种 方法写,给出代码。
#include<cstdio>#include<cstring>#include<iostream>using namespace std;int root[500000],r[500000];int n,m;void init(){ int i; for(i=1;i<=n;i++){ root[i]=i; r[i]=0; }}int find(int x){ int temp=root[x]; if(x==root[x]) return x; else{ root[x]=find(root[x]); r[x]=(r[temp]==0?r[x]:1-r[x]); return root[x]; }}void Union(int a,int b,int ra,int rb){ root[rb]=ra; r[rb]=(r[a]==r[b]?1:0);}int main(){ int t; scanf("%d",&t); for(int number=1;number<=t;number++){ scanf("%d%d",&n,&m); init(); int a,b,i; char c; for(i=1;i<=m;i++){ getchar(); scanf("%c%d%d",&c,&a,&b); int ra=find(a); int rb=find(b); if(c=='D') Union(a,b,ra,rb); else if(c=='A'){ if(ra!=rb) printf("Not sure yet.\n"); else{ if(r[a]==r[b]) printf("In the same gang.\n"); else printf("In different gangs.\n"); } } } }}
- 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
- C#动态数据绘图graphic代码简单示例
- lua之class
- Unexpected error occurred while trying to check for group existence.
- 搜索引擎——原理技术与系统第三章
- easyui tree 的异步加载 前台的编写方法和后台的json数据格式组装方法
- poj 1703 Find them, Catch them
- 运用函数的递归求1*3*5*.......*n
- 一段时间的学习记录
- Intents and Intent Filters
- eclipse快捷键
- POI总结
- 关系数据库的功能
- box2d在跑跳类游戏中的应用
- C#计算器简单代码示例