Back to Underworld

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


题意: 狼人和吸血鬼作战 (最近在看吸血鬼日记,看到这道题莫名的兴奋...) 计算得到狼人或吸血鬼数量的最大值...

思路: 带权并查集,,

         运用向量的想法, 使用rel数组表示U(狼人)和V(吸血鬼)的关系, 不同类为1,同类为0, 基本上是模板的套用...

         本题主要是卡在计算上面...本题会有多个群, 多个根节点, 需要计算根节点下的数量, 从而才能计算两种生物的数量,

         则 num数组计算根节点下的数量, foe数组计算与根节点不同生物的数量, don数组记录是否作战...

code:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
#define maxx 20005
int set[maxx],rel[maxx],num[maxx];
int don[maxx],foe[maxx];

int find(int a)
{
    int t;
    if(a==set[a])return a;
    else t=find(set[a]);
    rel[a]=(rel[a]+rel[set[a]])%2;
    return set[a]=t;
}

void unite(int a,int b)
{
    int fa=find(a),fb=find(b);
    if(fa==fb)return ;
    else
    {
        set[fb]=fa;
        num[fa]+=num[fb];//用来记录根节点下的数量
        rel[fb]=(rel[a]-rel[b]+1)%2;
    }
}

int main()
{
    //freopen("D.txt","r",stdin);
    int T,n,u,v;
    scanf("%d",&T);
    for(int i=1;i<=T;i++)
    {
        int ans=0;
        for(int j=1;j<=maxx;j++)
           {
               rel[j]=0;
               set[j]=j;
               foe[j]=0;
               num[j]=1;
               don[j]=0;
           }
        scanf("%d",&n);
        while(n--)
        {
            scanf("%d%d",&u,&v);
            unite(u,v);
            don[u]=1;
            don[v]=1;
        }
       for(int j=1;j<=maxx;j++)   //不用担心时间会长,因为已经压缩路径了,直接就可查到根节点
       {
           if(don[j])
           {
               int fj=find(j);
               if(rel[j]) foe[fj]++; 
           }
       }
       for(int j=1;j<=maxx;j++)  //计算每个群的最大数
       {
           if(don[j]&&j==set[j])
           {
               ans+=(foe[j]>(num[j]-foe[j])?foe[j]:(num[j]-foe[j]));
           }
       }
        printf("Case %d: %d\n",i,ans);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值