poj 1182-食物链解题报告
2012-05-09 22:22
453 查看
链接:http://poj.org/problem?id=1182
并查集的一道经典题目,主要是处理两个之间的关系的时候比较难想,也比较麻烦
View Code
并查集的一道经典题目,主要是处理两个之间的关系的时候比较难想,也比较麻烦
View Code
#include<stdio.h> #include<string.h> #define N 50005 struct node { int p; int r; }; node f ; int n; void init() { int i; for(i=1;i<=n;i++) { f[i].p=i; f[i].r=0; } } int find(int x) { if(f[x].p==x) return x; int temp=f[x].p; f[x].p=find(temp); f[x].r=(f[temp].r+f[x].r)%3; return f[x].p; } void uni(int x,int y,int sx,int sy,int d) { f[sy].p=sx; f[sy].r=(3+(d-1)+f[x].r-f[y].r)%3; } int main() { int d,a,b,m; int i,j,k; scanf("%d%d",&n,&m); init(); int num=0; while(m--) { scanf("%d%d%d",&d,&a,&b); if(a>n||b>n) { num++; continue; } int pa=find(a); int pb=find(b); if(pa!=pb) uni(a,b,pa,pb,d); else { if(d==1&&f[a].r!=f[b].r) num++; else if(d==2&&(f[b].r-f[a].r+3)%3!=1) num++; } } printf("%d\n",num); return 0; }
相关文章推荐
- POJ1182 食物链 ACM解题报告(并查集+路径压缩难题)
- POJ-1182-食物链 解题报告
- poj 1182 食物链(并查集)解题报告(转)
- 【原创】poj ----- 1182 食物链 解题报告
- poj 1182 食物链(并查集)解题报告(转)
- 【解题报告】 POJ 1182 食物链 并查集的经典应用+相对位置
- 【POJ1182】食物链,思路+数据+代码,可能是史上关于这道题最详细的解题报告
- poj 1182 食物链 解题报告
- POJ 1182 食物链 -- 解题报告
- POJ 1182 解题报告
- POJ 1182 并查集 解题报告
- poj1182解题报告
- poj解题报告——1182
- Poj 食物链 解题报告 (种类并查集)
- POJ1182 解题报告
- poj 3077 Rounders/bnuoj 3196 Rounders 解题报告
- POJ 1182 食物链
- POJ 1182 食物链
- POJ 1451 T9 [解题报告] Java
- POJ1001 Exponentiation (解题报告)