There are N people living in a certain city , any two acquaintances are either friends or enemies, and satisfy:
1. My friend's friend is my friend;
2. The enemy of my enemy is my friend;
All people who are friends form a gang. Tell you M pieces of information about these N people , that is, certain two people are friends, or certain two people are enemies, please write a program to calculate how many gangs there may be in this city at most?
enter
The first line contains an integer N , the second line contains an integer M , 1<N<=1000,1<=M<=5000 ;
The next M lines describe M pieces of information, and the content is one of the following: " 0 XY " means that X and Y are friends; " 1 XY " means that X and Y are enemies.
output
Contains an integer, the maximum possible number of gangs.
sample input
6 4
1 1 4
0 3 5
0 4 6
1 1 2
sample output
3
#include <cstdio>
#include <iostream>
using namespace std;
int fa[1001], vis[1001];
int n, m, s, i, x, y, z, r1, r2;
int find(int x)
{
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
int main()
{
cin >> n >> m;
for (i = 1; i <= n; i++)
fa[i] = i;
for (i = 1; i <= m; i++)
{
cin >> z >> x >> y;
if (z == 0)
{
r1 = find(x);
r2 = find(y);
fa[r2] = r1;
}
if (z == 1)
{
if (vis[x])
{
r1 = find(y);
r2 = find(vis[x]);
fa[r2] = r1;
}
else
vis[x] = y;
if (vis[y])
{
r1 = find(x);
r2 = find(vis[y]);
fa[r2] = r1;
}
else
vis[y] = x;
}
}
for (i = 1; i <= n; i++)
if (fa[i] == i)
s++;
cout << s;
return 0;
}