URAL 1997 Those are not the droids you're looking for
二分图的最大匹配。
每一个$0$与$1$配对,只建立满足时差大于等于$a$或者小于等于$b$的边,如果二分图最大匹配等于$n/2$,那么有解,遍历每一条边输出答案,否则无解。
#include<map>
#include<set>
#include<ctime>
#include<cmath>
#include<queue>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define ms(x,y) memset(x,y,sizeof(x))
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define per(i,j,k) for(int i=j;i>=k;i--)
#define loop(i,j,k) for (int i=j;i!=-1;i=k[i])
#define inone(x) scanf("%d",&x)
#define intwo(x,y) scanf("%d%d",&x,&y)
#define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define infou(x,y,z,p) scanf("%d%d%d%d",&x,&y,&z,&p)
#define lson x<<1,l,mid
#define rson x<<1|1,mid+1,r
#define mp(i,j) make_pair(i,j)
#define ft first
#define sd second
typedef long long LL;
typedef pair<int, int> pii;
const int low(int x) { return x&-x; }
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + ;
const int N = 1e6 + ;
const int M = 1e4 + ;
const double eps = 1e-; int a,b,n;
int T[],f[]; const int maxn = + ;
struct Edge
{
int from, to, cap, flow;
Edge(int u, int v, int c, int f) :from(u), to(v), cap(c), flow(f){}
};
vector<Edge>edges;
vector<int>G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
int s, t; void init()
{
for (int i = ; i < maxn; i++)
G[i].clear();
edges.clear();
}
void AddEdge(int from, int to, int cap)
{
edges.push_back(Edge(from, to, cap, ));
edges.push_back(Edge(to, from, , ));
int w = edges.size();
G[from].push_back(w - );
G[to].push_back(w - );
}
bool BFS()
{
memset(vis, , sizeof(vis));
queue<int>Q;
Q.push(s);
d[s] = ;
vis[s] = ;
while (!Q.empty())
{
int x = Q.front();
Q.pop();
for (int i = ; i<G[x].size(); i++)
{
Edge e = edges[G[x][i]];
if (!vis[e.to] && e.cap>e.flow)
{
vis[e.to] = ;
d[e.to] = d[x] + ;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x, int a)
{
if (x == t || a == )
return a;
int flow = , f;
for (int &i = cur[x]; i<G[x].size(); i++)
{
Edge e = edges[G[x][i]];
if (d[x]+ == d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>)
{
edges[G[x][i]].flow+=f;
edges[G[x][i] ^ ].flow-=f;
flow+=f;
a-=f;
if(a==) break;
}
}
if(!flow) d[x] = -;
return flow;
}
int dinic(int s, int t)
{
int flow = ;
while (BFS())
{
memset(cur, , sizeof(cur));
flow += DFS(s, INF);
}
return flow;
} int main()
{
while(~scanf("%d%d",&a,&b))
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d%d",&T[i],&f[i]); init(); for(int i=;i<=n;i++)
{
if(f[i]==) continue;
for(int j=i+;j<=n;j++)
{
if(f[j]==) continue;
if(T[j]-T[i]>=a||T[j]-T[i]<=b)
{
AddEdge(i,j,);
}
}
} s=,t=n+; for(int i=;i<=n;i++)
{
if(f[i]==) AddEdge(s,i,);
else AddEdge(i,t,);
} int M = dinic(s,t); if(M<n/)
{
printf("Liar\n");
}
else
{
printf("No reason\n");
for(int i=;i<edges.size();i++)
{
if(edges[i].flow!=) continue;
if(edges[i].from!=s&&edges[i].to!=t)
printf("%d %d\n",T[edges[i].from],T[edges[i].to]);
}
} }
return ;
}
URAL 1997 Those are not the droids you're looking for的更多相关文章
- URAL 1997 Those are not the droids you're looking for 二分图最大匹配
Those are not the droids you're looking for 题目连接: http://acm.timus.ru/problem.aspx?space=1&num=1 ...
- 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行是 ...
- Ural 1011. Conductors
1011. Conductors Time limit: 2.0 secondMemory limit: 64 MB Background Everyone making translations f ...
- HDU 3062 && HDU 1824 && POJ 3678 && BZOJ 1997 2-SAT
一条边<u,v>表示u选那么v一定被选. #include <iostream> #include <cstring> #include <cstdio> ...
- [BZOJ 1997][HNOI2010]Planar(2-SAT)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1997 分析: 考虑每条边是在圈子里面还是圈子外面 所以就变成了2-SAT判定问题了= ...
- 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome
题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...
- ural 2071. Juice Cocktails
2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...
- ural 2073. Log Files
2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...
- ural 2070. Interesting Numbers
2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...
随机推荐
- 修改Tomcat端口
1.自己的tomcat修改端口号,在tomcat解压缩目录中conf目录下又一个server.xml,将HTTP协议的端口修改为自己想要的即可 <Connector port="808 ...
- DES解码
DES加解密算法是一个挺老的算法了,现在给出它的C语言版. des.h #ifdef __cplusplus extern "C" { #endif ]); char* des(c ...
- Asp.Net MVC 自定义登录过滤器
1.新建类BaseController用于统一所有控制器继承扩展,方便扩展登录等过滤器.示例如下: using CloudWave.JustBeHere.JBH_H5.Controllers.Attr ...
- 将new Date() 格式化为 ’2018-10-11‘ 的字符串格式
function dateToString( date , format ){ if(!date) return ""; if (!Common.type.isDate(date) ...
- IO流-文件拷贝
其实文件的拷贝还是文件读取写入的应用,实际是读取此路径上的文件,然后写入到指定路径下的文件. 代码举例: import java.io.*; import java.lang.*; class Tes ...
- phpcms取内容发布管理中的来源
调取内容发布管理中的来源,如果直接写{$val['copyfrom']}调取出来的内容为 内容|0 ,要先根据“|”进行拆分,然后再写. 示例: <!--新闻开始--> {pc:co ...
- centOS7 vsftp ExecStart=/usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf (code=exited, status=0/SUCCESS) 启动失败问题?
[root@localhost c]# systemctl status vsftpd.service ● vsftpd.service - Vsftpd ftp daemon Loaded: loa ...
- CF148A Insomnia cure
公主睡前数龙, 每隔k, l, m, n只都会用不同的技能攻击龙. 假定共数了d只龙, 问共有多少龙被攻击了. 思路: 用一个visit数组记录被攻击过的dragon, 最后遍历visit数组统计被攻 ...
- windows安装linux虚拟机、修改apt源
记录一下windows安装虚拟机以及初始配置的一些坑. 安装VMware Workstation 直接百度搜索VMware,选择合适的版本下载: 按照一般软件的安装步骤安装VMware Worksta ...
- LCD常用接口原理【转】
转自:http://blog.csdn.net/wocao1226/article/details/23870149 LCD常用接口原理 点击打开链接 点击打开链接 点击打开链接 点击打开链接 点击打 ...