Find them, Catch them POJ
来源:互联网 发布:淘宝我的五星好评截图 编辑:程序博客网 时间:2024/06/06 21:50
点击打开链接
维护两个并查集 f1与f2
题目中所给两点间的关系都是对立的 而一共只有两个阵营 因此可以得出 a敌对b且b敌对c 则a与c为同伙
对于两点间关系的询问 首先要看两点之间的敌友关系是否已经确定 如果以敌对关系为边建立子图 则图中相邻两点为敌 间隔一点为友 以此类推 因此只要两点连通即可确定关系 可通过并查集f1实现
其次要看是敌还是友 可通过标记数组实现 比如新增a敌对b 如果之前还有a敌对c那就把b与c放入友好并查集f2 敌友关系即可明了
#include <stdio.h>#include <cstring>#include <algorithm>using namespace std;struct node{ int id; int sta;};node book[100010];int f1[100010],f2[100010];int n,m;void unite(int u,int v,int* f);int getf(int p,int* f);int main(){ int t,i,j,u,v,fu,fv; char ch[2]; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); memset(book,0,sizeof(book)); for(i=1;i<=n;i++) { f1[i]=i; f2[i]=i; } for(i=1;i<=m;i++) { scanf("%s%d%d",ch,&u,&v); if(ch[0]=='D') { unite(u,v,f1); if(book[u].sta==1) { unite(book[u].id,v,f2); } else { book[u].id=v; book[u].sta=1; } if(book[v].sta==1) { unite(book[v].id,u,f2); } else { book[v].id=u; book[v].sta=1; } } else { fu=getf(u,f1); fv=getf(v,f1); if(fu!=fv) { printf("Not sure yet.\n"); } else { fu=getf(u,f2); fv=getf(v,f2); if(fu!=fv) { printf("In different gangs.\n"); } else { printf("In the same gang.\n"); } } } } } return 0;}void unite(int u,int v,int* f){ int fu,fv; fu=getf(u,f); fv=getf(v,f); if(fu!=fv) { f[fv]=fu; } return;}int getf(int p,int* f){ if(f[p]==p) return p; else { f[p]=getf(f[p],f); return f[p]; }}
阅读全文
0 0
- POJ Find them ,catch them
- POJ Find them, Catch them
- Find them, Catch them POJ
- Find them, Catch them POJ
- Find them, Catch them POJ
- Find them, Catch them POJ
- Find them, Catch them POJ
- 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)
- Unity隐身 观察隐身
- 《Java与模式》学习笔记(连载中...)
- git撤销删除分支操作
- python基础一
- JavaScript 标准参考教程(alpha)
- Find them, Catch them POJ
- 关于双硬盘电脑掉盘的问题
- 基于共现提取人民的民义人物关系
- Cannot find module 'socket.io'
- 梯度下降的三种形式,BGD,SGD与MGD
- 判断是否是平衡二叉树
- iOS UITableView~datasource(数据源方法)
- Hive(下)
- Java中的static关键字解析