当前位置:   article > 正文

POJ 2699 最大流 竞赛图_竞赛图最大流

竞赛图最大流
The Maximum Number of Strong Kings
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1842 Accepted: 862

Description

A tournament can be represented by a complete graph in which each vertex denotes a player and a directed edge is from vertex x to vertex y if player x beats player y. For a player x in a tournament T, the score of x is the number of players beaten by x. The score sequence of T, denoted by S(T) = (s1, s2, . . . , sn), is a non-decreasing list of the scores of all the players in T. It can be proved that S(T) = (s1, s2, . . . , sn) is a score sequence of T if and only if  
for k = 1, 2, . . . , n and equality holds when k = n. A player x in a tournament is a strong king if and only if x beats all of the players whose scores are greater than the score of x. For a score sequence S, we say that a tournament T realizes S if S(T) = S. In particular, T is a heavy tournament realizing S if T has the maximum number of strong kings among all tournaments realizing S. For example, see T2 in Figure 1. Player a is a strong king since the score of player a is the largest score in the tournament. Player b is also a strong king since player b beats player a who is the only player having a score larger than player b. However, players c, d and e are not strong kings since they do not beat all of the players having larger scores. 
The purpose of this problem is to find the maximum number of strong kings in a heavy tournament after a score sequence is given. For example,Figure 1 depicts two possible tournaments on five players with the same score sequence (1, 2, 2, 2, 3). We can see that there are at most two strong kings in any tournament with the score sequence (1, 2, 2, 2, 3) since the player with score 3 can be beaten by only one other player. We can also see that T2 contains two strong kings a and b. Thus, T2 is one of heavy tournaments. However, T1 is not a heavy tournament since there is only one strong king in T1. Therefore, the answer of this example is 2. 
 

Input

The first line of the input file contains an integer m, m <= 10, which represents the number of test cases. The following m lines contain m score sequences in which each line contains a score sequence. Note that each score sequence contains at most ten scores.

Output

The maximum number of strong kings for each test case line by line.

Sample Input

5
1 2 2 2 3
1 1 3 4 4 4 4
3 3 4 4 4 4 5 6 6 6
0 3 4 4 4 5 5 5 6
0 3 3 3 3 3

Sample Output

2
4
5
3
5


题意是给你他们每个人的胜场数,问有多少最强竞赛者(指的如果胜场是最多的。或者赢了所有胜利数比他多的)


本来想二分胜利场数,后来想想不对。需要二分胜利人数


然后是建图:


超级源点连向人的流量是人的胜利场数


比赛连向超级汇点的流量是1


人连向比赛的流量是1  这里需要考虑如果这个人比另外一个人比赛场数低,且也在最强者里面,那么这把比赛他必须赢,所以只能连这个人到比赛的连线,其他的话从这两个人,各连一条到比赛,代表他们都能够去赢这场比赛。


