Lightoj1009 Back to Underworld(带权并查集)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
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(带权并查集)的更多相关文章
- POJ 1703 Find them, Catch them(带权并查集)
传送门 Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42463 Accep ...
- [NOIP摸你赛]Hzwer的陨石(带权并查集)
题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...
- poj1417 带权并查集 + 背包 + 记录路径
True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2713 Accepted: 868 Descrip ...
- poj1984 带权并查集(向量处理)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5939 Accepted: 2 ...
- 【BZOJ-4690】Never Wait For Weights 带权并查集
4690: Never Wait for Weights Time Limit: 15 Sec Memory Limit: 256 MBSubmit: 88 Solved: 41[Submit][ ...
- hdu3038(带权并查集)
题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 题意: n表示有一个长度为n的数组, 接下来有m行形如x, y, d的输入, 表示 ...
- 洛谷OJ P1196 银河英雄传说(带权并查集)
题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦 创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山 ...
- poj1984 带权并查集
题意:有多个点,在平面上位于坐标点上,给出一些关系,表示某个点在某个点的正东/西/南/北方向多少距离,然后给出一系列询问,表示在第几个关系给出后询问某两点的曼哈顿距离,或者未知则输出-1. 只要在元素 ...
- poj1611 带权并查集
题意:病毒蔓延,现在有 n 个人,其中 0 号被认为可能感染,然后给出多个社交圈,如果某个社交圈里有人被认为可能被感染,那么所有这个社交圈里的人都被认为可能被感染,现在问有多少人可能被感染. 带权并查 ...
随机推荐
- POJ 1151 - Atlantis 线段树+扫描线..
离散化: 将所有的x轴坐标存在一个数组里..排序.当进入一条线段时..通过二分的方式确定其左右点对应的离散值... 扫描线..可以看成一根平行于x轴的直线..至y=0开始往上扫..直到扫出最后一条平行 ...
- google反向代理网址收集
前言 亲,还在为谷歌被墙而懊恼么?还在苦苦搜集FQ手段么?往下看吧? 最近在网站链接来源统计中,发现了很多反向代理了谷歌的链接,故搜集在这里,供需要的人使用,使用如下链接谷歌搜素不需要FQ哦?下面地址 ...
- destoon系统商城加淘宝客按钮方法
destoon系统很多喜欢运营B2B的站长都在用,其中的商城模块常常被用来做淘宝客,其中的难点是如何把购买按钮做成淘宝客地址,这个问题的修改在论坛上被叫价50元,下面小编把这个实用的方法分享下,希望对 ...
- 进程外组件通信之免注册com通信【原创】
最近在搞进程外组件通信的东西,写了个demo,免注册的,一直没调通,其实就是两个问题卡了好几天,也没找到有用的资料,试了好几天终于才解决,现简单记录下来,免得大家跟我走一样的弯路.下面com端程序名称 ...
- [每日一题JS] 正则表达式
判断字符串是否是这样组成的,第一个必须是字母,后面可以是字母.数字.下划线,总长度为5-20 var reg = /\b[a-zA-Z]{1}[a-zA-Z0-9_]{4,19}\b/; var fl ...
- jquery获得select选中索引
select选中索引有好多方式, 这两种方式取不到索引值这两种方式取不到索引值这两种方式取不到索引值这两种方式取不到索引值 $('#someId').find('option:select ...
- Cmake Error: your compiler "cl" was not Found .etc
又是环境变量路径等问题,烦死人了. TIPS:请注意,控制台的窗口也有自己的环境变量,从系统环境变量和用户环境变量继承过来的,一个窗口(控制台)可以添加属于自己的环境变量(跟别的控制台窗口没关系) 解 ...
- OpenJTAG+Eclipse 3.5+GDB+Mini2440图文教程
OpenJTAG+Eclipse 3.5+GDB+Mini2440图文教程 OpenJTAG与JLink的区别比较: 相同点:都同时具备USB转JTAG.USB转串口功能 差别: 1. 操作系统: O ...
- 数字积分法DDA(DDA(Digital Differential Analyzer)
数字积分法DDA(DDA(Digital Differential Analyzer) 数字积分法又称数字微分分析法DDA(Digital differential Analyzer),是在数字 ...
- 第20讲- Spinner与适配器模式
第20讲 Spinner与适配器模式 使用Spinner相当于从下拉列表中选择项目,Spinner是一个每次只能选择所有项的一个项的控件.它的项来自于与之相关联的适配器中.Spinner的重点问题就是 ...