PKU - 1182
有三种动物 A、B、C,A吃 B,B吃 C,C吃 A
给出 K条陈述,描述 1, X与 Y同类,2, X吃 Y
问其中有多少条假话,即与之前的真话矛盾
种类并查集经典题!基本是抄的题解 orz
维护一个
eval[x]
,表示 x连向其父亲的边的权值,对3取模
通过这个边的权值,我们可以知道
x
相对其父亲的关系
得到一条陈述后,我们先进行判断,对
如果
fx≠fy
,说明
x
和
否则判断
eval[x]==eval[y]+d
,如果不等,则矛盾,为假话。
这里可以看成一个向量加法
接下来如果这句话是真话的,就要合并
x
,
计算
eval[fx]=eval[y]+d−eval[x]
然后在路径压缩时,记录
x
原先的父亲
更新
这样根据向量关系建立并查集即可
#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <queue>
using namespace std;
typedef pair<int,int> Pii;
typedef long long LL;
typedef unsigned long long ULL;
typedef double DBL;
typedef long double LDBL;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define Sqr(a) ((a)*(a))
const int maxn=5e4+10;
struct Union
{
int n;
int unin[maxn], eval[maxn];
void init(int tn){n=tn; for(int i=1; i<=n; i++){unin[i]=i;eval[i]=0;}}
int find(int x)
{
if(unin[x]==x) return x;
int ox=unin[x];
unin[x]=find(unin[x]);
eval[x]=(eval[x]+eval[ox])%3;
return unin[x];
}
void join(int d,int x,int y)
{
int fx=find(x), fy=find(y);
if(fx==fy) return;
unin[fx]=fy;
eval[fx]=(eval[y]+d-eval[x])%3;
eval[fx]=(eval[fx]+3)%3;
}
int judge(int d,int x,int y)
{
if(((d==1)&&(x==y)) || x>n || y>n) return 0;
int fx=find(x), fy=find(y);
if( fx != fy) return 1;
else
{
if(eval[x]==(d+eval[y])%3) return 1;
else return 0;
}
}
};
int N,K;
Union U;
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
scanf("%d%d", &N, &K);
int ans=0;
U.init(N);
for(int i=1; i<=K; i++)
{
int d,x,y;
scanf("%d%d%d", &d, &x, &y);
d--;
if(U.judge(d,x,y)) U.join(d,x,y);
else ans++;
}
printf("%d\n", ans);
return 0;
}