扩展并查集——POJ - 1182
题目含义
找出与之前的话不符的假话的数目
题目分析
简单的扩展并查集
题目代码
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=5e4+7;
int f[maxn*3];
int n,k,d,x,y;
int get(int x){
if(f[x]==x)return x;
else return f[x]=get(f[x]);
}
int main(){
scanf("%d%d",&n,&k);
int ans=0;
for(int i=1;i<=n;i++)
f[i]=i;
for(int i=0;i<k;i++){
scanf("%d%d%d",&d,&x,&y);
if(x>n||y>n){
ans++;
continue;
}
int fx_sel=get(x),fx_eat=get(x+n),fx_ene=get(x+n+n);
int fy_sel=get(y),fy_eat=get(y+n),fy_ene=get(y+n+n);
if(d==1){
if(fx_sel==fy_eat||fx_sel==fy_ene){
ans++;
continue;
}
f[fy_sel]=fx_sel;
f[fy_eat]=fx_eat;
f[fy_ene]=fx_ene;
}
else if(d==2){
if(fx_sel==fy_sel||fx_sel==fy_eat||x==y){
ans++;
continue;
}
f[fy_sel]=fx_eat;
f[fy_ene]=fx_sel;
f[fy_eat]=fx_ene;
}
}
printf("%d\n",ans);
return 0;
}
转载于//www.cnblogs.com/helman/p/11233921.html
还没有评论,来说两句吧...