题面戳这

题目描述

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

  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<cstring>
  4. #include<queue>
  5. using namespace std;
  6. #define inf 1000000000
  7. const int N = 10010;
  8. struct edge{int to,next,w;}a[N<<4];
  9. int n,m,s,t,l,r,A[N],B[N],head[N<<1],cnt,dep[N<<1],cur[N<<1],ans;
  10. queue<int>Q;
  11. void link(int u,int v,int w)
  12. {
  13. a[++cnt]=(edge){v,head[u],w};
  14. head[u]=cnt;
  15. a[++cnt]=(edge){u,head[v],0};
  16. head[v]=cnt;
  17. }
  18. bool bfs()
  19. {
  20. memset(dep,0,sizeof(dep));
  21. dep[s]=1;Q.push(s);
  22. while (!Q.empty())
  23. {
  24. int u=Q.front();Q.pop();
  25. for (int e=head[u];e;e=a[e].next)
  26. if (!dep[a[e].to]&&a[e].w)
  27. dep[a[e].to]=dep[u]+1,Q.push(a[e].to);
  28. }
  29. return dep[t];
  30. }
  31. int dfs(int u,int flow)
  32. {
  33. if (u==t)
  34. return flow;
  35. for (int &e=cur[u];e;e=a[e].next)
  36. if (dep[a[e].to]==dep[u]+1&&a[e].w)
  37. {
  38. int temp=dfs(a[e].to,min(a[e].w,flow));
  39. if (temp) {a[e].w-=temp;a[e^1].w+=temp;return temp;}
  40. }
  41. return 0;
  42. }
  43. bool check(int mid)
  44. {
  45. memset(head,0,sizeof(head));cnt=1;
  46. for (int i=1;i<=n;i++)
  47. link(s,i,mid);
  48. for (int i=1;i<=m;i++)
  49. link(A[i],i+n,1),link(B[i],i+n,1),link(i+n,t,1);
  50. ans=0;
  51. while (bfs())
  52. {
  53. for (int i=t;i;i--) cur[i]=head[i];
  54. while (int temp=dfs(s,inf)) ans+=temp;
  55. }
  56. return ans==m;
  57. }
  58. int main()
  59. {
  60. scanf("%d%d",&n,&m);s=n+m+1;t=s+1;
  61. for (int i=1;i<=m;i++)
  62. scanf("%d%d",&A[i],&B[i]);
  63. l=0;r=m;
  64. while (l<r)
  65. {
  66. int mid=l+r>>1;
  67. if (check(mid)) r=mid;
  68. else l=mid+1;
  69. }
  70. printf("%d\n",l);check(l);
  71. for (int i=1;i<=m;i++)
  72. for (int e=head[i+n];e;e=a[e].next)
  73. if (a[e].w) puts(a[e].to==A[i]?"1":"0");
  74. return 0;
  75. }

[Luogu3425][POI2005]KOS-Dicing的更多相关文章

  1. 【BZOJ】【1532】【POI2005】Kos-Dicing

    网络流/二分法 最大值最小……直接做不太好做的时候就可以用二分+判定来搞. 这题我们就也可以二分最大胜场v,那么怎么来判定呢?首先我们发现:每场比赛要么A赢,要么B赢,这一点跟二分图匹配非常类似,那么 ...

  2. Bzoj 1532: [POI2005]Kos-Dicing 二分,网络流

    1532: [POI2005]Kos-Dicing Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1373  Solved: 444[Submit][St ...

  3. BZOJ1532: [POI2005]Kos-Dicing

    1532: [POI2005]Kos-Dicing Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1060  Solved: 321[Submit][St ...

  4. bzoj [POI2005]Kos-Dicing 二分+网络流

    [POI2005]Kos-Dicing Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1835  Solved: 661[Submit][Status][ ...

  5. BZOJ_1532_[POI2005]Kos-Dicing_二分+网络流

    BZOJ_1532_[POI2005]Kos-Dicing_二分+网络流 Description Dicing 是一个两人玩的游戏,这个游戏在Byteotia非常流行. 甚至人们专门成立了这个游戏的一 ...

  6. 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][ ...

  7. [BZOJ1529][POI2005]ska Piggy banks

    [BZOJ1529][POI2005]ska Piggy banks 试题描述 Byteazar 有 N 个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. Byteazar 已经把每个存钱罐的钥匙放 ...

  8. 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 ...

  9. BZOJ1528: [POI2005]sam-Toy Cars

    1528: [POI2005]sam-Toy Cars Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 282  Solved: 129[Submit][S ...

随机推荐

  1. NoSQLBooster for MongoDB的基本使用

    连接 File -> Quik Connect ( Ctrl + Shift + N ) 或 Connect -> From URI 填入 mongodb://username:passw ...

  2. APACHE服务器出现No input file specified.解决方案

    thinkcmf程序默认的.htaccess里面的规则: <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_F ...

  3. Head First C 笔记

    嗨翻c语言 1. 入门 为什么字符从零开始编号? 字符的索引数值表示的是一个偏移量,它表示的是当前所引用的字符与第一个字符之间差多少个字符. 单双引号的区别? 单引号 一个字符,双- 字符串 字符串字 ...

  4. JDBC 基础

    JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口 ...

  5. volatile简要解析

    在当前的Java内存模型下,线程可以把变量保存在本地内存(比如机器的寄存器)中,而不是直接在主存中进行读写.这就可能造成一个线程在主存中修改了一个变量的值,而另外一个线程还继续使用它在寄存器中的变量值 ...

  6. CSS布局(五) 网页布局方式

    网页实质是块与块之间的位置,块挨着块,块嵌套块,块叠着块. 三种关系:相邻,嵌套,重叠. 下面介绍网页布局的常用几种方式 1.一列布局: 一般都是固定的宽高,设置margin : 0 auto来水平居 ...

  7. 使用Git的hook实现代码的自动部署

    这个功能非常的好用,可以省去诸多麻烦!我自己也是摸索了好久,才完全掌握的.希望能对大家有所帮助! 1,首先在我的阿里云服务器上已经创建好了一个代码远程的管理仓库,/srv/cmp.git 2, 在服务 ...

  8. PHP秒杀系统全方位设计(二)

    商品页面开发 静态化展示页面[效率要比动态PHP高很多,PHP程序需要解析等步骤,本身就需要很多流程,整个下来PHP的处理花的时间和资源要多] 商品状态的控制 开始前.进行中.库存不足.结束 数据逻辑 ...

  9. hihoCoder 1288 Font Size 二分

    题意:给定一个宽度为和高度为的屏幕,如果字体的大小为,那么一行可以显示个字,每一页可以显示行.给出段文本段落,每段有个文字,问现在能设置的最大字体并且总的页数不能超过? 思路:如果知道字体大小很容易求 ...

  10. nyoj44 子串和 线性DP

    线性DP经典题. dp[i]表示以i为结尾最大连续和,状态转移方程dp[i] = max (a[i] , dp[i - 1] + a[i]) AC代码: #include<cstdio> ...