poj_1182_食物链(并查集)
来源:互联网 发布:华硕truelife软件 编辑:程序博客网 时间:2024/05/07 05:50
题型:并查集
题意:中文题,不解释~
分析:
典型的并查集应用。
rank中存的秩为0、1、2,分别表示0吃1,1吃2,2吃0。
将同种动物归于一个集合,查找判断是否为假话。
代码:
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#define MAXN 50050using namespace std;int father[MAXN];//father[x]表示x的父节点int rank[MAXN];//rank[x]表示x的秩void Make_Set(int x) { //初始化 father[x] = x; rank[x] = 0;}int Find_Set(int x) { int tmp = father[x]; if(x != father[x]) { father[x] = Find_Set(father[x]); rank[x] = (rank[x] + rank[tmp]) % 3; } return father[x];}void Union(int x,int y,int w) { int a = Find_Set(x); int b = Find_Set(y); father[b] = a; rank[b] = (rank[x] - rank[y] + w + 3) % 3;}int main() { int n,m,num,x,y; scanf("%d%d",&n,&m); for(int i=1; i<=n; i++) { Make_Set(i); } int ans = 0; while(m--) { scanf("%d%d%d",&num,&x,&y); if(x>n || y>n) { ans++; } else { if(num == 1) { if(Find_Set(x) == Find_Set(y) && rank[x] != rank[y]) ans++; else Union(x,y,0); } else { if(Find_Set(x) == Find_Set(y) && (rank[x]+1)%3 != rank[y]) ans++; else Union(x,y,1); } } } printf("%d\n",ans); return 0;}
- poj_1182_食物链(并查集)
- poj_1182_并查集
- POJ_1182_食物链
- POJ_1182_食物链
- 并查集--食物链
- 【并查集】食物链
- 【并查集】食物链
- 【并查集】食物链
- 【并查集】食物链
- 食物链-并查集
- 食物链 并查集
- 食物链 并查集
- 食物链 【并查集】
- 【并查集】食物链
- 【并查集】食物链
- 食物链 -- 并查集
- 食物链 POJ1182 -- 并查集
- poj_1182 食物链、并查集
- 用Virtualbox搭建LAMP环境-2.安装Apache server
- LDAP学习笔记<三>深入管理openLDAP
- Shaping Regions
- 通过逻辑地址得到物理地址
- 获取手机号码,和IMEI 获取本地iphone手机号码
- poj_1182_食物链(并查集)
- C++ 学习网站
- Android 4学习(8):用户界面 - Fragment
- property 等
- java开发环境搭建
- [MySQL] 存储过程、函数、触发器和视图的权限检查
- 数据挖掘方面的一个算法——FP-Tree
- ERD Commander 2005搞定win7 系统平移换硬盘
- vs2010中在输出窗口COutputWnd中输出文字和自动滚动