The Vampires and Lykans are fighting each other to death. The war has become so fierce that, none knows who will win. The humans want to know who will survive finally. But humans are afraid of going to the battlefield.
So, they made a plan. They collected the information from the newspapers of Vampires and Lykans. They found the information about all the dual fights. Dual fight means a fight between a Lykan and a Vampire. They know the name of the dual fighters, but don’t know which one of them is a Vampire or a Lykan.
So, the humans listed all the rivals. They want to find the maximum possible number of Vampires or Lykans.
Input
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case contains an integer n (1 ≤ n ≤ 105), denoting the number of dual fights. Each of the next n lines will contain two different integers u v (1 ≤ u, v ≤ 20000) denoting there was a fight between u and v. No rival will be reported more than once.
Output
For each case, print the case number and the maximum possible members of any race.
Sample Input
Output for Sample Input
2
2
1 2
2 3
3
1 2
2 3
4 2
挺水的题只要找出来每一个联通块的大集合就可以了…
听说可以用什么卵种类并查集…
我再学学
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<memory.h>
#include<vector>
#include<map>
#include<queue>
using namespace std;
vector<int> tu[100001];
bool zouguo[100001];
map<int, int>qw;
int yingshe[100001];
queue<int> dui;
queue<int> se;
int main()
{
int T;
cin >> T;
int u = 0;
while (T--)
{
memset(zouguo, 0, sizeof(zouguo));
memset(yingshe, 0, sizeof(yingshe));
qw.clear();
for (int a = 1;a <= 100000;a++)tu[a].clear();
int n;
cin >> n;
int q, w;
for (int a = 1;a <= n;a++)
{
scanf("%d%d", &q, &w);
tu[q].push_back(w);
tu[w].push_back(q);
if (!qw[q])
{
qw[q] = qw.size();
yingshe[qw.size()] = q;
}
if (!qw[w])
{
qw[w] = qw.size();
yingshe[qw.size()] = w;
}
}
int sum = 0;
int yi = 0, er = 0;
for (int a = 1;a <= qw.size();a++)
{
if (zouguo[yingshe[a]])continue;
zouguo[yingshe[a]] = 1;
dui.push(yingshe[a]);
se.push(1);
while (!dui.empty())
{
int ss = se.front();
int tou = dui.front();
dui.pop();
se.pop();
if (ss % 2)yi++;
else er++;
for (int b = 0;b<tu[tou].size();b++)
{
if (zouguo[tu[tou][b]])continue;
zouguo[tu[tou][b]] = 1;
dui.push(tu[tou][b]);
se.push(ss + 1);
}
}
if (yi>er)sum += yi;
else sum += er;
yi = 0, er = 0;
}
printf("Case %d: %d\n", ++u, sum);
}
return 0;
}