POJ1703 Find them,Catch them
来源:互联网 发布:php企业站 编辑:程序博客网 时间:2024/06/05 08:07
题目大意:已知有两个团伙,给定一些信息。信息中表示两人不处于同一团伙。同时有一些询问,问两人是否在同一团伙中。
思路:显然这道题需要使用并查集。但是,似乎并查集并没有让两个元素不处于同一集合中的功能。于是对于给出的x和y,我们可以让x与y+n处于同一集合,让y与x+n处于同一集合,这样便能很好地解决这个问题。
判断条件:查询时,若getfa(x)=getfa(y),那么它们就在同一帮派;若getfa(x)=getfa(y+n)或getfa(x+n)=getfa(y),那么它们在不同帮派;否则信息不足,无法判定。
注意:我们在读取字符时,一定要用getchar( )去除回车符。
代码如下:
#include<iostream>#include<cstdio>using namespace std;const int maxn=100005;int n,m,t,fa[maxn*2];int getfa(int x){ if (fa[x]==x) return x; fa[x]=getfa(fa[x]); return fa[x];}void unite(int x,int y){ fa[getfa(x)]=getfa(fa[y]);}void init(){ scanf("%d",&t); char ch; int x,y,i,j; while (t--) { scanf("%d%d",&n,&m); getchar(); for (i=1;i<=n*2;++i) fa[i]=i; for (i=1;i<=m;++i) { scanf("%c%d%d",&ch,&x,&y); getchar(); if (ch=='D') { unite(x,y+n); unite(x+n,y); } else { if (getfa(x)==getfa(y)) printf("In the same gang.\n"); else if (getfa(x)==getfa(y+n) || getfa(x+n)==getfa(y)) printf("In different gangs.\n"); else printf("Not sure yet.\n"); } } }}int main(){ init(); return 0;}
0 0
- poj1703 - Find them, Catch them
- POJ1703 Find them, Catch them
- poj1703 Find them, Catch them
- poj1703 Find them, Catch them
- POJ1703 Find them,Catch them
- POJ1703--Find them, Catch them
- poj1703 find them,catch them
- Find them, Catch them poj1703
- POJ1703 Find them, Catch them
- poj1703 Find them, Catch them
- poj1703 Find them, Catch them
- POJ1703-Find them, Catch them
- POJ1703 Find them, Catch them
- poj1703 Find them, Catch them
- poj1703 Find them, Catch them
- poj1703——Find them, Catch them
- poj1703——Find them, Catch them
- Find them, Catch them (poj1703)
- 这个错误是由于强制要求应用程序按照多国语言的模式开发,避免死的字符串。
- Project Euler Problem 16
- IOS点击空白处隐藏键盘的几种方法
- Android ViewGroup触摸事件拦截详解
- Hadoop SequcenceFile 处理多个小文件
- POJ1703 Find them,Catch them
- WCF实现消息推送(单向操作/观察者模式)
- LoadRunner函数详解之web_reg_find
- Android DiskLruCache完全解析,硬盘缓存的最佳方案
- poj 1651 Multiplication Puzzle 矩阵链乘问题
- Fragment进阶(六)----->Fragment与Activity通讯3种方式
- UI 06 ScrollView 的手动循环播放 与 自动循环播放
- webview 错误(访问不到页面)
- Roundcube Webmail 安装配置图文详情