POJ 1703(Find them, Catch them)
来源:互联网 发布:金山卫士网络优化大师 编辑:程序博客网 时间:2024/06/10 05:19
题意:比较好懂,略;
思路:挺不错的一个并查集的题,这里需要记录每个人的敌人,和lrj黑书上的并查集专题那题有点像,就是敌人的敌人是朋友,用个数组记录下每个人的敌人就好了,也只需记录其中一个敌人即可,因为通过一个可以找到其他的,每次有更新信息D a b的时候就让a,enemy[b]和b,enemy[a]合并,因为他们肯定是一伙的,还要注意处理合并空集的情况;询问时,当a,b在同一集合时,他们是一伙的,当a的敌人里有b或b的敌人里有a则说明他们不是一伙的,转换成如果a和enemy[b]或b和enemy[a]是同一集合,则说明a和b不是一伙的,否则就是关系尚未确定;
#include <cstdio>#include <cstring>using namespace std;const int N = 100010;int check[N], enemy[N];int getFather(int x){if(x != check[x])check[x] = getFather(check[x]);return check[x];}void Union(int x, int y){int fx = getFather(x);int fy = getFather(y);if(fx != fy)check[fx] = fy;}int main(){int n, m, T, a, b, i;scanf("%d", &T);while(T--){char ord;memset(enemy, 0, sizeof(enemy));scanf("%d%d", &n, &m);for(i = 1;i <= n;i++)check[i] = i;for(i = 0;i < m;i++){getchar();scanf("%c%d%d", &ord, &a, &b);if(ord == 'D'){if(enemy[a] != 0)Union(b, enemy[a]);if(enemy[b] != 0)Union(a, enemy[b]);enemy[a] = b;enemy[b] = a;} else {if(getFather(a) == getFather(b))printf("In the same gang.\n");else if(getFather(enemy[a]) == getFather(b))printf("In different gangs.\n");elseprintf("Not sure yet.\n"); }}}return 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
- 检测非正常断开的TCP连接
- Android通过有线USB上网卡上网
- android sqlite 数据库读写
- 设计模式之结构型模式
- 用javascript编写的打字小游戏
- POJ 1703(Find them, Catch them)
- 使用mutt+msmtp做linux邮件客户端
- 使用jmeter测试web应用遇到的几个问题及解决
- 外企求职讲座听后小记
- linux php5 apache2 安装步骤
- myeclipse 无法部署项目
- yii文件命名要求
- LinkedHashMap 和 HashMap的区别
- ViewPager多页面滑动切换以及动画效果