uvalive 3231

3231 - Fair Share
Asia - Seoul - 2004/2005
You are given N processors and M jobs to be processed. Two processors are specified to each job. To process
the job, the job should be allocated to and executed on one of the two processors for one unit of time. If K jobs
are allocated to a processor, then it takes K units of time for the processor to complete the jobs. To complete
all the jobs as early as possible, you should allocate the M jobs to the N processors as fair as possible.
Precisely speaking, you should minimize the maximum number of jobs allocated to each processor over all
processors. The quantity, minimum number of jobs, is called fair share.
For example, you are given 5 processors and 6 jobs. Each job can be allocated to one of the two processors as
shown in the table below. Job 1 can be allocated to processors 1 or 2, and job 2 can be allocated to processors
2 or 3, etc. If you allocate job 1 to processor 1, job 2 to processor 2, job 3 to processor 3, job 4 to processor 4,
job 5 to processor 5, and job 6 to processor 1, then you have at most two jobs allocated to each processor.
Since there are more jobs than processors in this example, some processors necessarily have at least two jobs,
and thus the fair share is two.
Given N processors, M jobs, and the sets of two processors to which the jobs can be allocated, you are to write
a program that finds the fair share. Processors are numbered from 1 to N and jobs are numbered from 1 to M .
It is assumed that the sets of two processors to which the jobs can be allocated are distinct over all jobs.
That is, if a job J1
can be allocated to processors P1
or P2
, and a job J2
which is different from J1
can be
allocated to processors P3
or P4
, then {P1
, P2
} {P3
, P4
}.
Input
The input consists of T test cases. The number of test cases T is given in the first line of the input file. Each
test case begins with a line containing an integer N, 1 N 1, 000, that represents the number of processors
in the test case. It is followed by a line containing an integer M, 1 M 10, 000, that represents the number
of jobs. In the following M lines, K-th line contains two distinct integers representing processors to which job
K can be allocated, 1 K M. The integers given in a line are separated by a space. After that, the remaining
test cases are listed in the same manner as the above.
3231 - Fair Share 1/2Output
Print exactly one line for each test case. The line should contain the fair share for that test case.
The following shows sample input and output for three test cases.
Sample Input
3
5
6
1 2
2 3
3 4
4 5
5 1
1 3
3
2
3 2
1 2
6
6
1 2
3 4
4 6
6 5
5 3
6 3
Sample Output
2
1
2
Seoul 2004-2005

二分加最大流

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <queue>
  6 #include <vector>
  7 #include <climits>
  8
  9 using namespace std;
 10
 11 #define read() freopen("sw.in", "r", stdin)
 12
 13 const int MAX = 2e5 + 7;
 14 const int INF = 1e9 + 7;
 15 vector <int> G[MAX];
 16 struct Edge {int from, to, cap, flow;};
 17 vector <Edge> edges;
 18 bool vis[MAX];
 19 int d[MAX];
 20 int cur[MAX];
 21 int u[MAX], v[MAX];
 22
 23
 24 int N, M, s, t;
 25
 26 void add_edge(int from, int to, int cap) {
 27     edges.push_back((Edge) {from, to, cap, 0});
 28     edges.push_back((Edge) {to, from, 0, 0});
 29     int m = edges.size();
 30     G[from].push_back(m - 2);
 31     G[to].push_back(m - 1);
 32
 33 }
 34
 35 bool BFS() {
 36     memset(vis, 0, sizeof(vis));
 37     d[s] = 0;
 38     vis[s] = 1;
 39     queue <int> q;
 40     q.push(s);
 41
 42     while (!q.empty()) {
 43         int x = q.front(); q.pop();
 44         for (int i = 0; i < G[x].size(); ++i) {
 45             Edge &e = edges[ G[x][i] ];
 46             if (!vis[e.to] && e.cap > e.flow) {
 47                 d[e.to] = d[x] + 1;
 48                 vis[e.to] = 1;
 49                 q.push(e.to);
 50             }
 51         }
 52
 53     }
 54
 55     return vis[t];
 56 }
 57
 58 int DFS(int x, int a) {
 59     if (x == t || a == 0) return a;
 60     int flow = 0, f;
 61     for (int &i = cur[x]; i < G[x].size(); ++i) {
 62         Edge &e = edges[ G[x][i] ];
 63         if (d[e.to] == d[x] + 1 && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) {
 64             e.flow += f;
 65             edges[ G[x][i] ^ 1 ].flow -= f;
 66             flow += f;
 67             a -= f;
 68             if (a == 0) break;
 69         }
 70
 71     }
 72
 73     return flow;
 74 }
 75
 76 int Maxflow() {
 77     int flow = 0;
 78     while (BFS()) {
 79         memset(cur, 0, sizeof(cur));
 80         flow += DFS(s, INF);
 81     }
 82
 83     return flow;
 84 }
 85
 86 bool check(int mid) {
 87     edges.clear();
 88     for (int i = 0; i <= t; ++i) G[i].clear();
 89
 90     for (int i = 1; i <= N; ++i) {
 91         add_edge(s, i, mid);
 92     }
 93
 94     for (int i = 1; i <= M; ++i) {
 95         add_edge(N + i, t, 1);
 96     }
 97
 98     for (int i = 1; i <= M; ++i) {
 99         add_edge(u[i], N + i, 1);
100         add_edge(v[i], N + i, 1);
101     }
102
103     return Maxflow() >= M;
104 }
105 void solve() {
106     int l = 0, r = M;
107     while (l < r) {
108         int mid = (l + r) >> 1;
109        // printf("l = %d r = %d mid = %d\n", l, r, mid);
110         if (check(mid)) r = mid;
111         else l = mid + 1;
112     }
113
114     printf("%d\n", l);
115 }
116 int main()
117 {
118     read();
119     int T;
120     scanf("%d", &T);
121     for (int ca = 1; ca <= T; ++ca) {
122         scanf("%d%d", &N, &M);
123         s = 0, t = N + M + 1;
124         for (int i = 1; i <= M; ++i) {
125             scanf("%d%d", &u[i], &v[i]);
126         }
127
128         solve();
129     }
130     //cout << "Hello world!" << endl;
131     return 0;
132 }

