[Luogu3425][POI2005]KOS-Dicing
题面戳这
题目描述
Dicing is a two-player game and its outcome is fully random. Lately its popularity increases all over Byteotia. There is even a special club for dicing amateurs in the capital city of Byteotia. The club patrons take their time talking to each other and playing their favourite game with a randomly chosen opponent every once in a while. Everyone who wins the most games one day gains the title of the lucky chap. Sometimes it happens that the night at the club is a quiet one and only few games are played. It is a time when even one win can make you a lucky chap.
Once upon a time a most unlucky fellow, Byteasar, won the glorious title. He was so deeply shocked that he completely forgot how many games he had won. Now he is wondering how good his luck was and whether fortune finally smiled upon him - perhaps his luck changed for good? He knows exactly how many games and between whom were played that lucky night. However, he does not know the results. Byteasar desires to find out what is the smallest number of wins that could provide the title of the lucky chap. Be a good fellow and help him satisfy his curiosity!
TaskWrite a programme that:
for each game played reads from the standard input the pair of players who competed in it.
finds the smallest number kkk, such that a set of games' outcomes exists in which each player wins kkk games at the most,writes the number kkk and the results of games in the found set to the standard output.
Dicing 是一个两人玩的游戏,这个游戏在Byteotia非常流行. 甚至人们专门成立了这个游戏的一个俱乐部. 俱乐部的人时常在一起玩这个游戏然后评选出玩得最好的人.现在有一个非常不走运的家伙,他想成为那个玩的最好的人,他现在知道了所有比赛的安排,他想知道,在最好的情况下,他最少只需要赢几场就可以赢得冠军,即他想知道比赛以后赢的最多的那个家伙最少会赢多少场.
输入输出格式
输入格式:
In the first line of the standard input there is a pair of integers nnn and mmm separated by a single space, 1≤n≤100001\le n\le 100001≤n≤10000, 0≤m≤100000\le m\le 100000≤m≤10000; nnn denotes the number of players, while mmm is the number of games. The players are numbered from 111 to nnn. In the following mmm lines there are pairs of players' numbers depicting the sequence of games, separated by single spaces. One pair may occur many times in the sequence.
输出格式:
The first line of the standard output should contain the determined number kkk. For each pair of players' numbers aaa, bbb specified in the iii'th line of the input, in the iii'th line of the output the number 111 should be written if the player no. aaa wins against player no. bbb in the found set of outcomes, or 000 otherwise.
输入输出样例
输入样例#1
4 4
1 2
1 3
1 4
1 2
输出样例#1
1
0
0
0
1
题解
给每个人,每场比赛都新建一个点
源点流向每个人,容量二分
每个人连向他参加的每一场比赛,容量为1
每场比赛流向汇点,容量为1
二分每次check最大流是否等于总比赛场数m
记得二分得到答案后还要在check一次答案用于输出方案
code
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
#define inf 1000000000
const int N = 10010;
struct edge{int to,next,w;}a[N<<4];
int n,m,s,t,l,r,A[N],B[N],head[N<<1],cnt,dep[N<<1],cur[N<<1],ans;
queue<int>Q;
void link(int u,int v,int w)
{
a[++cnt]=(edge){v,head[u],w};
head[u]=cnt;
a[++cnt]=(edge){u,head[v],0};
head[v]=cnt;
}
bool bfs()
{
memset(dep,0,sizeof(dep));
dep[s]=1;Q.push(s);
while (!Q.empty())
{
int u=Q.front();Q.pop();
for (int e=head[u];e;e=a[e].next)
if (!dep[a[e].to]&&a[e].w)
dep[a[e].to]=dep[u]+1,Q.push(a[e].to);
}
return dep[t];
}
int dfs(int u,int flow)
{
if (u==t)
return flow;
for (int &e=cur[u];e;e=a[e].next)
if (dep[a[e].to]==dep[u]+1&&a[e].w)
{
int temp=dfs(a[e].to,min(a[e].w,flow));
if (temp) {a[e].w-=temp;a[e^1].w+=temp;return temp;}
}
return 0;
}
bool check(int mid)
{
memset(head,0,sizeof(head));cnt=1;
for (int i=1;i<=n;i++)
link(s,i,mid);
for (int i=1;i<=m;i++)
link(A[i],i+n,1),link(B[i],i+n,1),link(i+n,t,1);
ans=0;
while (bfs())
{
for (int i=t;i;i--) cur[i]=head[i];
while (int temp=dfs(s,inf)) ans+=temp;
}
return ans==m;
}
int main()
{
scanf("%d%d",&n,&m);s=n+m+1;t=s+1;
for (int i=1;i<=m;i++)
scanf("%d%d",&A[i],&B[i]);
l=0;r=m;
while (l<r)
{
int mid=l+r>>1;
if (check(mid)) r=mid;
else l=mid+1;
}
printf("%d\n",l);check(l);
for (int i=1;i<=m;i++)
for (int e=head[i+n];e;e=a[e].next)
if (a[e].w) puts(a[e].to==A[i]?"1":"0");
return 0;
}
[Luogu3425][POI2005]KOS-Dicing的更多相关文章
- 【BZOJ】【1532】【POI2005】Kos-Dicing
网络流/二分法 最大值最小……直接做不太好做的时候就可以用二分+判定来搞. 这题我们就也可以二分最大胜场v,那么怎么来判定呢?首先我们发现:每场比赛要么A赢,要么B赢,这一点跟二分图匹配非常类似,那么 ...
- Bzoj 1532: [POI2005]Kos-Dicing 二分,网络流
1532: [POI2005]Kos-Dicing Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1373 Solved: 444[Submit][St ...
- BZOJ1532: [POI2005]Kos-Dicing
1532: [POI2005]Kos-Dicing Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1060 Solved: 321[Submit][St ...
- bzoj [POI2005]Kos-Dicing 二分+网络流
[POI2005]Kos-Dicing Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1835 Solved: 661[Submit][Status][ ...
- BZOJ_1532_[POI2005]Kos-Dicing_二分+网络流
BZOJ_1532_[POI2005]Kos-Dicing_二分+网络流 Description Dicing 是一个两人玩的游戏,这个游戏在Byteotia非常流行. 甚至人们专门成立了这个游戏的一 ...
- bzoj 1537: [POI2005]Aut- The Bus 线段树
bzoj 1537: [POI2005]Aut- The Bus 先把坐标离散化 设f[i][j]表示从(1,1)走到(i,j)的最优解 这样直接dp::: f[i][j] = max{f[i-1][ ...
- [BZOJ1529][POI2005]ska Piggy banks
[BZOJ1529][POI2005]ska Piggy banks 试题描述 Byteazar 有 N 个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. Byteazar 已经把每个存钱罐的钥匙放 ...
- BZOJ1533: [POI2005]Lot-A Journey to Mars
1533: [POI2005]Lot-A Journey to Mars Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 174 Solved: 76[S ...
- BZOJ1528: [POI2005]sam-Toy Cars
1528: [POI2005]sam-Toy Cars Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 282 Solved: 129[Submit][S ...
随机推荐
- js到底new了点啥
在最开始学习js的时候,我看书上写着,创建一个数组,一个对象通常使用new,如下: var arr=new Array(),//arr=[] obj=new Object();//obj={} 到了后 ...
- 接口测试基础(fiddler、postman的使用、python实现测试接口程序)
写在前面:本文主要的章节规划: 1.什么是接口测试 另外,有的时候会直接调用别的公司的接口,比如银行的.淘宝的.支付宝的,此时也需要做接口测试以及验证数据: 做接口测试的好处: 其中, ...
- linux中权限对文件和目录的作用
chmod 755 a.txt 文件: r:读取文件内容(cat more head tail) w:编辑,新增,修改文件的内容(vi,echo) 不包括删除文件:原因是只能对文件内容进行修改,而在l ...
- 用VSCode开发一个基于asp.net core 2.0/sql server linux(docker)/ng5/bs4的项目(2)
第一部分: http://www.cnblogs.com/cgzl/p/8478993.html 为Domain Model添加约束 前一部分, 我们已经把数据库创建出来了. 那么我们先看看这个数据库 ...
- TP3.2 中使用 PHPMailer 发送邮件
第一步.添加PHPMailer类库 http://pan.baidu.com/s/1o7Zc7V0 第二步.添加发送邮件函数 在common目录中的公共函数文件加入函数 <?php /***** ...
- AGC010 - B: Boxes
原题链接 题意简述 给出一个由个数构成的环,每次可以选择一个位置并从这个数起顺时针依次对每个数-1,-2,-3,-,-n.问能否将所有数全变为0. 分析 考虑一次操作对环带来了什么影响. (在后加一个 ...
- Linux sftp 安全文件传输命令
sftp 是一个交互式文件传输程式.它类似于 ftp, 但它进行加密传输,比FTP有更高的安全性. 1.常用登陆方式: 格式:sftp <user>@<host> 通过sftp ...
- [Code] 中缀式转后缀式
[Code] 中缀式转后缀式 概要 对于一个可带括号的中缀四则运算表达式, 例如30 + 4 / 2 或 30 / ( 4 + 2 ), 下面代码将分别转换为对应的后缀表达形式 30 4 2 / + ...
- Centos搭建mysql/Hadoop/Hive/Hbase/Sqoop/Pig
目录: 准备工作 Centos安装 mysql Centos安装Hadoop Centos安装hive JDBC远程连接Hive Hbase和hive整合 Centos安装Hbase 准备工作: 配置 ...
- CPLD/FPGA厂商概述 .
随着可编程逻辑器件应用的日益广泛,许多IC制造厂家涉足PLD/FPGA领域.目前世界上有十几家生产CPLD/FPGA的公司,最大的三家是:ALTERA,XILINX,Lattice,其中ALTERA和 ...