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 | Case 1: 2 Case 2: 3 |
Note
Dataset is huge, use faster I/O methods.
题意:
有n场战争,每个战争的双方分别属于种族1和种族2。问你种族1和种族2之间最多的人数。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 20005;
vector<int>G[N];
int na, nb;//双方阵营人数
bool vis[N];//表示是否已经染过色
void dfs(int u, bool flag)//u染色成flag
{
vis[u] = 1;
if(flag == 0) na++;
else nb++;
for(int i = 0; i<G[u].size(); i++)
{
int v = G[u][i];
if(!vis[v])
{
dfs(v, !flag);
}
}
}
int main()
{
int n, t, i, cas = 0;
cin>>t;
while(t--)
{
scanf("%d", &n);
for(i = 1; i<=N; i++)//图可能不连通
{
G[i].clear();
vis[i] = 0;
}
int a, b, max1 = 0;
while(n--)
{
scanf("%d%d", &a, &b);
max1 = max(max(a, b), max1);
G[a].push_back(b);
G[b].push_back(a);
}
int ans = 0;
for(i = 1; i<=max1; i++)
{
if(G[i].size() && !vis[i])
{
na = 0, nb = 0;
dfs(i, 0);
ans += max(na, nb);
}
}
printf("Case %d: %d\n", ++cas, ans);
}
return 0;
}