Find them, Catch them poj 1703
来源:互联网 发布:最常用顶级域名 列表 编辑:程序博客网 时间:2024/05/11 21:45
传送门:poj1703
题意:就是有两个帮派,两个帮派之间是敌人关系,然后每次或者告诉你两个人是敌人关系,或者询问两个人的关系是什么(同伙,敌人或者不确定)。
这个题要是每次告诉你两个人是同伙那就是个水并查集啊,当时一读到输入的两个人是敌人关系就有点蒙,这该怎么处理啊。。。
最终没憋住还是查了题解。。关键点在于开一个数组保存每个人的敌人是谁,这样一来再输入两个人a,b是敌人关系的时候,如果diren[a]已经有了编号,那么就把b和diren[a]用并查集合并,反之亦然。如果diren[a]和diren[b]都没有编号的话,就无法判断他俩是哪个帮派的,因此只要把各自的敌人存起来就好了,无需合并。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int diren[100005],f[100005];int getf(int k){return k==f[k]?k:f[k]=getf(f[k]);}void merge(int x,int y){f[getf(y)]=getf(x);return ;}int main(){int t,n,m;scanf("%d",&t);while(t--){char c;int a,b;memset(diren,0,sizeof(diren));scanf("%d%d",&n,&m);for(int i=1;i<=n;i++)f[i]=i;for(int i=0;i<m;i++){getchar();scanf("%c%d%d",&c,&a,&b);if(c=='D'){if(diren[a])merge(diren[a],b);if(diren[b])merge(diren[b],a);diren[a]=b;diren[b]=a;}else{if(getf(a)==getf(b))printf("In the same gang.\n");else if(getf(a)==getf(diren[b]))printf("In different gangs.\n");elseprintf("Not sure yet.\n");}}}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
- 【u247】生物进化
- My SQL基本用法
- Linux BT下载(7)-种子解析模块设计与实现1
- 无意中的StackOverflowError
- struts2拦截器interceptor的三种配置方法
- Find them, Catch them poj 1703
- Linux touch 命令 - 为什么我们需要改变时间戳?
- 自定义easyui常用校验规则
- Quartz Cron表达式.docx
- nyoj-55懒省事的小明(优先队列)
- iOS开发之RAC(一)
- C语言基础------题目1
- [MySQL] 聚簇索引
- Java设计模式之——工厂模式