最大闭合权子图复习
最后的 s 所在的连通分量为 闭合子图
#include <bits/stdc++.h>
using namespace std;
typedef int LL;
typedef int lint;
const LL maxn = 1000;
const LL maxm = 1000000;
const LL inf = 0x3f3f3f3f;
struct dinic{
LL he[maxn],ne[maxm],ver[maxm],edge[maxm],d[maxn],vis[maxn];
vector<lint> ans;
LL s,t,tot;
void init( LL n ){
for( LL i = 0;i <= n;i++ ){
he[i] = 0;
}
tot = 1;
}
void add( LL x,LL y,LL z ){
ver[++tot] = y; ne[ tot ] = he[x]; he[x] = tot; edge[ tot ] = z;
ver[++tot] = x; ne[tot] = he[y]; he[y] = tot;edge[tot] = 0;
}
bool bfs(){
memset( d,0,sizeof( d ) );
while( que.size() ) que.pop();
que.push( s );
d[s] = 1;
while( que.size() ){
LL x = que.front();
que.pop();
for( LL cure = he[x];cure;cure = ne[cure] ){
LL y = ver[cure];
if( edge[cure] && !d[y] ){
que.push( y );
d[ y ] = d[x] + 1;
if( y == t ) return 1;
}
}
}
return 0;
}
LL dfs( LL x,LL flow ){
if( x== t ) return flow;
LL rest = flow,k;
for( LL cure = he[x];cure && rest;cure = ne[cure] ){
LL y = ver[cure];
if( edge[cure] && d[ y ] == d[x] + 1 ){
k = dfs( y,min( rest,edge[cure] ) );
if( !k ) d[ y ] = 0;
edge[cure] -= k;
edge[ cure^(LL)1 ] += k;
rest -= k;
}
}
return flow - rest;
}
LL max_flow( LL x,LL y ){
s = x; t = y;
LL maxflow = 0;
LL flow = 0;
while( bfs() )
while( flow = dfs( s,inf ) ) maxflow += flow;
return maxflow;
}
queue<lint> que;
void bfs( lint n ){
vis[s] = 1;
que.push(s);
while( que.size() ){
lint x = que.front();
que.pop();
for( lint cure = he[x];cure;cure = ne[cure] ){
lint y = ver[cure];
if( edge[cure] <= 0 || vis[y] ) continue;
vis[y] = 1;
que.push(y);
}
}
for( lint i = 1;i <= n;i++ ){
if( vis[i] )ans.push_back(i);
}
}
} g;
lint val[maxn];
vector<lint> ve[maxn],yiqi;
int main()
{
lint n,m,num,sum = 0,ulen;
scanf("%d%d",&n,&m);
char c[10001];g.tot = 1;
for (int i=1;i<=n;i++)
{
scanf("%d",&val[i]);
sum+=val[i];
memset(c,0,sizeof(c));
cin.getline(c,10000);
ulen=0;
while (sscanf(c+ulen,"%d",&num)==1)
{
//req[i].pb(num);
ve[i].push_back( num );
if (num==0) ulen++;
else while (num)
{
num/=10;
ulen++;
}
ulen++;
}
}
lint SS = 0,TT = n+m+1;
for (lint i=1;i<=m;i++) {
scanf("%d", &val[n+i]);
}
for( lint i = 1;i <= n+m;i++ ){
if( i <= n ){
g.add( SS,i,val[i] );
}else{
g.add( i,TT,val[i] );
}
}
for( lint i = 1;i <= n;i++ ){
for( lint j = 0;j < ve[i].size();j++ ){
lint y = ve[i][j]+n;
g.add( i,y,inf );
}
}
lint ans = g.max_flow(SS,TT);
g.bfs(n);
lint res = sum - ans ;
for( lint i = 0;i < g.ans.size();i++ ){
lint x= g.ans[i];
if( i == 0 ) printf("%d",x);
else printf(" %d",x);
for( lint j = 0;j < ve[x].size();j++ ){
lint y = ve[x][j];
yiqi.push_back( y );
}
}
printf("\n");
sort( yiqi.begin(),yiqi.end() );
yiqi.erase( unique( yiqi.begin(),yiqi.end() ),yiqi.end() );
for( lint i = 0;i < yiqi.size();i++ ){
if( i == 0 ){
printf("%d",yiqi[i]);
}else printf(" %d",yiqi[i]);
}
printf("\n");
printf("%d\n",res);
return 0;
}