# poj 1182 食物链(种类并查集)

http://poj.org/problem?id=1182

 cqlf 1182 Accepted 1080K 266MS G++ 966B 2012-03-21 23:57:17
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 50005
int N;
int pre[MAXN],rela[MAXN];//pre:parent of i,rela:relation between i and i's parent
void UFset()
{
int i;
for(i=1;i<=N;i++)
{
pre[i]=i;
rela[i]=0;
}
}
int find(int x)
{
int s;
if(x==pre[x]) return x;
s=find(pre[x]);
rela[x]=(rela[x]+rela[pre[x]])%3;
return pre[x]=s;
}
int Union(int R1,int R2,int D)
{
int r1=find(R1);
int r2=find(R2);
if(r1==r2)// same type
{
if((rela[R2]-rela[R1]+3)%3!=D)//judge the result equal type or not
return 1;
else
return 0;
}
else
{
pre[r2]=r1;
rela[r2]=(rela[R1]-rela[R2]+D+3)%3;
return 0;
}
}
int main()
{
int K;
int D,X,Y;
int i;
int num=0;
scanf("%d%d",&N,&K);
UFset();
for(i=1;i<=K;i++)
{
scanf("%d%d%d",&D,&X,&Y);
if(X>N||Y>N||(X==Y&&D==2)) {num++;continue;}
if(Union(X,Y,D-1)) num++;
}
printf("%d\n",num);
return 0;
}

rela[x]=(rela[x]+rela[pre[x]])%3;

(rela[R1]-rela[R2]+3)%3!=D

pre[r1]=r2;

rela[r1]=(rela[R2]-rela[R1]+D+3)%3;

/*
Problem ID:G - 食物链
meaning:
Analyzing:种类并查集，
*/
#include <iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>

using namespace std;
typedef struct even{int pi,di;}even;

#define FOR(i,s,t) for(int i=(s); i<(t); i++)
#define LL long long

#define maxn 50005
int pre[maxn],rela[maxn];
int N,K,D;
int num;
void init(){
for(int i=0;i<maxn;i++){
pre[i]=-1;
rela[i]=0;
}
}
int find(int x){
int s=x;
if(pre[x]<0) return x;
s=find(pre[x]);
rela[x]=(rela[x]+rela[pre[x]])%3;
return pre[x]=s;
}
void Union(int R1,int R2){
int r1=find(R1);
int r2=find(R2);
if(r1==r2) {
if((rela[R1]-rela[R2]+3)%3!=D) num++;
}
else {
pre[r1]=r2;
rela[r1]=(rela[R2]-rela[R1]+D+3)%3;
}
}
int main(){
int X,Y;
scanf("%d%d",&N,&K);
num=0;
init();
while(K--){
scanf("%d%d%d",&D,&X,&Y);
if(X>N||Y>N||(X==Y&&D==2)) {num++;continue;}
if(D==1&&X==Y) continue;
D--;
Union(X,Y);
}
printf("%d\n",num);
return 0;
}


09-24 7720

12-19 9751

03-04 2.2万

05-13 1230

02-09 2882

01-21 1.5万

01-31 739

03-05 5171

08-09 1521

04-05 1596