题目链接:
URAL 1997 Those are not the droids you’re looking for
题意:
给出一间酒吧的n个进出记录,判断进出这间酒吧的所有人是不是只属于这两种:
①:待在酒吧的时间不少于a
②:待在酒吧的时间至多为b
如果存在一种方案使得根据这n个进出记录的到的所有人都属于这两种人输出每个人的进出时间。否则输出”Liar”。
分析:
二分图匹配。
进入时间和出去时间的匹配。如果满足上述条件,就增加一条增广轨。判断最大匹配是否恰好为完全匹配。
特别注意:出去时间一定是大于进入时间的!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <cassert>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
typedef long long ll;
const int MAX_N = 1010;
int n, ids, ide, total;
ll a, b, s[MAX_N], e[MAX_N];
int matchx[MAX_N], matchy[MAX_N], vis[MAX_N], head[MAX_N];
struct Edge{
int to, next;
Edge() {}
Edge(int _to, int _next) :to(_to), next(_next) {}
}edge[MAX_N * MAX_N];
void AddEdge(int from, int to)
{
edge[total].to = to;
edge[total].next = head[from];
head[from] = total++;
}
bool dfs(int u)
{
for(int i = head[u]; i != -1; i = edge[i].next){
int v = edge[i].to;
if(vis[v]) continue;
vis[v] = 1;
if(matchy[v] == -1 || dfs(matchy[v])){
matchx[u] = v;
matchy[v] = u;
return true;
}
}
return false;
}
int Hungary()
{
memset(matchx, -1, sizeof(matchx));
memset(matchy, -1, sizeof(matchy));
int res = 0;
for(int i = 0; i < ids; i++){
memset(vis, 0, sizeof(vis));
if(dfs(i)) res++;
}
return res;
}
int main()
{
freopen("H.in", "r", stdin);
IOS;
while(cin >> a >> b){
cin >> n;
memset(head, -1, sizeof(head));
total = ids = ide = 0;
for(int i = 0; i < n; i++){
ll t;
int id;
cin >> t >> id;
if(id == 0) s[ids++] = t;
else e[ide++] = t;
}
for(int i = 0; i < ids; i++){
for(int j = 0; j < ide; j++){
if(e[j] <= s[i]) continue;
if(s[i] + b >= e[j] || s[i] + a <= e[j]){
AddEdge(i, j);
}
}
}
int ans = Hungary();
//cout << ans << endl;
if(ids != ide && n & 1 || ans < n / 2) cout << "Liar" << endl;
else {
cout << "No reason" << endl;
for(int i = 0; i < ids; i++){
cout << s[i] << " " << e[matchx[i]] << endl;
}
}
}
return 0;
}