题目链接

\(Description\)

有\(n\)个人都要参加考试,每个人可以在\(ai\)或\(bi\)天考试,同一天不能有两个人考试。求最晚考试的人的时间最早能是多少。无解输出-1。

\(Solution\)

把每个人向\(ai,bi\)连边。对于每个连通块单独考虑。

记点数为n,边数为m。可以发现当某一连通块n>m(n=m+1)时,可以有一个点不选(最大的);

当n=m时,所有点都要选;n<m时,无解。

用并查集维护连通,顺便维护最大、次大值。当第一次出现环时,即n=m;第二次出现就无解(n<m)了。

离散化可以写个Hash省掉log。

怎么这种题还是不去想连边。。

//795ms	41500KB
#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar()
//#define MAXIN 200000
//#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
const int N=2e6+5; int ref[N],A[N>>1],B[N>>1],fa[N],mx[N],smx[N];
bool tag[N];
//char IN[MAXIN],*SS=IN,*TT=IN; inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
inline int Find(int x,int r)
{
int l=1,mid;
while(l<r)
if(ref[mid=l+r>>1]<x) l=mid+1;
else r=mid;
return l;
}
inline int Getfa(int x)
{
return x==fa[x]?x:fa[x]=Getfa(fa[x]);
} int main()
{
int n=read(),t=0;
for(int i=1; i<=n; ++i) ref[++t]=A[i]=read(),ref[++t]=B[i]=read();
std::sort(ref+1,ref+1+t); int cnt=1;
for(int i=2; i<=t; ++i) if(ref[i]!=ref[i-1]) ref[++cnt]=ref[i]; for(int i=1; i<=cnt; ++i) fa[i]=i,mx[i]=ref[i];
for(int i=1,x,y,r1,r2; i<=n; ++i)
{
x=Find(A[i],cnt), y=Find(B[i],cnt);
if((r1=Getfa(x))!=(r2=Getfa(y)))
{
fa[r2]=r1, tag[r1]|=tag[r2];//!
if(mx[r2]>mx[r1]) smx[r1]=std::max(mx[r1],smx[r2]), mx[r1]=mx[r2];
else smx[r1]=std::max(smx[r1],mx[r2]);
}
else if(tag[r1]) return puts("-1"),0;
else tag[r1]=1;
}
int ans=0;
for(int i=1; i<=cnt; ++i)
if(fa[i]==i)
if(tag[i]) ans=std::max(ans,mx[i]);
else ans=std::max(ans,smx[i]);
printf("%d\n",ans); return 0;
}

Codeforces.1027F.Session in BSU(思路 并查集)的更多相关文章

  1. Codeforces 1027F. Session in BSU

    题目直通车:Codeforces 1027F. Session in BSU 思路: 对第一门考试,使用前一个时间,做标记,表示该时间已经用过,并让第一个时间指向第二个时间,表示,若之后的考试时间和当 ...

  2. Codeforces 1027F Session in BSU - 并查集

    题目传送门 传送门I 传送门II 传送门III 题目大意 有$n​$门科目有考试,第$i​$门科目有两场考试,时间分别在$a_i, b_i\ \ (a_i < b_i)​$,要求每门科目至少参加 ...

  3. Codeforces.1040E.Network Safety(思路 并查集)

    题目链接 \(Description\) 有一张\(n\)个点\(m\)条边的无向图,每个点有点权.图是安全的当且仅当所有边的两个端点权值不同.保证初始时图是安全的. 现在有权值为\(x\)的病毒,若 ...

  4. Codeforces 766D. Mahmoud and a Dictionary 并查集 二元敌对关系 点拆分

    D. Mahmoud and a Dictionary time limit per test:4 seconds memory limit per test:256 megabytes input: ...

  5. codeforces div2 603 D. Secret Passwords(并查集)

    题目链接:https://codeforces.com/contest/1263/problem/D 题意:有n个小写字符串代表n个密码,加入存在两个密码有共同的字母,那么说这两个密码可以认为是同一个 ...

  6. Codeforces Round #376 (Div. 2) C. Socks---并查集+贪心

    题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...

  7. CodeForces 698B Fix a Tree (并查集应用)

    当时也是想到了并查集,但是有几个地方没有想清楚,所以就不知道怎么写了,比如说如何确定最优的问题.赛后看了一下别人的思路,才知道自己确实经验不足,思维也没跟上. 其实没有那么复杂,这个题目我们的操作只有 ...

  8. Codeforces 977E:Cyclic Components(并查集)

    题意 给出nnn个顶点和mmm条边,求这个图中环的个数 思路 利用并查集的性质,环上的顶点都在同一个集合中 在输入的时候记录下来每个顶点的度数,查找两个点相连,且度数均为222的点,如果这两个点的父节 ...

  9. codeforces #541 D. Gourmet choice(拓扑+并查集)

    Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the wo ...

随机推荐

  1. Scala进阶之路-高级数据类型之数组的使用

    Scala进阶之路-高级数据类型之数组的使用 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.数组的初始化方式 1>.长度不可变数组Array 注意:顾名思义,长度不可变数 ...

  2. python创建与遍历List二维列表

    python创建与遍历List二维列表 觉得有用的话,欢迎一起讨论相互学习~Follow Me python 创建List二维列表 lists = [[] for i in range(3)] # 创 ...

  3. C#的Struct

  4. SQL语句(七)简单查询

    --简单信息查询 --例1 查询所有学生的信息 --学生 -- Student --所有学生 -- 不限定班级.性别.年龄等条件 --所有信息 -- 所有字段,* select * from stud ...

  5. .NET面试题系列(六)多线程

    1.多线程的三个特性:原子性.可见性.有序性 原子性:是指一个操作是不可中断的.即使是多个线程一起执行的时候,一个操作一旦开始,就不会被其他线程干扰. 比如,对于一个静态全局变量int i,两个线程同 ...

  6. [转]LaTex常用数学符号整理

    转载自 http://blog.csdn.net/ying_xu/article/details/51240291 (自己保存方便查阅,侵删) 另一个网站 Markdown 添加 Latex 数学公式 ...

  7. ACM数据对拍程序

    #include<cstdio> #include<cstdlib> #include<ctime> int main() { long s,t; while(1) ...

  8. perl6: Proc::Async (new)

    # command with arguments my $proc = Proc::Async.new('whoami'); # subscribe to new output from out an ...

  9. 二维码扫描开源库ZXing定制化【转】

    转自:http://www.cnblogs.com/sickworm/p/4562081.html 最近在用ZXing这个开源库做二维码的扫描模块,开发过程的一些代码修改和裁剪的经验和大家分享一下. ...

  10. USB协通讯议--深入理解【转】

    转自:http://blog.csdn.net/myarrow/article/details/8484113 0. 基本概念 一个[传输](控制.批量.中断.等时):由多个[事务]组成: 一个[事务 ...