POJ 1182 食物链
来源:互联网 发布:思途旅游cms源码 编辑:程序博客网 时间:2024/06/07 19:45
可以建立3*n的并查集,x,x+n,x+2*n分别代表一个食物圈。即若x和y属于一个类别的情况就是x和y连接,(x+n)和(y+n)连接,(x+2*n)和(y+2*n)连接;如果x吃y,那么x和(y+n)连接,(x+n)和(y+2*n)连接,(x+2*n)和y连接。依次连接即可
#include<iostream>#include<cstdio>#include<cstring>#include<ctime>#include<algorithm>#include<cstdlib>#include<cmath>#include<set>#include<bitset>#include<map>#include<stack>#include<queue>#include<vector>#include<utility>#define INF 0x3f3f3f3f#define inf 2*0x3f3f3f3f#define llinf 1000000000000000000#define pi acos(-1.0)#define mod 1000000007#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define lrt rt<<1#define rrt rt<<1|1#define rep(i,a,b) for(int i=(a);i<(b);i++)#define per(i,a,b) for(int i=(b)-1;i>=(a);i--)#define mem(a,b) memset(a,b,sizeof(a))#define lb(x) (x&-x)#define gi(x) scanf("%d",&x)#define gi2(x,y) scanf("%d%d",&x,&y)#define gll(x) scanf("%lld",&x)#define gll2(x,y) scanf("%lld%lld",&x,&y)#define gc(x) scanf("%c",&x)using namespace std;typedef long long ll;typedef unsigned long long ull;typedef pair<int,int>P;/***********************************************/int n,k,cnt=0,par[50005*3],ranks[50005*3],num,x,y;void init(int n){ rep(i,0,n)par[i]=i,ranks[i]=0;}int finds(int x){ if(par[x]==x)return x; return par[x]=finds(par[x]);}void unite(int x,int y){ x=finds(x),y=finds(y); if(x==y)return ; if(ranks[x]>ranks[y])par[y]=x; else par[x]=y; if(ranks[x]==ranks[y])ranks[y]++;}bool same(int x,int y){ return finds(x)==finds(y);}int main(){ cin>>n>>k; init(n*3); rep(i,0,k) { gi(num);gi2(x,y);x--,y--; if(x>=n||y>=n){cnt++;continue;} if(num==1) { if(same(x,y+n)||same(x,y+2*n))cnt++; else {unite(x,y),unite(x+n,y+n),unite(x+2*n,y+2*n);} } else { if(same(x,y)||same(x,y+2*n))cnt++; else {unite(x,y+n),unite(x+n,y+2*n),unite(x+2*n,y);} } } cout<<cnt; return 0;}
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 食物链
- 黑马程序员 十五、内省:JavaBean、 Beanutils、注解、泛型
- 7 天打造前端性能监控系统
- 主成分分析(PCA)原理详解
- Discuz!开发之将论坛首页设置为某个版块页面的方法
- HDU 1034 Candy Sharing Game
- POJ 1182 食物链
- hdu2925 约瑟夫环
- 黑马程序员 十六、 类加载器、代理
- org.hibernate.MappingException: Could not determine type for:
- leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405241b0 that was originally ad
- Backpack 解题报告 背包问题深入浅出
- 汇编作业
- 迟
- phpstorm 出现多个光标(phpstorm’即点即输’)功能禁用