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