因为数据比较小,我直接暴力枚举了。。并没有用二分。。。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <climits>
  4. #include <cstring>
  5. #include <cstdlib>
  6. #include <cmath>
  7. #include <vector>
  8. #include <queue>
  9. #include <algorithm>
  10. #define esp 1e-6
  11. #define inf 0x0f0f0f0f
  12. #define LL long long
  13. using namespace std;
  14. const int MAXN = 100010 ; //点数最大值
  15. const int MAXM = 400010 ; //边数最大值
  16. const int INF = 0x3f3f3f3f;
  17. int M,TOL,S,V;
  18. struct Edge{
  19. int to,next,cap,flow;
  20. }edge[MAXM];//注意是MAXM
  21. int tol;
  22. int head[MAXN];
  23. int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];
  24. //int imap[333][333];
  25. int b[13],co[13][13];
  26. int cmp(int a,int b){
  27. return a>b;
  28. }
  29. void init(){
  30. tol = 0;
  31. memset(head,-1,sizeof(head));
  32. }
  33. void addedge(int u,int v,int w,int rw=0){
  34. edge[tol].to = v;
  35. edge[tol].cap = w;
  36. edge[tol].next = head[u];
  37. edge[tol].flow = 0;
  38. head[u] = tol++;
  39. edge[tol].to = u;
  40. edge[tol].cap = rw;
  41. edge[tol].next = head[v];
  42. edge[tol].flow = 0;
  43. head[v] = tol++;
  44. }
  45. //最大流开始
  46. int sap(int start,int end,int N){
  47. memset(gap,0,sizeof(gap));
  48. memset(dep,0,sizeof(dep));
  49. memcpy(cur,head,sizeof(head));
  50. int u = start;
  51. pre[u] = -1;
  52. gap[0] = N;
  53. int ans = 0;
  54. while(dep[start] < N){
  55. if(u==end){
  56. int Min = INF;
  57. for(int i=pre[u];i!= -1; i=pre[edge[i^1].to])
  58. if(Min > edge[i].cap - edge[i].flow)
  59. Min = edge[i].cap - edge[i].flow;
  60. for(int i=pre[u];i!=-1;i=pre[edge[i^1].to]){
  61. edge[i].flow += Min;
  62. edge[i^1].flow -=Min;
  63. }
  64. u=start;
  65. ans +=Min;
  66. continue;
  67. }
  68. bool flag = false;
  69. int v;
  70. for(int i= cur[u];i!=-1;i=edge[i].next){
  71. v=edge[i].to;
  72. if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u]){
  73. flag=true;
  74. cur[u]=pre[v]=i;
  75. break;
  76. }
  77. }
  78. if(flag){
  79. u=v;
  80. continue;
  81. }
  82. int Min = N;
  83. for(int i=head[u];i!= -1;i=edge[i].next)
  84. if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min){
  85. Min=dep[edge[i].to];
  86. cur[u] = i;
  87. }
  88. gap[dep[u]]--;
  89. if(!gap[dep[u]]) return ans;
  90. dep[u] = Min +1;
  91. gap[dep[u]]++;
  92. if(u!=start) u = edge[pre[u]^1].to;
  93. }
  94. return ans;
  95. }
  96. //最大流结束
  97. //构图
  98. void build(int mid){
  99. int i,j;
  100. S=TOL+1;
  101. V=TOL+2;
  102. //超级源点 到每个人
  103. for(i=0;i<M;i++){
  104. addedge(S,i,b[i]);
  105. }
  106. //每场比赛到超级汇点
  107. for(i=0;i<M;i++){
  108. for(j=i+1;j<M;j++){
  109. addedge(co[i][j],V,1);
  110. }
  111. }
  112. //人到比赛
  113. for(i=0;i<M;i++){
  114. for(j=i+1;j<M;j++){
  115. if(i<mid&&j<mid){
  116. if(b[i]>b[j]){
  117. addedge(j,co[i][j],1);
  118. }
  119. else if(b[i]<b[j]){
  120. addedge(i,co[i][j],1);
  121. }
  122. else{
  123. addedge(j,co[i][j],1);
  124. addedge(i,co[i][j],1);
  125. }
  126. }
  127. else{
  128. addedge(j,co[i][j],1);
  129. addedge(i,co[i][j],1);
  130. }
  131. }
  132. }
  133. }
  134. //二分枚举解题
  135. bool solve(int mid){
  136. init();
  137. build(mid);
  138. int k=sap(S,V,V+1);
  139. // printf("k=%d\n",k);
  140. return k==TOL-M+1;
  141. }
  142. int main(){
  143. int i,j,k,l,m,n;
  144. int T;
  145. scanf("%d\n",&T);
  146. char a;
  147. while(T--){
  148. i=0;
  149. while(1){
  150. scanf("%c",&a);
  151. if(a=='\n'||a=='\0'){
  152. break;
  153. }
  154. else{
  155. if(a!=' '){
  156. b[i++]=a-'0';
  157. }
  158. }
  159. }
  160. //printf("%d\n",b[0]);
  161. M=i;
  162. sort(b,b+M,cmp);
  163. int num=M;
  164. // printf("M= %d\n",M);
  165. for(i=0;i<M;i++){
  166. for(j=i+1;j<M;j++){
  167. co[i][j]=num++;
  168. }
  169. }
  170. TOL=num-1;
  171. // printf("TOL= %d\n",TOL);
  172. // for(int aa=0;aa<M;aa++) printf("%d ",b[aa]);
  173. int l=0,orz;
  174. for(i=1;i<=M;i++){
  175. if(solve(i)){
  176. orz=i;
  177. }
  178. else break;
  179. }
  180. printf("%d\n",orz);
  181. }
  182. }




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/64616
推荐阅读
相关标签
  

闽ICP备14008679号