poj 1703——Find them, Catch them
来源:互联网 发布:新浪财经数据 编辑:程序博客网 时间:2024/06/06 20:36
题意:有两个帮派,警察要调查出每个人属于哪个帮派。每次更新的两个数据是属于不同帮派的人,每次查询的是两个人是否属于一个帮派。
思路:用并查集来表示可以扯上关系的人。如果两个人的关系可以判定,那么他们就属于一个集合。在一个集合中,用r来表示节点跟父节点的距离。如果距离是0,那么这个节点就是父节点。如果距离是1,那么这个节点跟父节点相差一个,就属于不同帮派,如果距离是2,就是一个帮派,以此类推。
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int maxn=100005;int father[maxn];int r[maxn];int n,m;void init(){ for(int i=1;i<=n;++i){ father[i]=i; r[i]=0; }}int find(int x){ if(x==father[x]){ return x; } else { int tmp=father[x]; father[x]=find(father[x]); r[x]=r[x]+r[tmp]; return father[x]; }}void merge(int a,int b){ int x=find(a); int y=find(b); if(x!=y){ father[x]=b; r[x]=r[a]+1; }}void solve(int a,int b){ int x=find(a); int y=find(b); if(x==y){ if((r[a]+r[b])&1){ puts("In different gangs."); } else { puts("In the same gang."); } } else { printf("Not sure yet.\n"); }}int main(){// freopen("data.txt","r",stdin); int T; scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); init(); for(int i=0;i<m;++i){ char op='z'; while(op!='A'&&op!='D'){ scanf("%c",&op); } int a,b; scanf("%d%d",&a,&b); if(op=='A'){ solve(a,b); } else { merge(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
- 使用node.js cheerio抓取网页数据
- discuz修改标签列表页面显示标签的数量
- 2014年工作总结
- Linux下编译FFmpeg之下载相关解码库并再次编译
- 2034,要学会找到自己哪里错了,多考虑极限情况
- poj 1703——Find them, Catch them
- 子集生成(二进制法)-java实现
- 微软或将对外推出付费版Cosmos大数据服务
- 单选、复选框、下拉列表等实现
- linux下vim中文乱码的解决方法
- Unity3D游戏开发之设置动画(Animations)属性
- 欢迎使用CSDN-markdown编辑器
- (二十一)即时通信的聊天气泡的实现II
- 求一棵二叉树的高度