题目思路
一道挺裸的种类并查集的题
但还是wa了好多发
对于这题来说因为a,b,c之间关系属于一种环形关系
所以每次判断只需要判断属于某一种种类的情况就好了
据说只有一组数据 如果写多组输入的话会wa
ac代码
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#define pi 3.1415926535898
#define ll long long
#define lson rt<<1
#define rson rt<<1|1
#define eps 1e-6
#define ms(a,b) memset(a,b,sizeof(a))
#define legal(a,b) a&b
#define print1 printf("111\n")
using namespace std;
const int maxn = 1e6+5;
const int inf = 0x3f3f3f3f;
const ll llinf = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1000000007;
//998244353
int f[maxn],sum[maxn];
int n,m;
struct node
{
int x,y;
}e[maxn];
int findx(int x)
{
return f[x]==x?f[x]:f[x]=findx(f[x]);
}
void join(int x,int y)
{
int r1=findx(x);
int r2=findx(y);
if(r1!=r2)
{
f[r1]=r2;
}
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=3*n;i++)
f[i]=i;
int cot=0;
for(int i=1;i<=m;i++)
{
int t,x,y;
scanf("%d%d%d",&t,&x,&y);
if(x>n||y>n||(t==2&&x==y))
cot++;
else
{
if(t==1)
{
if(findx(x)==findx(y+n)||findx(x)==findx(y+n+n))//如果不是同类就+1
cot++;
else
{
join(x,y);
join(x+n,y+n);
join(x+n+n,y+n+n);
}
}
if(t==2)
{
if(findx(x)==findx(y+n+n)||findx(x)==findx(y))//如果不属于吃与被吃关系就+1
cot++;
else
{
join(x,y+n);
join(x+n,y+n+n);
join(x+n+n,y);
}
}
}
}
printf("%d\n",cot);
}