转载请注明出处: 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 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

2

2

1 2

2 3

3

1 2

2 3

4 2

Sample Output

Case 1: 2

Case 2: 3

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

典型的带权并查集。

已知u和v不在一个种族,所需处理的手段就是,把u和非v放到一个集合,把v和非u放到一个集合。利用u+MAXN来表示非u,非v也是同理。

此外,维护好每一个集合中的节点的数目,取非的不要放到算入计数。

然后枚举每个点,看他们所在的集合,取一个较大值。注意不要重复取

 //#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
const int MAXN = ;
int pa[MAXN*];
int ra[MAXN*];
void init(int n){
for(int i=;i<n;i++){
pa[i] = i;
ra[i] = ;
}
}
int find(int x){
if(pa[x]!=x)pa[x] = find(pa[x]);
return pa[x];
}
int unite(int x,int y){
x = find(x);
y = find(y);
if(x==y)return ;
if(ra[x]<ra[y]){
pa[x] = y;
}else{
pa[y] = x;
if(ra[x]==ra[y])ra[x]++;
}
return ;
}
bool same(int x,int y){
return find(x) == find(y);
}
int used[MAXN*];
int num[MAXN*];
int vis[MAXN*];
int main()
{
ios::sync_with_stdio(false);
int t;
cin>>t;
int cas = ;
while(t--){
int n;
cin>>n;
init(MAXN*);
int u,v;
memset(used,,sizeof(used));
for(int i=;i<n;i++){
cin>>u>>v;
u--;v--;
used[u] = used[v] = ;
unite(u,v+MAXN);
unite(v,u+MAXN);
}
memset(num,,sizeof(num));
for(int i=;i<MAXN;i++){
if(used[i]){
int fa = find(i);
num[fa]++;
}
}
int x,y;
memset(vis,,sizeof(vis));
int ans = ;
for(int i=;i<MAXN;i++){
if(used[i]){
x = find(i);
y = find(i+MAXN);
if(vis[x]||vis[y])continue;
ans += max(num[x],num[y]);
vis[x] = vis[y] = ;
}
}
cout<<"Case "<<cas++<<": "<<ans<<endl;
} return ;
}

Lightoj1009 Back to Underworld(带权并查集)的更多相关文章

  1. POJ 1703 Find them, Catch them(带权并查集)

    传送门 Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42463   Accep ...

  2. [NOIP摸你赛]Hzwer的陨石(带权并查集)

    题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...

  3. poj1417 带权并查集 + 背包 + 记录路径

    True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2713   Accepted: 868 Descrip ...

  4. poj1984 带权并查集(向量处理)

    Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5939   Accepted: 2 ...

  5. 【BZOJ-4690】Never Wait For Weights 带权并查集

    4690: Never Wait for Weights Time Limit: 15 Sec  Memory Limit: 256 MBSubmit: 88  Solved: 41[Submit][ ...

  6. hdu3038(带权并查集)

    题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 题意: n表示有一个长度为n的数组, 接下来有m行形如x, y, d的输入, 表示 ...

  7. 洛谷OJ P1196 银河英雄传说(带权并查集)

    题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦 创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山 ...

  8. poj1984 带权并查集

    题意:有多个点,在平面上位于坐标点上,给出一些关系,表示某个点在某个点的正东/西/南/北方向多少距离,然后给出一系列询问,表示在第几个关系给出后询问某两点的曼哈顿距离,或者未知则输出-1. 只要在元素 ...

  9. poj1611 带权并查集

    题意:病毒蔓延,现在有 n 个人,其中 0 号被认为可能感染,然后给出多个社交圈,如果某个社交圈里有人被认为可能被感染,那么所有这个社交圈里的人都被认为可能被感染,现在问有多少人可能被感染. 带权并查 ...

随机推荐

  1. C++ Primer 5th 第6章 函数

    正如第一章所说:C++的函数是一个能够完成一个功能的模块或者说是一段命名了的代码块. 如下图所示,函数可以重载,是一段实现某些功能命名了的代码. 一个完整的函数的构成有四部分: 1.返回类型 2.函数 ...

  2. PHP打开PDO_MySQL扩展的配置方法

    PHP中的PDO其实是一个很好用的扩展,在一些PHPCMS系统中,开发者大多都有用到,那么如何开启PDO和PDO_MySQL扩展呢?方法同样很简单: 打开php.ini配置文件,找到extension ...

  3. .net转php laraval框架学习系列(二)项目实战---Models

    上一篇已经介绍开发环境的搭建,如果有问题可以在文章后留言. 这篇将从项目实战开发,一步一步了解laravel框架. 在开发mvc项目时,models都是第一步. 下面就从建模开始. 实体关系图 由于不 ...

  4. 重写Collections实现自定义排序

    Collections.sort(List<Object>, new Comparator<Object>() { public int compare(Object o1, ...

  5. UC/OS操作系统 (转)

    1.和其他一些著名的嵌入式操作系统不同,uC/OS-II在单片机系统中的启动过程比较简单,不像有些操作系统那样,需要把内核编译成一个映像文件写入ROM中,上电复位后,再从ROM中把文件加载到RAM中去 ...

  6. [poj 1364]King[差分约束详解(续篇)][超级源点][SPFA][Bellman-Ford]

    题意 有n个数的序列, 下标为[1.. N ], 限制条件为: 下标从 si 到 si+ni 的项求和 < 或 > ki. 一共有m个限制条件. 问是否存在满足条件的序列. 思路 转化为差 ...

  7. 定时关机命令-shutdown

    定时关机命令-shutdown 一般会用到的定时关机命令有两种: Shutdown -s -t 3600 1小时后自动关机(3600秒) at 12:00 Shutdown -s 12:00自动关闭计 ...

  8. 使IE6同样支持圆角效果

    之前写到过,IE6不支持:hover效果的解决办法,其它这个跟它一样.IE6(7/8)不支持border-radius属性,所以其中的圆角效果显示不出来,可以通过引用ie-css3.htc的方法解决. ...

  9. SOA 新业务语言 新系统架构——什么是SOA

    原文地址:http://blog.csdn.net/ichaos/archive/2008/01/20/2054377.aspx SOA的概念是Gartner在1996年提出来的,并于2002年12月 ...

  10. 第19讲- UI组件之_Button、checkbox、radio

    第19讲 UI组件之_Button.checkbox.radio 四.按钮Button Button继承自TextView,间接继承自View.当用户对按钮进行操作的时候,触发相应事件,如点击,触摸. ...