uvalive 3231

时间: 2024-11-06 02:16:24

uvalive 3231的相关文章

uvalive 3231 Fair Share 公平分配问题 二分+最大流 右边最多流量的结点流量尽量少。

/** 题目: uvalive 3231 Fair Share 公平分配问题 链接:https://vjudge.net/problem/UVALive-3231 题意:有m个任务,n个处理器,每个任务有两个候选处理器,只要其中一个运行,该任务就能执行. 不同任务的两个候选处理器,至少有一个不同. 求任务数最多的那个处理器所分配的任务数尽量少. 思路:二分+最大流 左边是任务,s->u,cap = 1. 如果任务u和u的候选处理器v,u->v, cap = 1. 右边是处理器,二分mi.所有处

UVALive 3231 网络流

题目要求给m个任务分配给n个机器,但最后任务量最多的那个机器的任务量尽量少,利用最大流,在最后的汇点那里设置关卡,二分结果,把机器到最终汇点的容量设置为该值,这样就达到题目条件,这样跑最大流 还能把m个任务跑完(最终流量为m),则可行,继续二分 用的dinic #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <vector>

UVALive 4848 Tour Belt

F - Tour Belt Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 4848 Description Korea has many tourist attractions. One of them is an archipelago (Dadohae in Korean), a cluster of small islands sca

UVALive 6467 Strahler Order 拓扑排序

这题是今天下午BNU SUMMER TRAINING的C题 是队友给的解题思路,用拓扑排序然后就可以了 最后是3A 其中两次RE竟然是因为: scanf("%d",mm); ORZ 以后能用CIN还是CIN吧 QAQ 贴代码了: 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <iostre

UVALive 7077 Little Zu Chongzhi&#39;s Triangles (有序序列和三角形的关系)

这个题……我上来就给读错了,我以为最后是一个三角形,一条边可以由多个小棒组成,所以想到了状态压缩各种各样的东西,最后成功了……结果发现样例过不了,三条黑线就在我的脑袋上挂着,改正了以后我发现N非常小,想到了回溯每个棍的分组,最多分5组,结果发现超时了……最大是5^12 =  244,140,625,厉害呢…… 后来想贪心,首先想暴力出所有可能的组合,结果发现替换问题是一个难题……最后T T ,我就断片了.. 等看了别人的办法以后,我才发现我忽视了三角形的特性,和把数据排序以后的特点. 如果数据从

Gym 100299C &amp;&amp; UVaLive 6582 Magical GCD (暴力+数论)

题意:给出一个长度在 100 000 以内的正整数序列,大小不超过 10^ 12.求一个连续子序列,使得在所有的连续子序列中, 它们的GCD值乘以它们的长度最大. 析:暴力枚举右端点,然后在枚举左端点时,我们对gcd相同的只保留一个,那就是左端点最小的那个,只有这样才能保证是最大,然后删掉没用的. UVaLive上的数据有问题,比赛时怎么也交不过,后来去别的oj交就过了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&qu

UVALive 6511 Term Project

Term Project Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ID: 651164-bit integer IO format: %lld      Java class name: Main 解题:强连通分量 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1

UVALive 6508 Permutation Graphs

Permutation Graphs Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ID: 650864-bit integer IO format: %lld      Java class name: Main 解题:逆序数 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long l

UVALive 2659+HUST 1017+ZOJ 3209 (DLX

UVALive 2659 题目:16*16的数独.试了一发大白模板. /* * @author: Cwind */ //#pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <map> #include <algorithm> #include <cstdio> #include <cstring> #include