【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] ...
随机推荐
- Shell脚本中cd命令使用
在写shell脚本的时候发现cd切换目录的时候无法切换,代码是下面的. #!/bin/bash #changedir.sh history cd /home/firefox sleep pwd 我仔细 ...
- 基于网页内容数据采集 PHP开发学习笔记
jQuery数字的截取: str.toFixed(num);//小数的截取 toFixed() <script type="text/javascript"> var ...
- Django authentication 使用方法
转自 : https://docs.djangoproject.com/en/1.8/topics/auth/customizing/
- Resumable uploads over HTTP. Protocol specification
Valery Kholodkov <valery@grid.net.ru>, 2010 1. Introduction This document describes applicatio ...
- 【Django】如何按天 小时等查询统计?
代码: from django.db import connection from django.db.models import Sum,Count #alarm_sum_group_items = ...
- .html和.htm的区别
很多人会认为网页扩展名html和htm是等同的,但事实上他们还是有区别的. 包含HTML内容的文件最常用的扩展名是.html,但是像DOS这样的旧操作系统限制扩展名为最多3个字符,所以.htm扩展名也 ...
- hdu 1160 FatMouse's Speed 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 题目意思:给出一堆老鼠,假设有 n 只(输入n条信息后Ctrl+Z).每只老鼠有对应的weigh ...
- HDU 5742 It's All In The Mind (贪心) 2016杭电多校联合第二场
题目:传送门. 题意:求题目中的公式的最大值,且满足题目中的三个条件. 题解:前两个数越大越好. #include <iostream> #include <algorithm> ...
- 《Algorithms算法》笔记:优先队列(2)——二叉堆
二叉堆 1 二叉堆的定义 堆是一个完全二叉树结构(除了最底下一层,其他层全是完全平衡的),如果每个结点都大于它的两个孩子,那么这个堆是有序的. 二叉堆是一组能够用堆有序的完全二叉树排序的元素,并在数组 ...
- 好玩儿的expect
前言 1> 借鉴里面的应用思想,使用断言提高代码的健壮性及维护性 2> 实现方式——不采用直接嵌入expect的方式,统一进行重写(提取常用断言方法,重新构造API) 官网介绍 https ...