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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

YbtOJ-大收藏家【分层图,最大流】

發布時間:2023/12/3 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 YbtOJ-大收藏家【分层图,最大流】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

正題

題目鏈接:https://www.ybtoj.com.cn/contest/117/problem/2


題目大意

nnn個人,每人有aia_iai?個屬于自己的物品。mmm次交換依次進行,每次xi,yix_i,y_ixi?,yi?兩個人可以決定拿不拿自己的一個物品進行交換。

111號人最后能拿到最多多少種物品

1≤n,m,ai≤30001\leq n,m,a_i\leq 30001n,m,ai?3000


解題思路

每種物品只需要一個,所以每種物品的第一個可以視為流量,aia_iai?可以視為自己的物品處的空位(自己的物品可以不視為自己的)。

xi,yix_i,y_ixi?,yi?的交換可以視為一條流量為111的雙向邊,因為依次進行所以要分成mmm層,然后每一層有交換的連邊。

發現這樣有很多點沒有用到,去掉這些多余的,那點數就是O(n+m)O(n+m)O(n+m)級別的了

跑最大流就好了


code

#include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; const int N=12100,inf=1e9; struct node{int to,next,w; }a[N<<2]; int T,n,m,tot,cnt,ans,s,t; int ls[N],dep[N],p[N],w[N]; queue<int> q; void addl(int x,int y,int w){a[++tot].to=y;a[tot].next=ls[x];ls[x]=tot;a[tot].w=w;a[++tot].to=x;a[tot].next=ls[y];ls[y]=tot;a[tot].w=0;return; } bool bfs(){memset(dep,0,sizeof(dep));dep[s]=1;while(!q.empty())q.pop();q.push(s);while(!q.empty()){int x=q.front();q.pop();for(int i=ls[x];i;i=a[i].next){int y=a[i].to;if(dep[y]||!a[i].w)continue;dep[y]=dep[x]+1;if(y==t)return 1;q.push(y);} }return 0; } int dinic(int x,int flow){if(x==t)return flow;int rest=0,k;for(int i=ls[x];i;i=a[i].next){int y=a[i].to;if(dep[x]+1!=dep[y]||!a[i].w)continue;rest+=(k=dinic(y,min(a[i].w,flow-rest)));a[i].w-=k;a[i^1].w+=k;if(rest==flow)return rest;}if(!rest)dep[x]=0;return rest; } int main() {freopen("collection.in","r",stdin);freopen("collection.out","w",stdout);scanf("%d",&T);while(T--){tot=0;memset(ls,0,sizeof(ls));scanf("%d%d",&n,&m);s=tot=1;t=cnt=2;ans=0;for(int i=1;i<=n;i++){p[i]=++cnt;scanf("%d",&w[i]);addl(s,p[i],1);}for(int i=1;i<=m;i++){int x,y;scanf("%d%d",&x,&y);++cnt;addl(p[x],cnt,w[x]);p[x]=cnt;++cnt;addl(p[y],cnt,w[y]);p[y]=cnt;addl(p[x],p[y],1);addl(p[y],p[x],1);}addl(p[1],t,inf);while(bfs())ans+=dinic(s,inf);printf("%d\n",ans);}return 0; }

總結

以上是生活随笔為你收集整理的YbtOJ-大收藏家【分层图,最大流】的全部內容,希望文章能夠幫你解決所遇到的問題。

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