POJ1182 食物链 【并查集变种】
来源:互联网 发布:网络曝光是什么意思 编辑:程序博客网 时间:2024/05/29 18:34
挺简单的
N个元素扩展为 3*N个
i-A i-B i-C
A吃B吃C吃A
挑战程序设计的89面
#include <cstdio>#include <cstdlib>#include <iostream>#include <cstring>#include <cmath>using namespace std;int N,K;const int MAX_N=333333;//并查集int par[MAX_N];int rank[MAX_N];//树的高度void init(int n){for(int i=0;i<n;i++){par[i]=i;rank[i]=0;}}int find(int x)//{if(par[x]==x)return x;elsereturn par[x]=find(par[x]);}//合并x和y所属的集合void unite(int x,int y){x=find(x);y=find(y);if(x==y)return;if(rank[x]<rank[y])par[x]=y;else{par[y]=x;if(rank[x]==rank[y])rank[x]++;}}//判断x和y是否同属于一个集合bool same(int x, int y){return find(x)==find(y);}int main(){#ifndef ONLINE_JUDGEfreopen("G:/1.txt","r",stdin);freopen("G:/2.txt","w",stdout);#endif scanf("%d%d",&N,&K);init(3*N);int kind,a,b;int ans=0;for(int i=0;i<K;i++){scanf("%d%d%d",&kind,&a,&b);int x=a-1;int y=b-1;if(x<0||x>=N||y<0||y>=N){ans++;continue;}if(kind==1){if(same(x,y+N)||same(x,y+2*N)){ans++;continue;}unite(x,y);unite(x+N,y+N);unite(x+2*N,y+2*N);}else{if(same(x,y)||same(x+N,y+N)||same(x+2*N,y+2*N)||same(x,y+2*N)){ans++;continue;}unite(x,y+N);unite(x+N,y+2*N);unite(x+2*N,y);}}printf("%d\n",ans);return 0;}
0 0
- POJ1182 食物链 【并查集变种】
- 食物链 POJ1182 -- 并查集
- poj1182 食物链 (并查集)
- poj1182 并查集 食物链
- poj1182食物链 并查集
- 并查集-POJ1182食物链
- POJ1182 - 食物链 - 并查集
- 并查集 食物链 POJ1182
- poj1182 食物链(并查集)
- POJ1182 食物链 并查集
- Poj1182食物链 (并查集)
- POJ1182 食物链(并查集)
- 【并查集】:poj1182,食物链
- POJ1182 食物链(并查集)
- POJ1182 食物链 并查集
- poj1182 食物链[并查集]
- poj1182 食物链(并查集)
- POJ1182 食物链【并查集】
- SQLite加密的方法(c#)
- JSF消息FacesMessage的使用
- hadoop备记
- 关于 redis、memcache、mongoDB 的对比
- hdu2070
- POJ1182 食物链 【并查集变种】
- cocos2d-x 聊天输入框实现
- xampp下mysql设置密码以及解决中文乱码问题
- sift 图像拼接
- 浏览器缓存问题
- hau2081
- JS 数组去重实现,一道面试题
- 黑马程序员_银行业务调度系统
- 扯谈web安全之JSON