正解:并查集
解题报告:
这题,大概难点在分类讨论?(划
反正整个儿就并查集板子题了
说难点在分类讨论主要是因为,一般这种都是分成两组嘛,就很简单fa[x]=y fa[x+n]=y+n差不多这意思嘛
但是这个是分成三组,,,所以打起来就要注意一下理清条理,最好注释下什么东西是干什么的,这样打起来其实还是挺susi的(就像之前做这个一样(逃
没啦!代码放最后辣!over!
![](https://i-blog.csdnimg.cn/blog_migrate/a2b7024cb6a8cccd23244148c90016f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/aa7790439652fcf252898472b6dc57b7.gif)
#include<bits/stdc++.h> using namespace std; #define ll long long #define rp(i,x,y) for(register ll i=x;i<=y;++i) #define lowbit(x) x&(-x) const ll N=50000+100; ll n,k,fa[N*3],ans; inline ll read() { register char ch=getchar();register ll x=0;register bool y=1; while(ch!='-' && (ch>'9' || ch<'0'))ch=getchar(); if(ch=='-')ch=getchar(),y=0; while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^'0'),ch=getchar(); return y?x:-x; } inline ll fd(ll x){return fa[x]==x?fa[x]:fa[x]=fd(fa[x]);} int main() { n=read();k=read();rp(i,1,n*3)fa[i]=i; rp(i,1,k) { ll op=read(); if(op==1) { ll x=read(),y=read(); if(x>n || y>n){++ans;continue;} ll fa1=fd(x),fa2=fd(y),fa3=fd(x+n),fa4=fd(y+n),fa5=fd(x+n+n),fa6=fd(y+n+n); if(fa3==fa2 || fa5==fa2){++ans;continue;} fa[fa1]=fa2;fa[fa3]=fa4;fa[fa5]=fa6; continue; } ll x=read(),y=read(); if(x>n || y>n || x==y){++ans;continue;} ll fa1=fd(x),fa2=fd(y),fa3=fd(x+n),fa4=fd(y+n),fa5=fd(x+n+n),fa6=fd(y+n+n); if(fa1==fa2 || fa5==fa2){++ans;continue;} fa[fa3]=fa2;fa[fa6]=fa1;fa[fa5]=fa4; } printf("%lld\n",ans); return 0; } //fax:xÒ»Àà fax+n:x³Ôʲô fax+2n:x±»³Ô