POJ 1182 食物链——种类并查集
来源:互联网 发布:自定义端口爆破 编辑:程序博客网 时间:2024/05/22 06:36
题目链接点击打开链接
此题不会做 在discuss里面学到了一招——用向量思维处理动物间的相对关系 的确很神!思考了一下 觉得应该是因为这道题因为关系最后成环才可以的吧。这题目感觉应该叫食物环。。。关系是环的,应该都可以这样做了。如果不是环,估计要用到拓扑排序了吧……
#include<cstdio>const int maxn = 50000+10;int p[maxn]; int r[maxn];void set(int n) { for(int x = 1; x <= n; x++) { p[x] = x; r[x] = 0; }}int find(int x){ if(x == p[x]) return x; int t = p[x]; p[x] = find(p[x]); r[x] = (r[x]+r[t])%3; return p[x];}void Union(int x, int y, int d){ int fx = find(x); int fy = find(y); p[fy] = fx; r[fy] = (r[x]-r[y]+3+(d-1))%3; }int main(){ int n, m; scanf("%d%d", &n, &m); set(n); int ans = 0; int d, x, y; while(m--) { scanf("%d%d%d", &d, &x, &y); if(x > n || y > n || (d == 2 && x == y)) ans++; else if(find(x) == find(y)) { if(d == 1 && r[x] != r[y]) ans++; if(d == 2 && (r[x]+1)%3 != r[y]) ans++; } else Union(x, y, d); } printf("%d\n", ans); 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 食物链 (种类并查集)
- 常见Java面试题
- 十三个经典算法研究与总结、目录+索引
- hdu 1018 Big Number_Stirling数求N!的位数
- 如何在Android Studio中引入Library Project
- CodingTrip - 携程编程大赛 (预赛第一场) :1002 括号匹配
- POJ 1182 食物链——种类并查集
- 设计模式学习--------4.外观模式学习
- 【OpenCV】图像遍历+像素压缩
- 双色球,今天你中奖了吗
- [java][spring]取得通过配置文件和注解注入的bean、取得当前数据库连接、取得ApplicationContext的方法
- SQL Server 2008连接字符串写法大全
- 005
- 百度地图 IOS sdk 错误总结
- Myeclipse打开web.xml很卡的问题