POJ 1182 食物链(带权并查集)
来源:互联网 发布:php读取excel数据 编辑:程序博客网 时间:2024/05/16 07:24
题目链接:POJ 1182 食物链
没学带权并查集时候做过一次这个题,没做出来,然后看了题解被那些看起来很巧妙的通项公式吓到了,放弃了。现在再做这个题,发现这个题是可以不用那些通项公式的,完全可以把几种情况列出来做。
其实把一些情况列出来之后连蒙带猜也是可以得出来通项公式的,并没有我开始想的那么复杂。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int MAX_N = 50000 + 100;int n, k, p[MAX_N], _rank[MAX_N];int _find(int x){ if(p[x] != x) { int root = _find(p[x]); _rank[x] = (_rank[x] + _rank[p[x]]) % 3; return p[x] = root; } return x;}int main(){ scanf("%d%d", &n, &k); for(int i = 1; i <= n; i++) p[i] = i; int o, a, b, u, v, res = 0; for(int i = 0; i < k; i++) { scanf("%d%d%d", &o, &a, &b); if((o == 2) && (a == b) || a > n || b > n) { res++; continue; } u = _find(a); v = _find(b); if(u == v) { /*if(o == 1) if(!(_rank[a] == _rank[b])) res++; if(o == 2) if(!((_rank[b] == 1 && _rank[a] == 2) || (_rank[b] == 2 && _rank[a] == 0) || (_rank[b] == 0 && _rank[a] == 1))) res++;*/ if((_rank[a] - _rank[b] + 3) % 3 != o - 1) res++; continue; } p[u] = v; _rank[u] = (_rank[b] - _rank[a] + o - 1 + 3) % 3; } printf("%d\n", res); return 0;}
算是今天才彻底搞懂吧。看了http://blog.csdn.net/niushuai666/article/details/6981689 15/11/17
/**relation[x] 表示rootx->x的值 注意不要搞反x->y=1 x吃yx->y=2 y吃xx->y=0 x与y是同类根据向量得到所求relation的值*/#include <iostream>#include <cstdio>#include <cstring>const int MAX_N = 50000 + 100;using namespace std;struct Ani{ int pa, re;};Ani anis[MAX_N];int _find(int x){ if(x == anis[x].pa) return x; int tempf = anis[x].pa; anis[x].pa = _find(anis[x].pa); anis[x].re = (anis[tempf].re + anis[x].re) % 3; return anis[x].pa;}int main(){ int n, k, sum = 0; scanf("%d%d", &n, &k); for(int i = 1; i <= n; i++) { anis[i].pa = i; anis[i].re = 0; } int d, x, y; for(int i = 0; i < k; i++) { scanf("%d%d%d", &d, &x, &y); if(x > n || y > n) { sum++; continue; } if(d == 1) { int xf = _find(x); int yf = _find(y); if(xf != yf) { anis[xf].pa = yf; anis[xf].re = (anis[y].re + 3 - anis[x].re) % 3; } else { int now = (3 - anis[x].re + anis[y].re) % 3; if(now != d - 1) sum++; } } else { int xf = _find(x); int yf = _find(y); if(xf != yf) { anis[xf].pa = yf; anis[xf].re = (anis[y].re + 3 - (d - 1) + 3 - anis[x].re) % 3; } else { int now = (3 - anis[x].re + anis[y].re) % 3; if(now != d - 1) sum++; } } } printf("%d\n", sum); return 0;}
0 0
- POJ 1182 食物链(带权并查集)
- poj 1182 食物链(带权并查集)
- poj 1182 食物链(带权并查集)
- POJ-1182 食物链 (带权并查集)
- POJ 1182 食物链(带权并查集)
- poj 1182 食物链 //带权并查集
- POJ 1182 食物链 (带权并查集)
- poj 1182 食物链 带权并查集
- poj 1182 食物链 【带权并查集】
- poj 1182 食物链(带权并查集)
- poj 1182 食物链(带权并查集)
- POJ 1182 食物链(带权并查集)
- POJ 1182 食物链 带权并查集
- POJ 1182 食物链 带权并查集
- poj 1182食物链(带权并查集)
- poj 1182 食物链 带权并查集
- POJ 1182 食物链(带权并查集)
- Poj 1182 食物链【带权并查集】
- Hard to Play
- jqGrid编辑—单元格编辑【附视频、ppt、源码】
- Java Beans
- 数据结构-栈和队列
- viewgroup scrollTo、scrollBy、getScrollX、getScrollY这4个方法的含义
- POJ 1182 食物链(带权并查集)
- 问题1see WinSDKVer.h,问题2stdafx.h(37): error C4430,问题3LNK1181
- DDoS攻击工具——TFN2K 分析
- Robot
- Android开源客户端之LookAround学习(一)Application & 网络框架
- 阿里面试经历与感受谈
- PB打印标签DLL,支持128码
- 数据结构与算法分析学习笔记---第三章(链表)
- 如何令你的网站“动感十足”