Timus OJ 1997 Those are not the droids you're looking for (二分匹配)

时间:2023-03-09 07:30:48
Timus OJ 1997 Those are not the droids you're looking for (二分匹配)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1997

这个星球上有两种人,一种进酒吧至少玩a小时,另一种进酒吧最多玩b小时。

下面n行是人进进出出的时刻,0为进,1为出。让你求是否有合法解。

将合法的进入和出去连边,然后二分匹配就可以了。

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 1e3 + ;
struct data {
int a , b;
bool operator <(const data &cmp) const {
return a < cmp.a;
}
}xx[N];
vector <int> G[N];
vector <int> vc;
int match[N];
bool vis[N]; bool dfs(int u) {
for(int i = ; i < G[u].size() ; ++i) {
int v = G[u][i];
if(!vis[v]) {
vis[v] = true;
if(match[v] == - || dfs(match[v])) {
match[v] = u;
match[u] = v;
return true;
}
}
}
return false;
} bool hungry() {
int res = ;
for(int i = ; i < vc.size() ; ++i) {
memset(vis , false , sizeof(vis));
if(dfs(vc[i]))
res++;
}
if(vc.size() == res)
return true;
return false;
} void solve() {
memset(match , - , sizeof(match));
if(hungry()) {
printf("No reason\n");
for(int i = ; i < vc.size() ; ++i) {
printf("%d %d\n" , xx[match[vc[i]]].a , xx[vc[i]].a);
}
}
else {
printf("Liar\n");
}
} int main()
{
int x , y , n;
while(~scanf("%d %d" , &x , &y)) {
scanf("%d" , &n);
int index = ;
for(int i = ; i <= n ; ++i) {
scanf("%d %d" , &xx[i].a , &xx[i].b);
}
sort(xx + , xx + n + );
for(int i = ; i <= n ; ++i) {
if(xx[i].b) {
vc.push_back(i);
for(int j = ; j < i ; ++j) {
if(!xx[j].b && (xx[i].a - xx[j].a <= y || xx[i].a - xx[j].a >= x))
G[i].push_back(j);
}
}
}
solve();
}
return ;
}