標籤:style blog color os io art
給一個圖,某些點需要單獨以某一種顏色的線串連到1點,問如何安排能夠使得整個圖顏色最多的一條路顏色最少。
顯然,二分枚舉然後加以顏色其實就是流量了,相當於對每條邊限定一個當前二分的流量值,判斷能否滿流即可。
召喚代碼君:
#include <iostream>#include <cstdio>#include <cstring>#define maxn 555#define maxm 333333using namespace std;const int inf=~0U>>2;int to[maxm],next[maxm],c[maxm],first[maxm],edge;int tag[maxn],d[maxn],TAG=520;int Q[maxn],bot,top;bool can[maxn];int n,m,k,T,h,s,t,house,ans,boder;void addedge(int U,int V){ edge++; to[edge]=V,c[edge]=1,next[edge]=first[U],first[U]=edge; edge++; to[edge]=U,c[edge]=1,next[edge]=first[V],first[V]=edge;}void _input(){ scanf("%d%d%d",&n,&m,&k); edge=-1; for (int i=0; i<=n; i++) first[i]=-1; s=1,t=0,house=k; while (k--) { scanf("%d",&h); addedge(h,t); } boder=edge; while (m--) { scanf("%d%d",&k,&h); addedge(k,h); }}bool bfs(){ Q[bot=top=1]=t,d[t]=0,can[t]=false,tag[t]=++TAG; while(bot<=top) { int cur=Q[bot++]; for (int i=first[cur]; i!=-1; i=next[i]) { if (c[i^1]>0 && tag[to[i]]!=TAG) { tag[to[i]]=TAG,Q[++top]=to[i]; d[to[i]]=d[cur]+1,can[to[i]]=false; if (to[i]==s) return true; } } } return false;}int dfs(int cur,int num){ if (cur==t) return num; int tmp=num,k; for (int i=first[cur]; i!=-1; i=next[i]) if (c[i]>0 && d[to[i]]==d[cur]-1 && tag[to[i]]==TAG && !can[to[i]]) { k=dfs(to[i],min(num,c[i])); if (k) num-=k,c[i]-=k,c[i^1]+=k; if (num==0) break; } if (num) can[cur]=true; return tmp-num;}bool check(int x){ for (int i=0; i<=boder; i++) c[i]=1; for (int i=boder+1; i<=edge; i++) c[i]=x; for ( ans=0; bfs(); ) ans+=dfs(s,inf); return ans>=house;}int main(){ scanf("%d",&T); while (T--) { _input(); int l=1,r=n,mid; while (l<r) { mid=(l+r)>>1; if (check(mid)) r=mid; else l=mid+1; } printf("%d\n",l); } return 0;}