LOJ 1009 - Back to Underworld(二分图染色)

                                                                                    1009 - Back to Underworld
Time Limit: 4 second(s)Memory Limit: 32 MB

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.

题意:问其中两个种族的数目较大的那个值最大可能是多少

#include<cstdio>
#include<cstring>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 20010;
vector<int> G[maxn];
int v;
int color[maxn]; // 染色为1和-1
bool vis[maxn];  // 记录该点是否存在
int sum,sum1;
bool dfs(int v,int c)
{
    sum++;
    if(c == 1)
        sum1++;
    color[v] = c;
    for(int i = 0 ; i < G[v].size() ; i++)
    {
        if(color[G[v][i]]) continue;
        dfs(G[v][i],-c);
    }
}
int main()
{
    int t,n,kcase = 1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        memset(color,0,sizeof(color));
        memset(vis,false,sizeof(vis));
        for(int i = 1 ; i <= maxn ; i++)
            G[i].clear();
        int maxx = 0;
        int minn = maxn;
        for(int i = 0 ; i < n ; i++)
        {
             int x,y;
             scanf("%d%d",&x,&y);
             vis[x] = vis[y] = true;
             G[x].push_back(y);
             G[y].push_back(x);
             maxx = max(max(maxx,x),y);
             minn = min(min(minn,x),y);
        }
        int ans = 0;
        for(int i = minn ; i <= maxx ; i++)
        {
            if(!vis[i] || color[i] != 0) continue; 
            sum = sum1 = 0;
            dfs(i,1);
            ans += max(sum1,sum-sum1);
        }
        printf("Case %d: %d\n",kcase++,ans);
    }
    return 0;
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值