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

[kuangbin带你飞]专题九 连通图 题解报告 专题链接 关于tarjan A - Network of Schools  原题地址 本题有2个问题,第一个是要算最少要给多少个点软件,才能使所有点都可以收到副本 第二个是要算最少加多少条边,使得图变成强连通 1:tarjan求强连通,然后缩点,计算入度为0的强连通分量 2:设现在有a个入度为0的点,b个出度为0的点(缩完点后的点),最合理的加边方法肯定是从出度为0的点 阅读详情

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.

只要对点染色就行,注意最后要取每个连通块里面的出现次数最多的那个颜色

/*************************************************************************
    > File Name: LightOJ1009.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年06月05日 星期五 12时33分33秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

static const int N = 30000;
vector <int> edge[N];
int col[N];
int Num[N][2];
int xis[N * 10];
int cnt;

struct node {
    int u, v;
}Temp[100010];

void bfs(int u, int tot) {
    col[u] = 1;
    queue <int> qu;
    qu.push(u);
    while (!qu.empty()) {
        int now = qu.front();
        ++Num[tot][col[now]];
        qu.pop();
        int size = edge[now].size();
        for (int i = 0; i < size; ++i) {
            int nxt = edge[now][i];
            if (col[nxt] == -1) {
                col[nxt] = (col[now] ^ 1);
                qu.push(nxt);   
            }
        }
    }
}

int Search(int val) {
    int l = 1, r = cnt, mid;
    while (l <= r) {
        mid = (l + r) >> 1;
        if (xis[mid] == val) {
            break;
        }
        else if (xis[mid] > val) {
            r = mid - 1;
        }
        else {
            l = mid + 1;
        }
    }
    return mid;
}

int main() {
    int t, icase = 1;
    scanf("%d", &t);
    while (t--) {
        int n, u, v;
        cnt = 0;
        scanf("%d", &n);
        for (int i = 1; i <= n; ++i) {
            scanf("%d%d", &u, &v);
            Temp[i].u = u;
            Temp[i].v = v;
            xis[++cnt] = u;
            xis[++cnt] = v;
        }
        sort(xis + 1, xis + 1 + cnt);
        cnt = unique(xis + 1, xis + 1 + cnt) - xis - 1;
        for (int i = 1; i <= cnt; ++i) {
            edge[i].clear();
            col[i] = -1;
            Num[i][0] = Num[i][1] = 0;
        }
        int tot = 0;
        for (int i = 1; i <= n; ++i) {
            u = Search(Temp[i].u);
            v = Search(Temp[i].v);
            edge[u].push_back(v);
            edge[v].push_back(u);
        }
        for (int i = 1; i <= cnt; ++i) {
            if (col[i] == -1) {
                bfs(i, ++tot);
            }
        }
        int ans = 0;
        for (int i = 1; i <= tot; ++i) {
            ans += max(Num[i][0], Num[i][1]);
        }
        printf("Case %d: %d\n", icase++, ans);
    }
    return 0;
}
LightOJ1009-Back to Underworld -二分图染色 题目大意:给你n个对,两个元素是不同阵营的,一共有两个阵营,问你一个阵营可能最多有多少个人; 题目解析:二分图染色问题,dfs即可,最后每个二分图都取最大值相加就是答案; AC代码: #include #include #include #include #include #include using namespace std; const int maxn=20010; int cnt, 阅读详情

相关推荐

1009 - Back to Underworld(二分图染色

http://www.lightoj.com/volume_showproblem.php?problem=1009 1009 - Back to Underworld     PDF (English) Statistics Forum Time Limit: 4 second(s) Memory Limit: 32 MB

ACcoders 644

1009 - Back to Underworld(DFS)

题目连接 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 hu

人生若只能如初见的博客 339

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

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 afrai

楚江枫 673

lightoj 1009 - Back to Underworld 【DFS】

题目链接:lightoj 1009 - Back to Underworld 1009 - Back to Underworld PDF (English) Statistics Forum Time Limit: 4 second(s) Memory Limit: 32 MB The Vampires and Lykans are fighting each oth

世界很大 724

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

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 t

Lesroad 298

Codeforces Round #277.5 (Div. 2)B——BerSU Ball

B. BerSU Ball time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output The Berland State University is hosting a ballroom dance

Alex 1590

LightOJ1003---Drunk(拓扑排序判环)

One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So, one day I was talking to him, about his drinks! He began to describe his way of drinking. So, let m

Alex 1492

Codeforces Round #288 (Div. 2)---D. Tanya and Password

D. Tanya and Password time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output While dad was at work, a little girl Tanya deci

Alex 1423

Timus Online Judge1627--- Join

1627. Join Time limit: 4.0 second Memory limit: 64 MB Businessman Petya recently bought a new house. This house has one floor with n × m square rooms, placed in rectangular lattice. Some roo

Alex 1411

hdu1179——Ollivanders: Makers of Fine Wands since 382 BC.

Ollivanders: Makers of Fine Wands since 382 BC. Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 964    Accepted Submission(s): 539 Prob

Alex 1407

SPOJ104. Highways

104. Highways Problem code: HIGH In some countries building highways takes a lot of time... Maybe that's because there are many possiblities to construct a network of highways and engineers can't

Alex 1382

LightOJ1002---Country Roads (最短路变形)

I am going to my home. There are many cities and many bi-directional roads between them. The cities are numbered from 0 to n-1 and each road has a cost. There are m roads. You are given the number of m

Alex 1244

Codeforces Round #294 (Div. 2)---E. A and B and Lecture Rooms

A and B are preparing themselves for programming contests.The University where A and B study is a set of rooms connected by corridors. Overall, the University has n rooms connected by n - 1 corridors s

Alex 1242

hdu1535——Invitation Cards

Invitation Cards Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2173    Accepted Submission(s): 1056 Problem Description In the age o

Alex 1182

ACdream1236——Burning Bridges

Burning Bridges Special JudgeTime Limit: 10000/5000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticNext Problem Problem Description       Ferry Kingdom is a nice litt

Alex 1149

LightOJ1012---Guilty Prince (并查集)

Once there was a king named Akbar. He had a son named Shahjahan. For an unforgivable reason the king wanted him to leave the kingdom. Since he loved his son he decided his son would be banished in a ne

Alex 1141

hdu1281---棋盘游戏

Problem Description 小希和Gardon在玩一个游戏:对一个N*M的棋盘,在格子里放尽量多的一些国际象棋里面的“车”,并且使得他们不能互相攻击,这当然很简单,但是Gardon限制了只有某些格子才可以放,小希还是很轻松的解决了这个问题(见下图)注意不能放车的地方不影响车的互相攻击。 所以现在Gardon想让小希来解决一个更难的问题,在保证尽量多的“车”的前提下,棋盘里有些格子是可

Alex 1130
上一篇: LightOJ1008---Fibsieve`s Fantabulous Birthday (规律)
下一篇: Codeforces Round #306 (Div. 2)
tokers
博客等级 码龄12年 71粉丝 700原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值