POJ1182 食物链
来源:互联网 发布:用python编写田字格 编辑:程序博客网 时间:2024/05/17 07:46
http://poj.org/problem?id=1182
并查集。
#include <cstdio>#include <cstring>#include <cmath>#include <map>#include <set>#include <vector>#include <iostream>#include <algorithm>using namespace std;#define N 50005int n,k,p[N],t[N];void init(){ int i; for (i=1;i<=n;i++) { p[i]=i; t[i]=0; }}int root(int s){ if (p[s]==s) return s; int st=p[s]; p[s]=root(p[s]); t[s]=(t[s]+t[st])%3; return p[s];}void un(int x,int y,int z){ int rx,ry; rx=root(x); ry=root(y); p[ry]=rx; t[ry]=(t[x]-z-t[y]+6)%3;}int main(){ freopen("a","r",stdin); int i; scanf("%d%d",&n,&k); init(); int sum=0,x,y,z; for (i=1;i<=k;i++) { scanf("%d%d%d",&z,&x,&y); if (x>n || y>n) sum++; else if (x==y && z==2) sum++; else { int rx,ry; rx=root(x); ry=root(y); if (rx==ry) { if (z==1 && t[x]!=t[y]) sum++; if (z==2 && (t[x]+3-t[y])%3!=1) sum++; } else un(x,y,z-1); } } cout<<sum<<endl; return 0;}
- POJ1182 食物链
- POJ1182食物链
- poj1182 食物链
- POJ1182--食物链
- poj1182-食物链
- poj1182 食物链
- poj1182 食物链
- POJ1182食物链
- poj1182 食物链
- POJ1182食物链
- POJ1182食物链
- POJ1182 食物链
- POJ1182:食物链
- poj1182食物链
- POJ1182 食物链
- poj1182 食物链
- POJ1182--食物链
- POJ1182 食物链
- Unity3D 代码优化
- VC6.0 files---open 出现 MSDEV.exe错误,Project ---- add to project 出现错误等等~~ .
- C语言 ---- typedef
- Unity3D动画制作之二(javascript)
- 使用PE文件格式 HOOK 其他进程API
- POJ1182 食物链
- usaco Mother's Milk
- 徒弟离职引发的思考
- Objective C对象之间信息传递(二)block
- 所谓的“腾讯抄袭”
- 常用算法思想
- 发一个displayTag的新样式
- WordPress模板修改相关信息
- OSGI 中嵌入 Http 服务器的运行环境