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_ ...
随机推荐
- 【JavaScript】JS读取XML文件并进行搜索
需求效果 点击链接.当前页面载入xml文件并展示相应内容 通过搜索框.搜索xml文件内节点数据.展示包括内容的节点数据 功能实现 Demo终于实现效果 http://loadxmldemo.coder ...
- ZOJ 2397:Tian Ji -- The Horse Racing
Tian Ji -- The Horse Racing Time Limit: 5 Seconds Memory Limit: 32768 KB Here is a famous story ...
- 【SQL Server】SQL触发器经验详解
[SQL Server]SQL触发器经验详解 | 浏览: 4314 | 更新: 2013-01-07 15:33 25 11 全文阅读分步阅读 加入杂志 步骤 1 2 3 4 5 6 7 8 ...
- java 提取主域名
import com.google.common.net.InternetDomainName; public static void main(String[] args) { InternetDo ...
- 描述一下ArrayList和LinkedList各自实现和区别
ArrayList,LinkedList,Vestor这三个类都实现了java.util.List接口,但它们有各自不同的特性,主要如下: 一.同步性 ArrayList,LinkedList是不同步 ...
- 【线程安全】—— 单例类双重检查加锁(double-checked locking)
1. 三个版本单例类的实现 版本1:经典版 public class Singleton { public static Singleton getInstance() { if (instance ...
- atoi函数——将字符串转换为整数
atoi在一个叫<cstdlib>的库里,可以把字符串直接转换为整数,贼强势. 还有一个atof,就是换成浮点数,实质上是一样的. 例子: #include<cstdlib> ...
- Mybatis 代码自动生成(generatorConfig.xml配置)
博客推荐: Mybatis最入门---代码自动生成(generatorConfig.xml配置) MyBatis Generator generatorConfig.xml配置详解 pom.xml&l ...
- Java中的自定义注解
## 元注解 要声明一个注解, 我们需要元注解, 元注解是指注解的注解,包括@Retention, @Target, @Document, @Inherited. @Retention 注解的保留位置 ...
- windows server 2008 r2 安裝IE11
https://support.microsoft.com/en-us/help/2847882/prerequisite-updates-for-internet-explorer-11 https ...