codeforce 1027 F. Session in BSU

F. Session in BSU

time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Polycarp studies in Berland State University. Soon he will have to take his exam. He has to pass exactly nn exams.

For the each exam ii there are known two days: aiai — day of the first opportunity to pass the exam, bibi — day of the second opportunity to pass the exam (ai<biai<bi). Polycarp can pass at most one exam during each day. For each exam Polycarp chooses by himself which day he will pass this exam. He has to pass all the nn exams.

Polycarp wants to pass all the exams as soon as possible. Print the minimum index of day by which Polycarp can pass all the nn exams, or print -1 if he cannot pass all the exams at all.

Input

The first line of the input contains one integer nn (1≤n≤1061≤n≤106) — the number of exams.

The next nn lines contain two integers each: aiai and bibi (1≤ai<bi≤1091≤ai<bi≤109), where aiai is the number of day of the first passing the ii-th exam and bibi is the number of day of the second passing the ii-th exam.

Output

If Polycarp cannot pass all the nn exams, print -1. Otherwise print the minimum index of day by which Polycarp can do that.

思路:
每个a,b之间连一条边,最后形成多个连通图,
是一颗树的即edge==v+1,最大的点不选,若是
环即edge==v,则所有的点都选,若edge>v则-1。
代码:
#include<bits/stdc++.h>
using namespace std;
#define cherry main
const int maxn=2e6+10;
int par[maxn],num[maxn];
int a[maxn],b[maxn],c[maxn];
vector<int>G[maxn];
int Find(int x)
{
    if(x==par[x]) return x;
    return par[x]=Find(par[x]);
}
void unite(int x,int y)
{
    x=Find(x);y=Find(y);
    num[x]++;
    if(x!=y)
    {
        par[x]=y;
        num[y]+=num[x];
        num[x]=0;
    }
}
int cherry()
{
    for(int i=0;i<maxn;i++) par[i]=i;
    int n;scanf("%d",&n);
    int len=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&a[i],&b[i]);
        c[++len]=a[i];c[++len]=b[i];
    }
    sort(c+1,c+len+1);
    len=unique(c+1,c+len+1)-c-1;
    for(int i=1;i<=n;i++)
    {
        a[i]=upper_bound(c+1,c+len+1,a[i])-c-1;
        b[i]=upper_bound(c+1,c+len+1,b[i])-c-1;
    }
    for(int i=1;i<=n;i++) unite(a[i],b[i]);
    for(int i=1;i<=len;i++) G[Find(i)].push_back(i);
    bool bb=1;
    int ans=0;
    for(int i=1;i<=len;i++)
    {
        int SIZE=G[i].size();
        if(!SIZE) continue;
        sort(G[i].begin(),G[i].end());
        if(num[i]==SIZE) ans=max(ans,G[i][SIZE-1]);
        else if(num[i]==SIZE-1) ans=max(ans,G[i][SIZE-2]);
        else bb=0;
    }
    c[0]=-1;
    if(!bb) ans=0;
    printf("%d\n",c[ans]);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值