【BZOJ】1854: [Scoi2010]游戏
http://www.lydsy.com/JudgeOnline/problem.php?id=1854
题意:n个数据,每个数据有两个属性,要求取一些数据且每个数据取一个属性使得组成连续的一段单调递增的数(从1开始),求最大能连续到多少。(n<=1000000)
#include <bits/stdc++.h>
using namespace std;
const int N=1000005;
int p[N], n, mx;
bool vis[N];
int ifind(int x) { return x==p[x]?x:p[x]=ifind(p[x]); }
struct dat { int x, y; }a[N];
int main() {
scanf("%d", &n);
for(int i=0; i<n; ++i) scanf("%d %d", &a[i].x, &a[i].y), mx=max(mx, max(a[i].x, a[i].y));
for(int i=1; i<=mx; ++i) p[i]=i;
for(int i=0; i<n; ++i) {
int fx=ifind(a[i].x), fy=ifind(a[i].y); if(fx>fy) swap(fx, fy);
if(fx==fy) vis[fx]=1;
else { vis[fx]=1; p[fx]=fy; }
}
for(int i=1; i<=mx+1; ++i) if(!vis[i]) { printf("%d\n", i-1); return 0; }
return 0;
}
一开始我想了个很诡异的贪心...wa了...然后自测是90囧QAQ....(然后对着数据改程序然后rp好我改了一个地方就a了....
然后明摆着我那样做是错的....请看当时代码:
#include <bits/stdc++.h>
using namespace std;
const int N=1000005;
int cnt[N], n, mx, ihead[N], tot;
struct dat { int next, to; }e[N];
inline void add(const int &u, const int &v) { e[++tot].next=ihead[u]; ihead[u]=tot; e[tot].to=v; }
int main() {
scanf("%d", &n);
for(int i=0; i<n; ++i) {
int x, y;
scanf("%d %d", &x, &y);
if(x>y) swap(x, y);
mx=max(mx, y);
++cnt[x]; ++cnt[y];
add(x, y);
}
for(int i=1; i<=mx+1; ++i) {
if(cnt[i]==0) { printf("%d\n", i-1); break; }
int cmx=mx+2;
for(int j=ihead[i]; j; j=e[j].next) if(cnt[e[j].to]==1) cmx=min(cmx, e[j].to);
--cnt[cmx]; //printf("%d\n", cmx);
--cnt[i];
}
return 0;
}
。。。。。
后来查了题解。。。好神的并查集....
首先我们可以将每个点所有的两个属性看做两个点,然后连边。
那么有:
如果每个连通块是一棵树,大小为a,显然a-1个数一定能得到
如果连通块是环,大小为a,那么a个数都能得到
然后就用并查集维护,且启发式合并:小的数设置大的数为父亲,而且默认每个连通块的根的vis为0.除非有环,则连通块的根的vis为1
【BZOJ】1854: [Scoi2010]游戏的更多相关文章
- BZOJ 1854: [Scoi2010]游戏 无向图判环
题目链接: 题目 1854: [Scoi2010]游戏 Time Limit: 5 Sec Memory Limit: 162 MB 问题描述 lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装 ...
- BZOJ 1854: [Scoi2010]游戏( 二分图最大匹配 )
匈牙利算法..从1~10000依次找增广路, 找不到就停止, 输出答案. --------------------------------------------------------------- ...
- BZOJ 1854: [Scoi2010]游戏 并查集
1854: [Scoi2010]游戏 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 2672 Solved: 958[Submit][Status][ ...
- ●BZOJ 1854 [Scoi2010]游戏
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1854 题解: 并查集(还可以用匈牙利算法进行单路增广的二分图匹配) 把每个武器看成是一条边, ...
- bzoj 1854: [Scoi2010]游戏 (并查集||二分图最大匹配)
链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1854 写法1: 二分图最大匹配 思路: 将武器的属性对武器编号建边,因为只有10000种 ...
- BZOJ 1854: [Scoi2010]游戏(二分图匹配/并查集)
题面: https://www.lydsy.com/JudgeOnline/problem.php?id=1854 题解: 1.二分图匹配: 首先我们发现每件装备只能在两种属性中选一种.因此,我们以每 ...
- bzoj 1854: [Scoi2010]游戏
#include<cstdio> #include<iostream> #include<cstring> #define M 2000008 using name ...
- BZOJ 1854: [Scoi2010]游戏 [连通分量 | 并查集 | 二分图匹配]
题意: 有$n \le 10^6$中物品,每种两个权值$\le 10^4$只能选一个,使得选出的所有权值从1递增,最大递增到多少 一开始想了一个奇怪的规定流量网络流+二分答案做法...然而我还不知道怎 ...
- bzoj 1854: [Scoi2010]游戏【匈牙利算法】
没啥可说的,就是一边属性一边道具建二分图,把两个属性都连到道具上,然后枚举匹配,如果无法匹配就输出,时间戳优化 #include<iostream> #include<cstdio& ...
- 【BZOJ】1854: [Scoi2010]游戏【二分图】
1854: [Scoi2010]游戏 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 6759 Solved: 2812[Submit][Status] ...
随机推荐
- Linux upstart启动方式详解
Ubuntu从6.10开始逐步用Upstart()代替原来的SysVinit进行服务进程的管理.RHEL(CentOS)也都从版本6开始转用Upstart代替以往的init.d/rcX.d的线性启动 ...
- HDOJ 1312 DFS&BFS
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 3 字节的 UTF-8 序列的字节 3 无效。
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document fro ...
- 21.左旋转字符串[LeftRotateString]
[题目] 定义字符串的左旋转操作:把字符串前面的若干个字符移动到字符串的尾部.如把字符串AB1234 左旋转2位得到字符串1234AB.请实现字符串左旋转的函数.要求时间对长度为n的字符串操作的复杂度 ...
- python chm 中文帮助 (2.7 和 3.4)
sphinx-build 生成的(htmlhelp) 存在2个问题 1.生成的html 编码 cp2152,需要修改/sphinx/builders/html.py ctx['encoding'] = ...
- JavaScript设计模式 - 迭代器模式
迭代器模式是指提供一种方法顺序访问一个聚合对象中的各个元素,而又不需要暴露该对象的内部表示. 迭代器模式可以把迭代的过程从业务逻辑中分离出来,在使用迭代器模式之后,即使不关心对象的内部构造,也可以按顺 ...
- angularjs 指令(directive)详解(1)
原文地址 什么是directive?我们先来看一下官方的解释: At a high level, directives are markers on a DOM element (such as an ...
- WebSocket技术
webSocket技术 在html5技术革新中,加入了WebSocket技术 1.webSocket实际是TCP连接 webSocket在最初将发送http连接请求到服务器端, 但是在header中加 ...
- GLSL语言变量
转自:http://www.th7.cn/Program/cp/201208/87679.shtml GLSL语言内置的变量,包括内置的顶点属性(attribute).一致变量(uniform). ...
- HTML CSS 中DIV内容居中汇总
转载博客(http://www.cnblogs.com/dearxinli/p/3865099.html) (备注:DIV居中情况,网上谈到也比较多,但是这篇文字,相对还是挺全面,现转载,如果冒犯,还 ...