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

原创 2016年08月29日 12:29:53
                                                                                    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;
}


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

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

http://www.lightoj.com/volume_showproblem.php?problem=1009 1009 - Back to Underworld ...
  • Burning_newbie
  • Burning_newbie
  • 2016年03月28日 15:57
  • 264

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

题目大意:有两个国家在打仗。现在给出N场战役,每场战役由两个数字表示,两个数字分别代表一个国家的其中一只军队。问一个国家最多有多少军队解题思路:二分图染色,和该点对立的点,就是别的国家的,再统计一下相...
  • L123012013048
  • L123012013048
  • 2015年11月08日 23:05
  • 495

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

Description The Vampires and Lykans are fighting each other to death. The war has become so fierce ...
  • WYK1823376647
  • WYK1823376647
  • 2016年10月07日 11:46
  • 290

lightoj 1009 - Back to Underworld 【DFS】

题目链接:lightoj 1009 - Back to Underworld 1009 - Back to Underworld PDF (English) Statistic...
  • chenzhenyu123456
  • chenzhenyu123456
  • 2016年04月13日 22:07
  • 389

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

The Vampires and Lykans are fighting each other to death. The war has become so fierce that, none kn...
  • SSimpLe_Y
  • SSimpLe_Y
  • 2017年01月14日 14:18
  • 366

LightOJ1009---Back to Underworld (bfs染色)

The Vampires and Lykans are fighting each other to death. The war has become so fierce that, none kn...
  • Guard_Mine
  • Guard_Mine
  • 2015年06月05日 13:00
  • 991

LightOJ 1009 Back to Underworld

贪心+搜索 #include using namespace std; int fa[20005]; vector g[20005]; int bfs(int start) { int...
  • qq_25884463
  • qq_25884463
  • 2015年11月17日 20:34
  • 199

【lightoj1009 - Back to Underworld】二分图染色dfs

The Vampires and Lykans are fighting each other to death. The war has become so fierce that, none kn...
  • c_circle
  • c_circle
  • 2018年01月22日 10:50
  • 18

1009 - Back to Underworld(DFS)

题目连接 Time Limit: 4 second(s) Memory Limit: 32 MB The Vampires and Lykans ...
  • qq_38177302
  • qq_38177302
  • 2018年01月28日 12:51
  • 31

lightoj 1009 Back to Underworld (种类并查集)

The Vampires and Lykans are fighting each other to death. The war has become so fierce that, none kn...
  • zs120197
  • zs120197
  • 2017年08月22日 15:58
  • 70
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LOJ 1009 - Back to Underworld(二分图染色)
举报原因:
原因补充:

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