1182 食物链
来源:互联网 发布:自动发弹幕软件 编辑:程序博客网 时间:2024/05/18 12:02
今天又把这个经典的带权并查集拿出来看看……….
唉…..其中一个括号打错了,浪费3个小时。
感觉并查集,就是确定一个根,然后看点与根之前的关系。普通并查集就是连起来就好了,只有有关系和没关系之分。但是带权并查集的话,就是还分有什么关系。
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>using namespace std;const int maxn = 5e4+10;int dis[maxn];int sum[maxn];int findf(int k){ if(k==dis[k]) return k; int t=dis[k]; dis[k]=findf(dis[k]); sum[k]=(sum[t]+sum[k])%3; return dis[k];}int ans=0;int main(){ int n,k; scanf("%d %d",&n,&k); int c,a,b; for(int i=0;i<=n;i++) dis[i]=i; memset(sum,0,sizeof(sum)); for(int i=0;i<k;i++){ scanf("%d %d %d",&c,&a,&b); if(a>n||b>n) ans++; else if(c==2&&a==b) {ans++;} else { if(c==1) { int root1=findf(a); int root2=findf(b); if(root2==root1) { if(sum[a]!=sum[b]) ans++; } else { if(root1<root2) { dis[root2]=root1; sum[root2]=(sum[a]-sum[b]+3)%3; } else { dis[root1]=root2; sum[root1]=(sum[b]-sum[a]+3)%3; } } } else if(c==2) { int root1=findf(a); int root2=findf(b); if(root1==root2) { if((sum[a]+(3-sum[b])%3)%3!=1) ans++;//就是这里的某个括号。 } else { if(root1<root2) { dis[root2]=root1; sum[root2]=(sum[a]-(1+sum[b])+3)%3; } else { dis[root1]=root2; sum[root1]=(1+sum[b]-sum[a]+3)%3; } } } } } printf("%d\n",ans); return 0;}
还可以用那个精神分裂法来做啊【手动滑稽}
阅读全文
0 0
- 1182 食物链
- 1182 食物链
- 1182 食物链
- 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 食物链
- Python2 包的安装
- 用python做数据分析4|pandas库介绍之DataFrame基本操作
- 扩展函数重写,返回多参函数,infix函数
- SlidingMenu的基本讲解,基础侧滑
- android 软键盘回车键捕获
- 1182 食物链
- git查看/新建/删除远程分支
- explicit关键字
- linux FTP安装与配置
- 九度1031:xxx定律
- Tcp/IP协议基础
- 两数组的交 II
- 九度1033:继续xxx定律
- MySQL-sql的逻辑查询处理