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
二分加最大流
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <climits> using namespace std; #define read() freopen("sw.in", "r", stdin) const int MAX = 2e5 + ;
const int INF = 1e9 + ;
vector <int> G[MAX];
struct Edge {int from, to, cap, flow;};
vector <Edge> edges;
bool vis[MAX];
int d[MAX];
int cur[MAX];
int u[MAX], v[MAX]; int N, M, s, t; void add_edge(int from, int to, int cap) {
edges.push_back((Edge) {from, to, cap, });
edges.push_back((Edge) {to, from, , });
int m = edges.size();
G[from].push_back(m - );
G[to].push_back(m - ); } bool BFS() {
memset(vis, , sizeof(vis));
d[s] = ;
vis[s] = ;
queue <int> q;
q.push(s); while (!q.empty()) {
int x = q.front(); q.pop();
for (int i = ; i < G[x].size(); ++i) {
Edge &e = edges[ G[x][i] ];
if (!vis[e.to] && e.cap > e.flow) {
d[e.to] = d[x] + ;
vis[e.to] = ;
q.push(e.to);
}
} } return vis[t];
} int DFS(int x, int a) {
if (x == t || a == ) return a;
int flow = , f;
for (int &i = cur[x]; i < G[x].size(); ++i) {
Edge &e = edges[ G[x][i] ];
if (d[e.to] == d[x] + && (f = DFS(e.to, min(a, e.cap - e.flow))) > ) {
e.flow += f;
edges[ G[x][i] ^ ].flow -= f;
flow += f;
a -= f;
if (a == ) break;
} } return flow;
} int Maxflow() {
int flow = ;
while (BFS()) {
memset(cur, , sizeof(cur));
flow += DFS(s, INF);
} return flow;
} bool check(int mid) {
edges.clear();
for (int i = ; i <= t; ++i) G[i].clear(); for (int i = ; i <= N; ++i) {
add_edge(s, i, mid);
} for (int i = ; i <= M; ++i) {
add_edge(N + i, t, );
} for (int i = ; i <= M; ++i) {
add_edge(u[i], N + i, );
add_edge(v[i], N + i, );
} return Maxflow() >= M;
}
void solve() {
int l = , r = M;
while (l < r) {
int mid = (l + r) >> ;
// printf("l = %d r = %d mid = %d\n", l, r, mid);
if (check(mid)) r = mid;
else l = mid + ;
} printf("%d\n", l);
}
int main()
{
read();
int T;
scanf("%d", &T);
for (int ca = ; ca <= T; ++ca) {
scanf("%d%d", &N, &M);
s = , t = N + M + ;
for (int i = ; i <= M; ++i) {
scanf("%d%d", &u[i], &v[i]);
} solve();
}
//cout << "Hello world!" << endl;
return ;
}
uvalive 3231的更多相关文章
- uvalive 3231 Fair Share 公平分配问题 二分+最大流 右边最多流量的结点流量尽量少。
/** 题目: uvalive 3231 Fair Share 公平分配问题 链接:https://vjudge.net/problem/UVALive-3231 题意:有m个任务,n个处理器,每个任 ...
- UVALive 3231 Fair Share
Fair Share Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Origina ...
- UVALive 3231 网络流
题目要求给m个任务分配给n个机器,但最后任务量最多的那个机器的任务量尽量少,利用最大流,在最后的汇点那里设置关卡,二分结果,把机器到最终汇点的容量设置为该值,这样就达到题目条件,这样跑最大流 还能把m ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- 小吃(codevs 3231)
3231 小吃 时间限制: 1 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 这里简直是吃货的天堂,小吃太多了. ...
- 思维 UVALive 3708 Graveyard
题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...
- UVALive 6145 Version Controlled IDE(可持久化treap、rope)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
随机推荐
- Python3基础(十) 类的初印象
Python是一种面向对象的脚本语言,所以它也提供了面向对象编程的所有基本特征:允许多继承的类继承机制.派生类可以重写它父类的任何方法.一个方法可以调用父类中同名的方法.对象可以包含任意数量和类型的数 ...
- kettle_删除“共享输出表”引发的错误
原创作品.出自 "深蓝的blog" 博客.欢迎转载,转载时请务必注明出处,否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlong ...
- LeetCode 7. Reverse Integer (倒转数字)
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- C++中stringstream ostringstream istringstream使用方式
C++引入了ostringstream.istringstream.stringstream这三个类,要使用他们创建对象就必须包括sstream.h头文件. istringstream类用于运行C++ ...
- HDU 5862Counting Intersections
Counting Intersections Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- fprintf与stderr、stdout的使用
#include <stdio.h> void main() { fprintf(stderr,"soyo8888!"); fprintf(stdout,"s ...
- android api level对应表(copy)
Platform Version API Level VERSION_CODE Notes Android 4.4 19 KITKAT Platform Highlights Android 4.3 ...
- 【TODO】Ruby Range类
创建: 2017/10/28 Range类 ...
- E20170627-hm
confirmation n. 证实; 证明; 确认,
- frameset的target属性
使用frameset时的target属性 (2012-09-18 08:19:31) 转载▼ 分类: java技术之路 一般常用的有四个属性 _blank 浏览器总在一个新打开.未命名的窗口中载入 ...