Back to Underworld(lightoj 1009)
| 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 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.
思路:并查集;
我们要把上面的点分成两个阵营,p-q表示p,q这两个点是对立的是不同的阵营,那么我们将每一个点分别构造两个不同的阵营,假如是点P;
那么(p,p+cnt)就为两个不同的阵营的表示法,P-q;也就是p和q+cnt是一个阵营,q和p+cnt是一个阵营,因为p和p+cnt对立,同理。
那么我们就用并差集来合并,然后最后我们可以得到多个集合,并且,其中的每个集合,在这些集合中我们可以找到他的对立集合,所以我们贪心选取,两个集合中最大的那个,加入sum。
(注:每个两个点最多有3种关系,1,同一阵营,2不同阵营,3没有关系)

1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<stdlib.h>
6 #include<queue>
7 #include<map>
8 using namespace std;
9 int du[20005*3];
10 int str[3*20005];
11 int flag[3*20005];
12 typedef struct pp
13 {
14 int x;
15 int y;
16 }ss;ss aa[100005];
17 map<int,int>my;
18 int main(void)
19 {
20 int n,i,j,k,p,q;
21 int s;
22 scanf("%d",&k);
23 for(s=1;s<=k;s++)
24 { my.clear();
25 scanf("%d",&n);memset(flag,0,sizeof(flag));
26 for(i=0;i<3*20005;i++)
27 {str[i]=i;du[i]=1;}
28 int cnt=1;
29 for(i=0;i<n;i++)
30 {
31 scanf("%d %d",&aa[i].x,&aa[i].y);
32 if(my[aa[i].x]==0)
33 my[aa[i].x]=cnt++;
34 aa[i].x=my[aa[i].x];
35 if(my[aa[i].y]==0)
36 my[aa[i].y]=cnt++;
37 aa[i].y=my[aa[i].y];
38 }
39 for(i=cnt;i<3*20005;i++)
40 du[i]=0;
41 for(i=0;i<n;i++)
42 {
43 int x=aa[i].x;
44 int y=aa[i].y;
45 int xx=x+cnt-1;
46 int yy=y+cnt-1;
47 int c,c1,c2,c3;
48 for(c=x;str[c]!=c;)
49 c=str[c];
50 for(c1=y;c1!=str[c1];)
51 c1=str[c1];
52 for(c2=xx;str[c2]!=c2;)
53 c2=str[c2];
54 for(c3=yy;c3!=str[c3];)
55 c3=str[c3];
56 if(c!=c3)
57 {
58 if(du[c]>=du[c3])
59 {
60 du[c]+=du[c3];
61 str[c3]=c;
62 }
63 else {du[c3]+=du[c];str[c]=c3;}
64 }
65 if(c1!=c2)
66 {
67 if(du[c2]>=du[c1])
68 {
69 du[c2]+=du[c1];
70 str[c1]=c2;
71 }
72 else
73 {
74 du[c1]+=du[c2];
75 str[c2]=c1;
76 }
77 }
78 }int sum=0;
79 for(i=1;i<cnt;i++)
80 {
81 int c,d;
82 for(c=i;c!=str[c];)
83 {c=str[c];}
84 for(d=i+cnt-1;d!=str[d];)
85 {d=str[d];}
86 if(!flag[c])
87 {
88 sum+=max(du[c],du[d]);
89 flag[c]=1;flag[d]=1;
90 }
91 }
92 printf("Case %d: %d\n",s,sum);
93 }return 0;
94 }
Back to Underworld(lightoj 1009)的更多相关文章
- Lightoj 1009 - Back to Underworld
1009 - Back to Underworld PDF (English) Statistics Forum Time Limit: 4 second(s) Memory Limit: 32 ...
- LightOJ 1009 二分图染色+BFS/种类并查集
题意:有两个阵营的人,他们互相敌对,给出互相敌对的人,问同个阵营的人最多有多少个. 思路:可以使用种类并查集写.也可以使用使用二分图染色的写法,由于给定的点并不是连续的,所以排序离散化一下,再进行BF ...
- Back to Underworld(搜索)
Back to Underworld Time Limit:4000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Su ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 1009: [HNOI2008]GT考试
1009: [HNOI2008]GT考试 Time Limit: 1 Sec Memory Limit: 162 MB Description 阿申准备报名参加GT考试,准考证号为N位数\(X_1X_ ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- LightOj 1298 - One Theorem, One Year(DP + 欧拉)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1298 题意:给你两个数 n, p,表示一个数是由前 k 个素数组成的,共有 n 个素数 ...
- 1214 - Large Division -- LightOj(大数取余)
http://lightoj.com/volume_showproblem.php?problem=1214 这就是一道简单的大数取余. 还想还用到了同余定理: 所谓的同余,顾名思义,就是许多的数被一 ...
- LightOJ Beginners Problems 部分题解
相关代码请戳 https://coding.net/u/tiny656/p/LightOJ/git 1006 Hex-a-bonacci. 用数组模拟记录结果,注意取模 1008 Fibsieve's ...
随机推荐
- 截取字符串、拼接字符串【c#】
string compname="1与3"; String[] name = compname.Split('与'); string namer=name[0]; namer=1 ...
- 8 — springboot中静态资源处理方式 - 前后端分离 这没屁用
7中说了thymeleaf,哪还有一个目录是static 那么就来研究一下静态资源 静态资源,springboot底层是怎么去装配的,都在WebMvcAutoConfiguration有答案,去看一下 ...
- 面向Web应用的并发压力测试工具——Locust实用攻略
1. 概述 该方案写作目的在于描述一个基于Locust实现的压力测试,文中详细地描述了如何利用locustfile.py文件定义期望达成的测试用例,并利用Locust对目标站点进行并发压力测试. 特别 ...
- CAD简介
Computer-aided design (CAD) is the use of computers (or workstations) to aid in the creation, modifi ...
- Mybatis相关知识点(二)
Mybatis解决jdbc编程的问题 1. 数据库连接创建.释放频繁造成系统资源浪费从而影响系统性能,如果使用数据库连接池可解决此问题. 解决:在SqlMapConfig.xml中配置数据连接池,使用 ...
- 零基础学习java------day18------properties集合,多线程(线程和进程,多线程的实现,线程中的方法,线程的声明周期,线程安全问题,wait/notify.notifyAll,死锁,线程池),
1.Properties集合 1.1 概述: Properties类表示了一个持久的属性集.Properties可保存在流中或从流中加载.属性列表中每个键及其对应值都是一个字符串 一个属性列表可包含另 ...
- 关于浏览器,从输入URL到呈现页面过程!(主讲TCP/IP协议)
一.文本对话--从请求到响应 我们在浏览器中输入一个 URL,回车之后便会在浏览器中观察到页面内容.实际上这个过程是: (1)浏览器向网站所在的服务器发送了一个 Request(请求) (2)网站服务 ...
- redis入门到精通系列(一)
(一)为什么要用Nosql 如果你是计算机本科学生 ,那么一定使用过关系型数据库mysql.在请求量小的情况下,使用mysql不会有任何问题,但是一旦同时有成千上万个请求同时来访问系统时,就会出现卡顿 ...
- navicat突然连接不上远程linux服务器上的mysql
我linux服务器上的mysql是docker安装的,突然有一天我的navicat连接不上服务器上的mysql,于是开始了下面一系列的修复 1.首先登录服务器上mysql,看是否能正常登录,我发现不能 ...
- springboot优雅实现异常处理
前言 在平时的 API 开发过程中,总会遇到一些错误异常没有捕捉到的情况.那有的小伙伴可能会想,这还不简单么,我在 API 最外层加一个 try...catch 不就完事了. 哈哈哈,没错.这种方法简 ...