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

原创 2016年08月29日 12:29:53
                                                                                    1009 - Back to Underworld
    PDF (English) Statistics Forum
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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【Lightoj 1009 Back to Underworld】+ 二分图染色

Description The Vampires and Lykans are fighting each other to death. The war has become so fierce ...

LightOJ1009-Back to Underworld -二分图染色

题目大意:给你n个对,两个元素是不同阵营的,一共有两个阵营,问你一个阵营可能最多有多少个人; 题目解析:二分图染色问题,dfs即可,最后每个二分图都取最大值相加就是答案; AC代码: #incl...

LightOJ 1009 Back to Underworld 搜索

题目:http://www.lightoj.com/volume_showproblem.php?problem=1009 题意:吸血鬼和狼人(胡乱猜的~)大战,知道所有的对战组合,但不能分...

Light OJ 1009 - Back to Underworld

简单的二分图

lightoj1009 - Back to Underworld【dfs】

1009 - Back to Underworld     PDF (English) Statistics Forum Time Limit...

Back to Underworld

Time Limit:4000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status...

【loj】#6006. 「网络流 24 题」试题库(二分图匹配)

假设一个试题库中有 n n n 道试题。每道试题都标明了所属类别。同一道题可能有多个类别属性。现要从题库中抽取 m m m 道题组成试卷。并要求试卷包含指定类型的试题。试设计一个满足要求的组卷算法。

[线段树][二分图 霍尔定理]LOJ#6062 && 2017 山东一轮集训 Day2. Pair

刚做过一道类似的题因为题目要求连续子序列,但是只要这个连续子序列和b序列两两对应,这就可以转化成一个二分图的模型。把b序列排序,一个ai就可以和b的一个后缀相连,可以看成一个覆盖某个后缀的线段,根据霍...

【loj】#6007. 「网络流 24 题」方格取数(二分图最大点权独立集)

在一个有 m×n 个方格的棋盘中,每个方格中有一个正整数。 现要从方格中取数,使任意 2 个数所在方格没有公共边,且取出的数的总和最大。试设计一个满足要求的取数算法。

【loj】#6000. 「网络流 24 题」搭配飞行员(二分图匹配)

题目链接 网络流虽然看过,但一直没系统的学过。现在一边复习,一边研究一下。。都是模板题。 二分图匹配。#include using namespace std; #define ALL(v) (...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)