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]游戏的更多相关文章

  1. BZOJ 1854: [Scoi2010]游戏 无向图判环

    题目链接: 题目 1854: [Scoi2010]游戏 Time Limit: 5 Sec Memory Limit: 162 MB 问题描述 lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装 ...

  2. BZOJ 1854: [Scoi2010]游戏( 二分图最大匹配 )

    匈牙利算法..从1~10000依次找增广路, 找不到就停止, 输出答案. --------------------------------------------------------------- ...

  3. BZOJ 1854: [Scoi2010]游戏 并查集

    1854: [Scoi2010]游戏 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 2672  Solved: 958[Submit][Status][ ...

  4. ●BZOJ 1854 [Scoi2010]游戏

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1854 题解: 并查集(还可以用匈牙利算法进行单路增广的二分图匹配) 把每个武器看成是一条边, ...

  5. bzoj 1854: [Scoi2010]游戏 (并查集||二分图最大匹配)

    链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1854 写法1: 二分图最大匹配 思路:  将武器的属性对武器编号建边,因为只有10000种 ...

  6. BZOJ 1854: [Scoi2010]游戏(二分图匹配/并查集)

    题面: https://www.lydsy.com/JudgeOnline/problem.php?id=1854 题解: 1.二分图匹配: 首先我们发现每件装备只能在两种属性中选一种.因此,我们以每 ...

  7. bzoj 1854: [Scoi2010]游戏

    #include<cstdio> #include<iostream> #include<cstring> #define M 2000008 using name ...

  8. BZOJ 1854: [Scoi2010]游戏 [连通分量 | 并查集 | 二分图匹配]

    题意: 有$n \le 10^6$中物品,每种两个权值$\le 10^4$只能选一个,使得选出的所有权值从1递增,最大递增到多少 一开始想了一个奇怪的规定流量网络流+二分答案做法...然而我还不知道怎 ...

  9. bzoj 1854: [Scoi2010]游戏【匈牙利算法】

    没啥可说的,就是一边属性一边道具建二分图,把两个属性都连到道具上,然后枚举匹配,如果无法匹配就输出,时间戳优化 #include<iostream> #include<cstdio& ...

  10. 【BZOJ】1854: [Scoi2010]游戏【二分图】

    1854: [Scoi2010]游戏 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 6759  Solved: 2812[Submit][Status] ...

随机推荐

  1. sqlMapConfig.xml配置文件详解

    sqlMapConfig.xml配置文件详解: Xml代码 Xml代码  <? xml version="1.0" encoding="UTF-8" ?& ...

  2. BNUOJ 1038 Flowers

    春天到了,师大的园丁们又开始忙碌起来了. 京师广场上有一块空地,边界围成了一个多边形,内部被划分成一格一格的.园丁们想在这个多边形内的每一格内种植一些花. 现在请你帮忙计算一下一共最多可以种多少花. ...

  3. HDOJ并查集题目 HDOJ 1213 HDOJ 1242

    Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...

  4. 【Hibernate】Hibernate系列3之配置文件详解

    配置文件详解 3.1.配置文件 连接池性能优化:http://www.cnblogs.com/xdp-gacl/p/4002804.html

  5. for循环,pydev提示未使用的变量,解决办法

    对于如下代码,pvdev会产生未使用变量的警告 for i in range(5): func() 解决办法: 把变量替换成下划线_,就不会生产告警了.改变后如下: for _ in range(5) ...

  6. VC++遇到的错误汇集

    1.在程序头添加#include "stdafx.h" 和#include <afx.h>时会出现以下错误 在Win32Project下使用,出现“error C118 ...

  7. Delphi经验总结(2)

    Q: 怎么来改变ListBox的字体呢?就修改其中的一行. A: 先把ListBox1.Style 设成lbOwnerDrawFixed 然后在 OnDrawItem 事件下写下如下代码 proced ...

  8. [Android Memory] App调试内存泄露之Context篇(下)

    转载地址:http://www.cnblogs.com/qianxudetianxia/p/3655475.html 5. AsyncTask对象 我N年前去盛大面过一次试,当时面试官极力推荐我使用A ...

  9. 大端(big endian)和小端(little endian)

    http://www.cnblogs.com/Romi/archive/2012/01/10/2318551.html 当前的存储器,多以byte为访问的最小单元,当一个逻辑上的地址必须分割为物理上的 ...

  10. tar -cvPf new.tar `rpm -ql vsftpd` 建议不要用绝对路径'/'

    tar -cvPf new.tar `rpm -ql vsftpd` 解压这样的压缩包,会在当前用户的家目录下解压:~./xxxx;加参数-C :tar -xvf xxx.tar -C /  ;来指定 ...