poj 1182 食物链
来源:互联网 发布:国家题库软件 编辑:程序博客网 时间:2024/06/04 18:21
http://poj.org/problem?id=1182
分析:
rank[x]=(rank[x]+rank[t])%3;
rank[t1]=(rank[y]-rank[x]+d-1+3)%3;
参考:http://cavenkaka.iteye.com/blog/1489588
#include<iostream>#include<cstdio>using namespace std;const int NUM=50005;int father[NUM],rank[NUM];int Find(int x){if(x!=father[x]) {int t=father[x];father[x]=Find(father[x]);rank[x]=(rank[x]+rank[t])%3;//某结点和爷爷的关系由它和父亲的关系决定,每一层相差1(0:同类,1:吃父亲,2:被吃)}return father[x];}void Union(int x,int y,int d){int t1=Find(x),t2=Find(y);father[t1]=t2;rank[t1]=(rank[y]-rank[x]+d-1+3)%3;}int main(){int i,x,y,d,count,N,K;scanf("%d%d",&N,&K);for(i=0;i<=N;i++){father[i]=i;rank[i]=0;}count=0;while(K--){scanf("%d%d%d",&d,&x,&y);if(x>N||y>N||(d==2&&x==y)) {count++;continue;}int t1=Find(x),t2=Find(y);if(t1==t2){if((rank[x]-rank[y]+3)%3!=d-1) count++;//0->2,1->0,2->1}elseUnion(x,y,d);}printf("%d\n",count);return 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 食物链
- 自动补全(智能提示)原理与实现
- 内存数据库
- poj 2386 Lake Counting
- 利用Theme自定义Activity进入退出动画
- NSNotificationCenter用法总结
- poj 1182 食物链
- RTP协议分析
- C/C++获取当前系统时间
- 我是个程序猿
- TCP的定时器
- 我,一个非典型程序员
- android gps 定位
- Linux下使用goagent
- [使用资源] 简单获取drawable下规律性命名的图片资源