POJ 1703 Find them, Catch them 种类并查集(入门)
来源:互联网 发布:js string 转 double 编辑:程序博客网 时间:2024/05/18 03:56
/*** 种类并查集入门题:* 这题开始做的时候总是MLE 因为在get_together(x, y)之前没有判断find(x) != find(y)* 因为,如果find(x) == find(y) 的时候,x,y在同个集合中,那么在合并的时候,根节点的fa = y;* 而y的fa又等于根节点,就会变成没有根节点。当下次在调用find(y)的时候就会不停的递归,导致MLE.* 还有,就是看discuss上说,如果find()函数是用迭代而非递归写的话会TLE。而用递归则MLE* 总之在合并之前要先判断find(x) != find(y) 才可以继续合并。*/#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <algorithm>#define INF 0x7fffffff#define MAXS 100005#define LL long longusing namespace std;struct Person { int re, fa;} guys[MAXS];void init(int n) { for(int i = 1; i <= n; i ++) { guys[i].fa = i; guys[i].re = 1; }}int find(int x) { int fa = guys[x].fa; if(fa == x) return fa; else { guys[x].fa = find(fa); guys[fa].re == guys[x].re ? guys[x].re = 1 : guys[x].re = 0; return guys[x].fa; }}bool judge(int x, int y) { if(guys[x].re == guys[y].re) return true; else return false;}void get_together(int x, int y) { int xfa = find(x); guys[xfa].fa = y; if(guys[x].re == 1) guys[xfa].re = 0; else guys[xfa].re = 1;}int main(){ int t, N, M, a, b; char cmd; scanf("%d", &t); while(t --) { scanf("%d%d", &N, &M); init(N); for(int i = 1; i <= M; i ++) { scanf("\n%c", &cmd); scanf("%d%d", &a, &b); if(cmd == 'A') { if(find(a) != find(b)) { printf("Not sure yet.\n"); } else { if(judge(a, b)) printf("In the same gang.\n"); else printf("In different gangs.\n"); } } else if(cmd == 'D') { if(find(a) != find(b)) get_together(a, b); } } } 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(种类并查集)
- Find them, Catch them - POJ 1703 种类并查集
- POJ 1703 Find them, Catch them 种类并查集
- POJ 1703 - Find them, Catch them(种类并查集)
- POJ 1703 Find them, Catch them 种类并查集
- POJ 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 (种类并查集)
- 你不知道的Eclipse用法:使用findBugs预先检测错误
- unix系统接口(一)
- 绿色软件下么——简单两步实现win 7上网自动拨号
- Java 深入必备
- 黑马程序员_全面接触Java集合框架(一)
- POJ 1703 Find them, Catch them 种类并查集(入门)
- Java_类加载器ClassLoader
- 自动添加注释—VS2010宏的使用
- WaitForSingleObject的用法
- POJ-1029-False coin
- 精通CSS+DIV基础总结(一)
- 根据iframe中内容设置iframe显示的高度
- VC++中ATL\CLR\MFC\Win32区别是什么
- 中断处理函数中不用disable_irq而用disable_irq_nosync原因