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

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: [email protected]
    > 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;
}
时间: 2024-10-21 18:17:32

LightOJ1009---Back to Underworld (bfs染色)的相关文章

XMU 1617 刘备闯三国之汉中之战 【BFS+染色】

1617: 刘备闯三国之汉中之战 Time Limit: 1000 MS  Memory Limit: 128 MBSubmit: 6  Solved: 5[Submit][Status][Web Board] Description 刘备(161年-223年6月10日),字玄德,东汉末年幽州涿郡涿县,西汉中山靖王刘胜的后代.刘备一生极具传奇色彩,早年颠沛流离.备尝艰辛最终却凭借自己的谋略终成一方霸主.那么在那个风云激荡的年代,刘备又是如何从一个卖草鞋的小人物一步一步成为蜀汉的开国皇帝呢?让我们

UVALive 6663 Count the Regions 离散+bfs染色_(:зゝ∠)_

题目链接:点击打开链接 gg..== #include <cstdio> #include <cstring> #include<iostream> #include <queue> #include <set> #include <map> #include <algorithm> #include <string> using namespace std; #define ll long long #def

UVALive 3977 BFS染色

这个题意搞了半天才搞明白 就是如果定义一个d-summit,即从该点到另一个更高的点,经过的路径必定是比当前点低至少d高度的,如果该点是最高点,没有比他更高的,就直接视为顶点 其实就是个BFS染色,先按降序排序,然后每个点对其可到达的点染色,h-d的点为边界,走到这里就不用往下染了 然后其他店染色的时候若产生冲突,则非d—summit,否则该点为顶点 今天还有COJ上一个BFS染色的题目,一直TLE...还没弄出来 #include <iostream> #include <cstdio

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

1009 - Back to Underworld PDF (English) Statistics ForumTime Limit: 4 second(s) Memory Limit: 32 MBThe 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

Lightoj1009 Back to Underworld(带权并查集)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 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 fierc

BFS(染色) LA 3977 Summits

题目传送门 题意:题意坑爹.问符合条件的的山顶个数 分析:降序排序后从每个点出发,假设为山顶,如果四周的点的高度>h - d那么可以走,如果走到已经走过的点且染色信息(山顶高度)不匹配那么就不是山顶.重点在于就算知道不是山顶也要染色完. #include <bits/stdc++.h> using namespace std; const int N = 5e2 + 5; const int INF = 0x3f3f3f3f; int h, w, d; struct Point { in

Hdu 5285 wyh2000 and pupil (bfs染色判断奇环) (二分图匹配)

题目链接: BestCoder Round #48 ($) 1002 题目描述: n个小朋友要被分成两班,但是有些小朋友之间是不认得的,所以规定不能把不认识的小朋友分在一个班级里面,并且一班的人数要比二班的人数多,每个班的人数都大于零. 解题思路: hdu给出的题解是二分图匹配加上贪心,就不多说了. 还可以用bfs对节点染色,建好图后,对节点进行bfs分成,偶数成与奇数成染成不同的颜色,颜色相同的节点都可以分到同一个集合里面,但是要判断一下奇环,如果出现奇环的话,是无法进行分组的.在每次bfs的

UVALive - 3977 Summits (BFS染色)

题目大意:坑爹的题目,题意那么难理解. 讲的就是,如果该点是山顶的话(高度为h),那么以该点为中心,往外辐射,走高度大于h-d的点,到达不了另一个比它高的点 这就提示了,高度要从大到小排序,依次以高的点(假设高度为h)为核心辐射,如果碰上高度小于等于h-d的,表示此路不通了,就在该处停止 反之,如果碰上高度大于h-d的,且没有被染色过的,那么就将其染色 如果碰上高度大于h-d的,且被染色的话,就表明可以走到另一个更高点了,那么此点就不是山顶了 如果中途碰到了和该点一样高的,那么山顶的数量就加1

CF796D Police Stations BFS+染色

类似贪心,用 BFS 对树进行染色,然后枚举哪些边的两个端点颜色不同. code: #include <bits/stdc++.h> #define N 300006 #define setIO(s) freopen(s".in","r",stdin) using namespace std; vector<int>G; queue<int>q; int n,k,d,edges,vis[N],hd[N],to[N<<1