题目描述
Input & Output
Sample
Code
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
int Find(int x, vector<int> &father)
{
int t = x;
while (father[t] != t)
t = father[t];
while (x != t)
{
int temp = father[x];
father[x] = t;
x = temp;
}
return t;
}
void Merge(int a, int b, vector<int> &father)
{
int fa = Find(a, father);
int fb = Find(b, father);
if (fa != fb)
father[fa] = fb;
}
int main()
{
unsigned sum = 0U;
int num_animal, num_bond;
scanf("%d %d", &num_animal, &num_bond);
vector<int> father(3 * (num_animal + 1), -1);
for (int i = 1; i <= 3 * num_animal; i++)
father[i] = i;
while (num_bond--)
{
int type, a, b;
scanf("%d %d %d", &type, &a, &b);
if (a > num_animal || b > num_animal || a < 1 || b < 1)
{
sum++;
continue;
}
if (type == 2 && a == b)
{
sum++;
continue;
}
if (type == 1)
{
if (Find(a, father) == Find(b + num_animal, father) || Find(b, father) == Find(a + num_animal, father))
{
sum++;
continue;
}
Merge(a, b, father);
Merge(a + num_animal, b + num_animal, father);
Merge(a + 2 * num_animal, b + 2 * num_animal, father);
}
else if (type == 2)
{
if (Find(a, father) == Find(b, father) || Find(b, father) == Find(a + num_animal, father))
{
sum++;
continue;
}
Merge(a, b + num_animal, father);
Merge(a + num_animal, b + 2 * num_animal, father);
Merge(a + 2 * num_animal, b, father);
}
}
cout << sum << endl;
return 0;
}