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也是同理。
此外,维护好每一个集合中的节点的数目,取非的不要放到算入计数。
然后枚举每个点,看他们所在的集合,取一个较大值。注意不要重复取
1 //##################### 2 //Author:fraud 3 //Blog: http://www.cnblogs.com/fraud/ 4 //##################### 5 //#pragma comment(linker, "/STACK:102400000,102400000") 6 #include <iostream> 7 #include <sstream> 8 #include <ios> 9 #include <iomanip> 10 #include <functional> 11 #include <algorithm> 12 #include <vector> 13 #include <string> 14 #include <list> 15 #include <queue> 16 #include <deque> 17 #include <stack> 18 #include <set> 19 #include <map> 20 #include <cstdio> 21 #include <cstdlib> 22 #include <cmath> 23 #include <cstring> 24 #include <climits> 25 #include <cctype> 26 using namespace std; 27 #define XINF INT_MAX 28 #define INF 0x3FFFFFFF 29 #define MP(X,Y) make_pair(X,Y) 30 #define PB(X) push_back(X) 31 #define REP(X,N) for(int X=0;X<N;X++) 32 #define REP2(X,L,R) for(int X=L;X<=R;X++) 33 #define DEP(X,R,L) for(int X=R;X>=L;X--) 34 #define CLR(A,X) memset(A,X,sizeof(A)) 35 #define IT iterator 36 typedef long long ll; 37 typedef pair<int,int> PII; 38 typedef vector<PII> VII; 39 typedef vector<int> VI; 40 const int MAXN = 20010; 41 int pa[MAXN*3]; 42 int ra[MAXN*3]; 43 void init(int n){ 44 for(int i=0;i<n;i++){ 45 pa[i] = i; 46 ra[i] = 0; 47 } 48 } 49 int find(int x){ 50 if(pa[x]!=x)pa[x] = find(pa[x]); 51 return pa[x]; 52 } 53 int unite(int x,int y){ 54 x = find(x); 55 y = find(y); 56 if(x==y)return 0; 57 if(ra[x]<ra[y]){ 58 pa[x] = y; 59 }else{ 60 pa[y] = x; 61 if(ra[x]==ra[y])ra[x]++; 62 } 63 return 1; 64 } 65 bool same(int x,int y){ 66 return find(x) == find(y); 67 } 68 int used[MAXN*3]; 69 int num[MAXN*3]; 70 int vis[MAXN*3]; 71 int main() 72 { 73 ios::sync_with_stdio(false); 74 int t; 75 cin>>t; 76 int cas = 1; 77 while(t--){ 78 int n; 79 cin>>n; 80 init(MAXN*2); 81 int u,v; 82 memset(used,0,sizeof(used)); 83 for(int i=0;i<n;i++){ 84 cin>>u>>v; 85 u--;v--; 86 used[u] = used[v] = 1; 87 unite(u,v+MAXN); 88 unite(v,u+MAXN); 89 } 90 memset(num,0,sizeof(num)); 91 for(int i=0;i<MAXN;i++){ 92 if(used[i]){ 93 int fa = find(i); 94 num[fa]++; 95 } 96 } 97 int x,y; 98 memset(vis,0,sizeof(vis)); 99 int ans = 0; 100 for(int i=0;i<MAXN;i++){ 101 if(used[i]){ 102 x = find(i); 103 y = find(i+MAXN); 104 if(vis[x]||vis[y])continue; 105 ans += max(num[x],num[y]); 106 vis[x] = vis[y] = 1; 107 } 108 } 109 cout<<"Case "<<cas++<<": "<<ans<<endl; 110 } 111 112 return 0; 113 }
转载于:https://www.cnblogs.com/fraud/p/4665866.html
Lightoj1009 Back to Underworld(带权并查集)相关推荐
- 2017乌鲁木齐区域赛I(带权并查集)
#include<bits/stdc++.h> using namespace std; int f[200010];//代表元 long long rl[200010];//记rl[i] ...
- BZOJ 2303 方格染色(带权并查集)
要使得每个2*2的矩形有奇数个红色,如果我们把红色记为1,蓝色记为0,那么我们得到了这2*2的矩形里的数字异或和为1. 对于每个方格则有a(i,j)^a(i-1,j)^a(i,j-1)^a(i-1,j ...
- POJ1703带权并查集(距离或者异或)
题意: 有两个黑社会帮派,有n个人,他们肯定属于两个帮派中的一个,然后有两种操作 1 D a b 给出a b 两个人不属于同一个帮派 2 A a b 问a b 两个人关系 输出 同一个帮派 ...
- POJ1988(带权并查集,搬砖块)
题意: 可以这样理解,有n快方形积木,一开始都是单独的放到哪,然后有两种操作 1 M a b 把a所在的那一堆落到b所在那一堆的上面(一开始自己是一堆) 2 C a 问a下面有多少个积木 ...
- LA3027简单带权并查集
题意: 有n个点,一开始大家都是独立的点,然后给出一些关系,a,b表示a是b的父亲节点,距离是abs(a-b)%1000,然后有一些询问,每次询问一个节点a到父亲节点的距离是多少? 思路: ...
- hdu3234 带权并查集(XOR)
题意: 给你n个未知的正整数,有三总操作 I P V P的值是V I P Q V P XOR Q = V Q K ...
- hdu4829 带权并查集(题目不错)
题意: Information Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- poj1182 and 携程预赛2第一题 带权并查集
题意: 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底 ...
- How Many Answers Are Wrong HDU - 3038(带权并查集经典题,满满的都是注释)
How Many Answers Are Wrong HDU - 3038 点击打开链接 题意:现在有n个数(你并不知道这n个数是什么),m次查询,每次查询给出u,v,w.表示从第u个数到第v个数的 ...
- cdoj 1070 秋实大哥打游戏 带权并查集
题目链接: http://acm.uestc.edu.cn/#/problem/show/1070 题意: 题解: 带权并查集 每次往上更新的时候,顺便把边权更新了就好 记住得路径压缩 代码: 1 # ...
最新文章
- ExecutorService框架
- 点评----和时代专线
- Unity3D学习笔记(七):叉乘和四元素
- 利用photoshop制作gif图片
- mysql连接非常慢的觖决办法及其它常见问题解决办法
- 杭电2019 数列有序!(STL解法)
- Android刷新当前页面
- Thymeleaf 模板布局三种区别
- 迅速学tar命令对文件的打包压缩与解压缩
- 计算机与测控技术专业就业方向,东北电力大学测控技术与仪器专业就业前景
- win10系统在文件夹中图片不显示内容问题
- UE4.26像素流公网访问linux和win两种实现方式
- SNF快速开发平台MVC-Grid++集成打印
- 深入理解计算机系统bomb lab
- Android MQTT TLS/SSL 认证
- Springboot下RedisTemplate的两种序列化方式
- 环信即时通讯云获得A轮融资 开启 “高品质更好用”IM时代
- 芋道 Spring Cloud Alibaba 介绍
- 网络工程师能干到多大?网络工程师是不是青春饭?
- 百问网7天物联网智能家居 学习心得 打卡第五天