2 条题解
-
0
#include<bits/stdc++.h> using namespace std; const int N=1e5+10; #define int long long struct node{int to,v,nxt;}e[N];int head[N],len; void add(int x,int y,int c) { e[++len]={y,c,head[x]};head[x]=len; e[++len]={x,0,head[y]};head[y]=len; } int cur[N],d[N],st,ed; bool find() { memset(d,0,sizeof(d));d[st]=1; deque<int>q;q.push_back(st); while(!q.empty()) { int x=q.front();q.pop_front(); for(int i=head[x];i;i=e[i].nxt) { int y=e[i].to; if(d[y]==0&&e[i].v) { d[y]=d[x]+1; q.push_back(y); if(y==ed)return 1; } } } return 0; } int flow(int x,int s) { if(x==ed)return s; int ans=0; for(int i=cur[x];i;i=e[i].nxt) { int y=e[i].to; cur[x]=i; if(d[y]==d[x]+1&&e[i].v) { int sum=flow(y,min(e[i].v,s)); e[i].v-=sum; e[i^1].v+=sum; ans+=sum; s-=sum; if(s==0)break; } } if(ans==0)d[x]=0; return ans; } int dinic() { int ans=0; while(find()) { memcpy(cur,head,sizeof(cur)); ans+=flow(st,1e18); } return ans; } int n,k,sum=0; signed main() { cin>>k>>n;len=1,st=0,ed=n+k+2; for(int i=1;i<=n;i++)add(st,i,1); for(int i=1;i<=k;i++) { int x;cin>>x;sum+=x; add(i+n,ed,x); } for(int i=1;i<=n;i++) { int p;cin>>p; for(int j=1;j<=p;j++) { int x;cin>>x; add(i,x+n,1); } } int ans=dinic(); if(ans!=sum) { cout<<"No Solution!"; return 0; } vector<int>res[k+10]; for(int i=1;i<=k;i++) { int ee=i+n; for(int j=head[ee];j;j=e[j].nxt) { int y=e[j].to; if(e[j].v==1&&y<=n) res[i].push_back(y); } } for(int i=1;i<=k;i++) { sort(res[i].begin(),res[i].end()); cout<<i<<": ";for(int y:res[i])cout<<y<<' ';cout<<'\n'; } return 0; } -
0
#include<bits/stdc++.h> using namespace std; const int N = 1010; typedef long long LL; struct edge { LL x,cap,rev; }; vector<edge> e[N]; int n,m,s,t; int d[N],it[N]; void add(int a,int b,LL c) { e[a].push_back({b,c,(LL)e[b].size()}); e[b].push_back({a,0,(LL)e[a].size() - 1}); } void bfs() { memset(d,-1,sizeof d); queue<int> q; d[s] = 0; q.push(s); while(!q.empty()) { int u = q.front(); q.pop(); for(auto t : e[u]) if(t.cap > 0 && d[t.x] < 0) d[t.x] = d[u] + 1,q.push(t.x); } } LL dfs(int u,LL f) { if(u == t) return f; for(int &i = it[u]; i < e[u].size(); i ++) { edge &t = e[u][i]; if(d[u] < d[t.x] && t.cap > 0) { LL d = dfs(t.x,min(f,t.cap)); if(d > 0) { t.cap -= d; e[t.x][t.rev].cap += d; return d; } } } return 0; } LL dinic() { LL flow = 0; while(1) { bfs(); if(d[t] < 0) break; memset(it,0,sizeof it); LL d = dfs(s,1e18); while(d > 0) { flow += d; d = dfs(s,1e18); } } return flow; } int main() { cin>>n>>m; s = 0,t = n + m + 1; LL sum = 0; for(int i = 1,x; i <= n; i ++) cin>>x,add(i + m,t,x),sum += x; for(int i = 1,x,y; i <= m; i ++) { add(s,i,1); cin>>x; while(x --) cin>>y,add(i,y + m,1); } LL res = dinic(); if(res == sum) for(int i = 1; i <= n; i ++) { cout<<i<<": "; for(auto x : e[i + m]) if(x.cap) cout<<x.x<<' '; cout<<'\n'; } else cout<<"No Solution!\n"; return 0; }
- 1
信息
- ID
- 958
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 9
- 标签
- 递交数
- 14
- 已通过
- 5
- 上传者