[Codeforces Round49F] Session in BSU
[题目链接]
http://codeforces.com/contest/1027/problem/F
[算法]
二分图匹配
[代码]
#include<bits/stdc++.h>
#pragma GOC optimize("Ofast")
using namespace std;
const int MAXN = 1e6 + ; struct edge
{
int to,nxt;
} e[MAXN << ]; int n,q,len,ans,tot;
int a[MAXN],b[MAXN],ta[MAXN],tb[MAXN],tmp[MAXN << ],match[MAXN << ],head[MAXN << ],visited[MAXN << ]; template <typename T> inline void read(T &x)
{
int f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline void addedge(int u,int v)
{
tot++;
e[tot] = (edge){v,head[u]};
head[u] = tot;
}
inline bool hungary(int u,int k)
{
int v;
for (int i = head[u]; i; i = e[i].nxt)
{
v = e[i].to;
if (visited[v] != k)
{
visited[v] = k;
if (!match[v] || hungary(match[v],k))
{
match[v] = u;
return true;
}
}
}
return false;
} int main()
{ read(n);
for (int i = ; i <= n; i++)
{
read(a[i]); read(b[i]);
tmp[++len] = a[i]; tmp[++len] = b[i];
}
sort(tmp + ,tmp + len + );
len = unique(tmp + ,tmp + len + ) - tmp - ;
for (int i = ; i <= n; i++)
{
ta[i] = lower_bound(tmp + ,tmp + len + ,a[i]) - tmp;
tb[i] = lower_bound(tmp + ,tmp + len + ,b[i]) - tmp;
}
for (int i = ; i <= n; i++)
{
addedge(ta[i],len + i);
addedge(tb[i],len + i);
}
for (int i = ; i <= len; i++)
{
if (hungary(i,i))
{
ans++;
if (ans == n)
{
printf("%d\n",tmp[i]);
return ;
}
}
}
printf("-1\n"); return ; }
[Codeforces Round49F] Session in BSU的更多相关文章
- Codeforces 1027F. Session in BSU
题目直通车:Codeforces 1027F. Session in BSU 思路: 对第一门考试,使用前一个时间,做标记,表示该时间已经用过,并让第一个时间指向第二个时间,表示,若之后的考试时间和当 ...
- Codeforces 1027F Session in BSU - 并查集
题目传送门 传送门I 传送门II 传送门III 题目大意 有$n$门科目有考试,第$i$门科目有两场考试,时间分别在$a_i, b_i\ \ (a_i < b_i)$,要求每门科目至少参加 ...
- Codeforces.1027F.Session in BSU(思路 并查集)
题目链接 \(Description\) 有\(n\)个人都要参加考试,每个人可以在\(ai\)或\(bi\)天考试,同一天不能有两个人考试.求最晚考试的人的时间最早能是多少.无解输出-1. \(So ...
- CF 1027 F. Session in BSU
F. Session in BSU https://codeforces.com/contest/1027/problem/F 题意: n场考试,每场可以安排在第ai天或者第bi天,问n场考完最少需要 ...
- codeforces1027F. Session in BSU
题目链接 codeforces1027F. Session in BSU 题解 二分图匹配就fst了....显然是过去的,不过tle test87估计也pp了,好坑 那么对于上面做匹配的这个二分图分情 ...
- Session in BSU CodeForces - 1027F(思维 树 基环树 离散化)
题意: 有n门考试,每门考试都有两个时间,存在几门考试时间冲突,求考完所有的考试,所用的最后时间的最小值 解析: 对于时间冲突的考试 就是一个联通块 把每个考试看作边,两个时间看作点,那么时间冲突的考 ...
- [Codeforces 1027 F] Session in BSU [并查集维护二分图匹配问题]
题面 传送门 思路 真是一道神奇的题目呢 题目本身可以转化为二分图匹配问题,要求右半部分选择的点的最大编号最小的一组完美匹配 注意到这里左边半部分有一个性质:每个点恰好连出两条边到右半部分 那么我们可 ...
- [CF1027F]Session in BSU[最小基环树森林]
题意 有 \(n\) 门课程,每门课程可以选择在 \(a_i\) 或者 \(b_i\) 天参加考试,每天最多考一门,问最早什么时候考完所有课程. \(n\leq 10^6\). 分析 类似 [BZOJ ...
- CF1027F Session in BSU
link 花絮: 这场看起来打得还不错的样子……(别问我是用哪个号打的). 然后听说这题的思想被出了好多次,女生赛也出过,quailty算法,然而当时没反应过来,而且时间不多啦. 题意: 有n个人,每 ...
随机推荐
- 梦想CAD控件文字COM接口知识点
一.参数绘制文字 在CAD设计时,需要绘制文字,用户可以设置设置绘制文字的高度等属性. 主要用到函数说明: _DMxDrawX::DrawText 绘制一个单行文字.详细说明如下: 参数 说明 DOU ...
- apache 添加虚拟机
<VirtualHost *:80> DocumentRoot "E:/UPUPW_AP7.0/htdocs/xd.local/public" ServerName a ...
- Django DTL模板语法中的循环
from django.shortcuts import render def index(request): context={ 'books':[ '5年高考3年模拟', '家猪养殖与配种', ' ...
- ACM多校联赛7 2018 Multi-University Training Contest 7 1009 Tree
[题意概述] 给一棵以1为根的树,树上的每个节点有一个ai值,代表它可以传送到自己的ai倍祖先,如果不存在则传送出这棵树.现在询问某个节点传送出这棵树需要多少步. [题解] 其实是把“弹飞绵羊”那道题 ...
- List lambda 排序
Comparator<PromotionRule> comparator = Comparator.comparing(PromotionRule::getCreatedTime); pr ...
- POJ 2217 Secretary
Secretary Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...
- HashMap源码分析3:移除
本文源码基于JDK1.8.0_45. final Node<K,V> removeNode(int hash, Object key, Object value, boolean matc ...
- Eclipse查看方法/类调用的方法
1.(首推)双击选中该方法/类,[Ctrl]+[Alt]+[H](Open Call Hierarchy) 2.(次推)选中该方法/类,[Ctrl]+[Shift]+[G](References) 3 ...
- WINDOW 专家
http://www.cnblogs.com/shanyou/category/725986.html
- oracle em 5500访问问题
oracle em 5500访问问题 需要加s了:https://127.0.0.1:5500/em/