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 {P1P2}{P3P4}.

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, 1N1, 000, that represents the number of processors in the test case. It is followed by a line containing an integer M, 1M10, 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, 1KM. 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.

Output

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

题意:m个任务n个处理器,一个任务只能在可供运行的两个机器中的其中一个上运行,问怎样分配使得任务最多的机器尽量少
思路:把任务和可以选择的机器连边,容量为1,建立源点汇点s, t,s到每个任务的容量为1,对于机器到汇点的容量二分答案

#include<cstdio>
#include<cstring>
#include<queue>
#define m(s) memset(s,0,sizeof s)
#define R register
#define inf 0x3f3f3f3f
using namespace std;
int read(){
R int x=;bool f=;
R char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return f?x:-x;
}
const int N=2e4+;
int cas,S,T,n,m;
int g[N][],head[N],cur[N],dis[N],q[N*];
struct node{
int v,next,cap;
}e[N<<];int tot=;
void add(int x,int y,int z){
e[++tot].v=y;e[tot].cap=z;e[tot].next=head[x];head[x]=tot;
e[++tot].v=x;e[tot].cap=;e[tot].next=head[y];head[y]=tot;
}
bool bfs(){
for(int i=S;i<=T;i++) dis[i]=inf;
int h=,t=;dis[S]=;q[t]=S;
while(h!=t){
int x=q[++h];
for(int i=head[x],v;i;i=e[i].next){
if(e[i].cap&&dis[v=e[i].v]>dis[x]+){
dis[v]=dis[x]+;
if(v==T) return ;
q[++t]=v;
}
}
}
return ;
}
int dfs(int x,int f){
if(x==T) return f;
int used=,t;
for(int i=head[x],v;i;i=e[i].next){
if(e[i].cap&&dis[v=e[i].v]==dis[x]+){
t=dfs(v,min(f,e[i].cap));
e[i].cap-=t;e[i^].cap+=t;
used+=t;f-=t;
if(!f) return used;
}
}
if(!used) dis[x]=;
return used;
}
bool check(int mid){
tot=;m(head);int res=;
for(int i=;i<=m;i++) add(S,i,);
for(int i=;i<=m;i++) add(i,g[i][]+m,),add(i,g[i][]+m,);
for(int i=;i<=n;i++) add(i+m,T,mid);
while(bfs()){
res+=dfs(S,inf);
}
return res>=m;
}
int main(){
for(cas=read();cas--;){
n=read();m=read();S=;T=n+m+;
for(int i=;i<=m;i++) g[i][]=read(),g[i][]=read();
int mid,ans=,l=,r=m;
while(l<=r){
mid=(l+r)>>;
if(check(mid)) ans=mid,r=mid-;
else l=mid+;
}
printf("%d\n",ans);
}
return ;
}

LA 3231 - Fair Share的更多相关文章

  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. LA3231 Fair Share 二分_网络流

    Code: #include<cstdio> #include<vector> #include<queue> #include<cstring> #i ...

  4. uvalive 3231

    3231 - Fair ShareAsia - Seoul - 2004/2005You are given N processors and M jobs to be processed. Two ...

  5. 三:Fair Scheduler 公平调度器

    参考资料: http://hadoop.apache.org/docs/current/hadoop-yarn/hadoop-yarn-site/FairScheduler.html http://h ...

  6. <老友记>学习笔记

    这是六个人的故事,从不服输而又有强烈控制欲的monica,未经世事的千金大小姐rachel,正直又专情的ross,幽默风趣的chandle,古怪迷人的phoebe,花心天真的joey——六个好友之间的 ...

  7. 清华大学ucore操作系统课笔记

    操作系统 清华大学ucore操作系统课笔记 全文思维导图 1. 操作系统概述 1.1 什么是操作系统? 操作系统的定义 没有公认的精确定义 一个控制程序 一个系统软件 控制程序执行过程,防止错误和计算 ...

  8. 大数据之Yarn——Capacity调度器概念以及配置

    试想一下,你现在所在的公司有一个hadoop的集群.但是A项目组经常做一些定时的BI报表,B项目组则经常使用一些软件做一些临时需求.那么他们肯定会遇到同时提交任务的场景,这个时候到底如何分配资源满足这 ...

  9. [大数据之Yarn]——资源调度浅学

    在hadoop生态越来越完善的背景下,集群多用户租用的场景变得越来越普遍,多用户任务下的资源调度就显得十分关键了.比如,一个公司拥有一个几十个节点的hadoop集群,a项目组要进行一个计算任务,b项目 ...

随机推荐

  1. Linux驱动学习 —— 在/sys下面创建目录示例

    有时我们需要在/sys下面创建一些目录, 下面给出了一个示例. 在加载驱动模块后, 在/sys下面会创建一个名为sysfs_demo的目录,并在其中在创建几个文件和目录. [root@tiny4412 ...

  2. Java 计算N阶乘末尾0的个数-LeetCode 172 Factorial Trailing Zeroes

    题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...

  3. “全能”选手—Django 1.10文档中文版Part1

    本文是博主翻译的Django1.10版本官方文档的第一部分,如时间充裕,争取一直翻译下去,经验不足,或有错漏,敬请指正. 另外对于公开文档进行翻译的版权问题不是很清楚,如有侵权请联系我! 另外,要转载 ...

  4. Redis简单案例(三) 连续登陆活动的简单实现

    连续登陆活动,或许大家都不会陌生,简单理解就是用户连续登陆了多少天之后,系统就会送一些礼品给相应的用户.最常见的 莫过于游戏和商城这些.游戏就送游戏币之类的东西,商城就送一些礼券.正值国庆,应该也有不 ...

  5. [译]为什么我要离开gulp和grunt转投npm脚本的怀抱

    原文链接:https://medium.freecodecamp.com/why-i-left-gulp-and-grunt-for-npm-scripts-3d6853dd22b8#.n7m1855 ...

  6. GreenDao2.2升级GreenDao3.0的适配之路

    前言.为什么要升级到Greendao3.0? 1. 多人开发 以往的数据库建表建Dao等操作要新开一个module,在统一的地方管理数据库建表,现在可以直接写Entity.多人开发时自己管自己的Ent ...

  7. Android 手机卫士--导航界面2

    本文地址:http://www.cnblogs.com/wuyudong/p/5947504.html,转载请注明出处. 在之前的文章中,实现了导航界面1布局编写与相关的逻辑代码,如下图所示: 点击“ ...

  8. xUtils 3.0 post使用详解

    github:https://github.com/wyouflf/xUtils3 xUtils3简介 xUtils 包含了很多实用的android工具. xUtils 支持超大文件(超过2G)上传, ...

  9. Struts2初始(一)

    1.过滤器:org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter位置:在struts2的核心包struts2-co ...

  10. GitLab CI持续集成配置方案

    目录 1. 持续集成介绍 1.1 概念 1.2 持续集成的好处 2. GitLab持续集成(CI) 2.1 简介 2.2 GitLab简单原理图 2.3 GitLab持续集成所需环境 2.4 需要了解 ...