POJ 1182 食物链
来源:互联网 发布:php页面传递数组 编辑:程序博客网 时间:2024/06/03 19:25
带权并查集。
出现的正确关系就加入集合。
用 f[A]=B ;如果 r[A]=0;表示同类,r[A]=2表示天敌,r[A]=1表示食物。
不知道POJ 怎么了。
写上 while(~scanf("%d%d",&n,&m) 这个循环就WA。
还得我还看别人题解,然后改来改去。最后实在不行了,重写的时候没有加居然AC了。
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int f[50001],r[50001];int n,m;int fa(int x){ if(f[x]!=x) { int tmp=fa(f[x]); r[x]=(r[x]+r[f[x]])%3; f[x]=tmp; } return f[x];}int main(){ scanf("%d%d",&n,&m); int a,b,c; int ans=0; for(int i=1; i<=n; i++) f[i]=i,r[i]=0; while(m--) { int x,y; scanf("%d%d%d",&c,&a,&b); if(a>n||b>n||(a==b&&c==2)) { ans++; continue; } x=fa(a),y=fa(b); if(x==y) { if((r[b]-r[a]+3)%3!=c-1) ans++; } else { f[y]=x; r[y]=(r[a]-r[b]+c+2)%3; } } printf("%d\n",ans);}
0 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 食物链
- QT笔记:数据库总结(三)之SQL模型类-QSqlTableModel模型
- hadoop ipc原理简介
- Java - 对象(object) 详解
- winxp退市是微软windows操作系统的滑铁卢
- 杭电 1016 Prime Ring Problem
- POJ 1182 食物链
- JAVA容器(七)---HashMap的实现原理
- 匈牙利算法
- c++ const知多少
- DNS使用的是TCP协议还是UDP协议
- Struts2+Spring+Hibernate step by step 04 整合Spring之二,从数据库验证用户名和密码
- 谈下一代企业和下一代技术
- ZOJ题目分类
- String类其他操作方法