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的更多相关文章

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

    /** 题目: uvalive 3231 Fair Share 公平分配问题 链接:https://vjudge.net/problem/UVALive-3231 题意:有m个任务,n个处理器,每个任 ...

  2. UVALive 3231 Fair Share

    Fair Share Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Origina ...

  3. UVALive 3231 网络流

    题目要求给m个任务分配给n个机器,但最后任务量最多的那个机器的任务量尽量少,利用最大流,在最后的汇点那里设置关卡,二分结果,把机器到最终汇点的容量设置为该值,这样就达到题目条件,这样跑最大流 还能把m ...

  4. UVALive - 4108 SKYLINE[线段树]

    UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug ...

  5. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  6. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  7. 小吃(codevs 3231)

    3231 小吃  时间限制: 1 s  空间限制: 16000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 这里简直是吃货的天堂,小吃太多了. ...

  8. 思维 UVALive 3708 Graveyard

    题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...

  9. UVALive 6145 Version Controlled IDE(可持久化treap、rope)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

随机推荐

  1. iOS页面右滑返回的实现方法总结

    1.边缘触发的系统方法 ①系统返回按钮 self.navigationController.interactivePopGestureRecognizer.enabled = YES;  ②自定义返回 ...

  2. sqlite 数据库 相关知识

    一基本简单介绍 SQLite 是一个自持的(self-contained).无server的.零配置的.事务型的关系型数据库引擎.由于他非常小,所以也能够作为嵌入式数据库内建在你的应用程序中. SQL ...

  3. swift 2.0 语法 分支

    import UIKit // 注意: Swift中可以不写;号, 但是有一定的前提条件, 一行只有一句代码 //      如果一行有多句代码, 那么;还是必须写 // 注意: Swift变态的地方 ...

  4. POJ 2367:Genealogical tree(拓扑排序)

    Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2738 Accepted: 1838 Spe ...

  5. LeetCode 709. To Lower Case (转换成小写字母)

    题目标签:String 题目让我们把大写字母转换成小写,只要遇到的是大写字母,把它 + 32 变成 小写就可以了. Java Solution: Runtime beats 100.00% 完成日期: ...

  6. 应用程序无法正常启动 0xc0000013 vs2013

    今天下午切换到Windows 优化代码,在debug 的时候一直出现这个问题,折腾了很久,发现原来是系统环境变量的问题,我之前装了双系统,讲原来win7 下的一块E盘删掉做了Linux 盘,而系统环境 ...

  7. 【树剖求LCA】树剖知识点

    不太优美但是有注释的版本: #include<cstdio> #include<iostream> using namespace std; struct edge{ int ...

  8. C++ 函数模板与类模板(使用 Qt 开发编译环境)

    注意:本文中代码均使用 Qt 开发编译环境,如有疑问和建议欢迎随时留言. 模板是 C++ 支持参数化程序设计的工具,通过它可以实现参数多态性.所谓参数多态性,就是将程序所处理的对象的类型参数化,使得一 ...

  9. 继承自TWinControl的控件不能在设计期间接受子控件,用代码设置子控件却可以(它的自绘是直接改写PaintWindow虚函数,而不是覆盖Paint函数——对TWinControl.WMPaint又有新解了)

    这个控件直接继承自TWinControl,因此不是改写Paint;函数,而是直接改写PaintWindow虚函数,它在VCL框架里被直接调用,直接就把自己画好了(不用走给控件Perform(WM_Pa ...

  10. Android 信息提示——Toast方式

    Toast用于向用户显示一些帮助/提示.一下列举了5中样式. 一.默认的效果(显示在屏幕的底部) 代码: Toast.makeText(getApplicationContext(), "默 ...