日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Mean

發布時間:2023/12/2 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mean 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目描述

NiroBC 是貓咪學堂一年級的新生,開學第一天,學堂組織了一場迎新會,在
迎新會上,貓咪們會互相贈送禮物。
一年級的新生共有 N 只貓咪,編號為 1 . . . N(包括 NiroBC 自己),其中有
M 對貓咪是在開學前就互相認識的。學堂規定,對于任意一對已經互相認識的
貓咪 u, v,要么 u 送 v 一份禮物,要么 v 送 u 一份禮物。
學堂知道貓咪們都十分摳門,所以希望安排一種送禮物的方案,使得送出禮
物最多的貓咪送出的禮物最少。

輸入格式

第一行兩個正整數 N, M,表示貓咪的數量和已經互相認識的貓咪的對數。
接下來 M 行,每行兩個整數 u, v,表示 u, v 已經互相認識。數據保證 u ?= v,
且同一個數對最多出現一次((u, v) 和 (v, u) 算作同一數對)。

輸出格式

一個整數,表示送出禮物最多的貓咪最少需要送出幾份禮物。

分析:

由于決策單調性,所以可以二分答案,而判定mid的合法性可以跑最大流,將個關系
當作一個點,源點與這些點相連,容量為1,將這些關系的點與此關系的兩個點連一條
容量為1的邊,最后將這些人的點與匯點連接一條容量為mid的邊,跑出最大流,若等
于關系數m,則合法,否則不合法。

代碼:

#include<cstdio> #include<cctype> #define min(a,b) (a<b?a:b) #define out(x) printf("%d",x) #define inf 0x3f3f3f3f #define maxn 1000007 #define maxm 1000007template <typename T> void in(T &x) {char ch=getchar();bool flag=0;while(ch>'9'||ch<'0') flag|=(ch=='-'),ch=getchar();x=ch-'0';ch=getchar();while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();if(flag) x=-x;return ; }int n,m,s,t,cnt=1,cur[maxn],head[maxn],d[maxn],q[maxn]; struct edge{int to,cost,nxt; }e[maxm];void link(int u,int v,int cost){e[++cnt].to=v;e[cnt].nxt=head[u];e[cnt].cost=cost;head[u]=cnt;e[++cnt].to=u;e[cnt].nxt=head[v];e[cnt].cost=0;head[v]=cnt; }bool bfs(){for(int i=0;i<=t;i++)cur[i]=head[i],d[i]=0;int ha=1,ta=1,now;d[s]=1;q[1]=s;while(ha<=ta){now=q[ha++];for(int i=head[now];i;i=e[i].nxt)if(!d[e[i].to]&&e[i].cost){d[e[i].to]=d[now]+1;q[++ta]=e[i].to;if(e[i].to==t) return 1;}}return 0; }int dfs(int u,int flow){if(u==t) return flow;int rest=flow;for(int w,i=cur[u];i;i=e[i].nxt){cur[u]=i;if(e[i].cost&&d[e[i].to]==d[u]+1){w=dfs(e[i].to,min(rest,e[i].cost));if(!w) { d[e[i].to]=0;continue;}e[i].cost-=w;e[i^1].cost+=w;rest-=w;if(rest==0) return flow;}}if(rest==flow)d[u]=0;return flow-rest; }int maxflow=0;void dinic(){int w;maxflow=0;while(bfs())while(w=dfs(s,inf)) maxflow+=w;return ; }void build(){in(n);in(m);s=0,t=n+m+5;for(int i=1,u,v;i<=m;i++){in(u);in(v);link(s,i,1);link(i,u+m,1);link(i,v+m,1);}for(int i=1;i<=n;i++)link(i+m,t,0);return ; }bool work(int x){for(int i=head[s];i;i=e[i].nxt){e[i].cost=1;e[i^1].cost=0;}for(int i=1;i<=m;i++)for(int to,j=head[i];j;j=e[j].nxt){to=e[j].to;if(to!=s){e[j].cost=1;e[j^1].cost=0;}}for(int i=m+1;i<=n+m;i++)for(int to,j=head[i];j;j=e[j].nxt){to=e[j].to;if(to==t){e[j].cost=x;e[j^1].cost=0;}}dinic();if(maxflow==m) return 1;return 0; }int main(){build();int l=1,ans=n,r=n,mid;while(l<=r){mid=(l+r)>>1;if(work(mid)) ans=mid,r=mid-1;else l=mid+1;}printf("%d",ans);return 0; }

轉載于:https://www.cnblogs.com/ieqefcr/p/9833209.html

總結

以上是生活随笔為你收集整理的Mean的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。