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, 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=0;bool f=1; R char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=0;ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();} return f?x:-x; } const int N=2e4+10; int cas,S,T,n,m; int g[N][2],head[N],cur[N],dis[N],q[N*2]; struct node{ int v,next,cap; }e[N<<2];int tot=1; 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=0;e[tot].next=head[y];head[y]=tot; } bool bfs(){ for(int i=S;i<=T;i++) dis[i]=inf; int h=0,t=1;dis[S]=0;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]+1){ dis[v]=dis[x]+1; if(v==T) return 1; q[++t]=v; } } } return 0; } int dfs(int x,int f){ if(x==T) return f; int used=0,t; for(int i=head[x],v;i;i=e[i].next){ if(e[i].cap&&dis[v=e[i].v]==dis[x]+1){ t=dfs(v,min(f,e[i].cap)); e[i].cap-=t;e[i^1].cap+=t; used+=t;f-=t; if(!f) return used; } } if(!used) dis[x]=0; return used; } bool check(int mid){ tot=1;m(head);int res=0; for(int i=1;i<=m;i++) add(S,i,1); for(int i=1;i<=m;i++) add(i,g[i][0]+m,1),add(i,g[i][1]+m,1); for(int i=1;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=0;T=n+m+1; for(int i=1;i<=m;i++) g[i][0]=read(),g[i][1]=read(); int mid,ans=0,l=1,r=m; while(l<=r){ mid=(l+r)>>1; if(check(mid)) ans=mid,r=mid-1; else l=mid+1; } printf("%d ",ans); } return 0; }