POJ 1182 食物链(种类并查集)
来源:互联网 发布:电气控制系统设计软件 编辑:程序博客网 时间:2024/06/04 08:28
题目地址:POJ 1182
一道很经典的种类并查集的题目。利用互相之间的关系来进行权值的维护。
代码如下:
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;int bin[60000], rank[60000];int find1(int x){ int y; if(bin[x]!=x) { y=bin[x]; bin[x]=find1(bin[x]); rank[x]=(rank[x]+rank[y])%3; } return bin[x];}int main(){ int n, k, p, x, y, i, flag, f1, f2, cnt=0; scanf("%d%d",&n,&k); for(i=1;i<=n;i++) { bin[i]=i; rank[i]=0; } while(k--) { flag=0; scanf("%d%d%d",&p,&x,&y); if(x>n||y>n||(p==2&&x==y)) { cnt++; continue ; } f1=find1(x); f2=find1(y); if(p==1) { if(f1==f2) { if(rank[x]!=rank[y]) cnt++; } else { bin[f2]=f1; rank[f2]=(rank[x]+3-rank[y])%3; } } else { if(f1==f2) { if(rank[x]!=(rank[y]+1)%3) cnt++; } else { bin[f2]=f1; rank[f2]=(rank[x]+2-rank[y])%3; } } } printf("%d\n",cnt); return 0;}
2 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 食物链 (种类并查集)
- 297 - Quadtrees
- UVA 1232 - SKYLINE
- 2028 Lowest Common Multiple Plus
- 【问底】李平:大型网站的灵魂——性能
- objective c设计模式--KVC
- POJ 1182 食物链(种类并查集)
- 【实验报告】《数据结构》实验一: VC编程工具的灵活使用
- POJ 2482——Stars in Your Window(线段树+扫描线,二维区域最值转化为线段树-经典)最浪漫的题目
- Linked List Cycle
- HDOJ-5050-Divided Land 解题报告
- 【DFS】HDU 1364 && POJ 1071 Illusive Chase
- LCA - RMQ & TRAJAN ZOJ 3195
- 关闭防火墙
- 黑马程序员——C语言结构体