Back to Underworld

E - Back to Underworld

Crawling in process... Crawling failed Time Limit:4000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

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

2

2

1 2

2 3

3

1 2

2 3

4 2

Sample Output

Case 1: 2

Case 2: 3

 

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
/*
这道题目只要记住他的意思是说,数字的编号是唯一的
1 2
2 3
这里的二必定是一个队的,不是两个队的,没有理解题目的读者请仔细品味题目意思
其他的就简单了,运用一点二分图的知识就可以解决问题了,
*/
using namespace std;
const int maxn=20000+5;
vector<int >G[maxn];
bool vis[maxn];
bool exist[maxn];
int par[maxn];
int xse[maxn],sum1,sum2,ans;
int T,u,v,_max,n;
void init() {
    memset(vis,false,sizeof(vis));
    memset(xse,0,sizeof(xse));
    memset(exist,false,sizeof(exist));
    ans=_max=0;
    for(int i=0; i<maxn; i++) {
        par[i]=i;
        G[i].clear();
    }
}
int find(int x) {
    return par[x]==x?x:par[x]=find(par[x]);
}
void unite(int x,int y) {
    x=find(x);
    y=find(y);
    par[x]=y;
}
void DFS(int u) {
    for(int i=0; i<G[u].size(); i++) {
        if(vis[G[u][i]])continue;
        vis[G[u][i]]=true;
        xse[G[u][i]]=!xse[u];
        DFS(G[u][i]);
    }
}
void DFS_TWO(int u) {
    if(xse[u]) {
        sum1++;
    } else {
        sum2++;
    }
    for(int i=0; i<G[u].size(); i++) {
        if(vis[G[u][i]]) {
            vis[G[u][i]]=false;
            DFS_TWO(G[u][i]);
        }
    }
}
int main() {
    scanf("%d",&T);
    for(int cases=1; cases<=T; cases++) {
        scanf("%d",&n);
        init();
        for(int i=0; i<n; i++) {
            scanf("%d%d",&u,&v);
            G[u].push_back(v);
            G[v].push_back(u);
            exist[u]=exist[v]=true;
            _max=max(_max,u);
            _max=max(_max,v);
            unite(u,v);
           // printf("<%d,%d>\n",u,v);
        }
        for(int u=1;u<=_max;u++){
            //printf("[%d,%d,%d]\n",exist[u],par[u],u);
            if(exist[u]&&par[u]==u){
                sum1=sum2=0;
                vis[u]=true;DFS(u);//将在一条连通图上的点进行标记
                vis[u]=false;DFS_TWO(u);//将这些点进行分类,分为A,B两组
               // printf("[%d,%d]\n",sum1,sum2);
                ans+=max(sum1,sum2);//选择其中最大的
            }
        }
        printf("Case %d: %d\n",cases,ans);
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值