【TJU】2944 Mussy Paper 最大权闭合子图

传送门:【TJU】2944 Mussy Paper


题目分析:最大权闭合子图模板题。。没啥好说的。。。

PS:置换群的轨道长度的证明迟迟没看懂。。TUT。。十分不开心就来写水题了


代码如下:


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

#define REP( i , a , b ) for ( int i = a ; i < b ; ++ i )
#define FOR( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REV( i , a , b ) for ( int i = a ; i >= b ; -- i )
#define CLR( a , x ) memset ( a , x , sizeof a )
#define CPY( a , x ) memcpy ( a , x , sizeof a )

const int MAXN = 105 ;
const int MAXQ = 105 ;
const int MAXE = 23333 ;
const int INF = 0x3f3f3f3f ;

struct Edge {
	int v , c , n ;
	Edge () {}
	Edge ( int v , int c , int n ) : v ( v ) , c ( c ) , n ( n ) {}
} ;

struct Net {
	Edge E[MAXE] ;
	int H[MAXN] , cntE ;
	int d[MAXN] , pre[MAXN] , cur[MAXN] , num[MAXN] ;
	int Q[MAXQ] , head , tail ;
	int s , t , nv ;
	int flow ;
	
	int n , m ;
	bool vis[MAXN] ;
	
	void init () {
		cntE = 0 ;
		CLR ( H , -1 ) ;
	}
	
	void addedge ( int u , int v , int c ) {
		E[cntE] = Edge ( v , c , H[u] ) ;
		H[u] = cntE ++ ;
		E[cntE] = Edge ( u , 0 , H[v] ) ;
		H[v] = cntE ++ ;
	}
	
	void rev_bfs () {
		CLR ( d , -1 ) ;
		CLR ( num , 0 ) ;
		head = tail = 0 ;
		Q[tail ++] = s ;
		num[0] = 1 ;
		d[t] = 0 ;
		while ( head != tail ) {
			int u = Q[head ++] ;
			for ( int i = H[u] ; ~i ; i = E[i].n ) {
				int v = E[i].v ;
				if ( d[v] == -1 ) {
					d[v] = d[u] + 1 ;
					Q[tail ++] = v ;
					num[d[v]] ++ ;
				}
			}
		}
	}
	
	int ISAP () {
		CPY ( cur , H ) ;
		rev_bfs () ;
		flow = 0 ;
		int u = pre[s] = s , i , mmin , pos ;
		while ( d[s] < nv ) {
			if ( u == t ) {
				int f = INF ;
				for ( i = s ; i != t ; i = E[cur[i]].v )
					if ( f > E[cur[i]].c ) {
						f = E[cur[i]].c ;
						pos = i ;
					}
				for ( i = s ; i != t ; i = E[cur[i]].v ) {
					E[cur[i]].c -= f ;
					E[cur[i] ^ 1].c += f ;
				}
				flow += f ;
				u = pos ;
			}
			for ( i = cur[u] ; ~i ; i = E[i].n )
				if ( E[i].c && d[u] == d[E[i].v] + 1 )
					break ;
			if ( ~i ) {
				cur[u] = i ;
				pre[E[i].v] = u ;
				u = E[i].v ;
			}
			else {
				if ( 0 == num[d[u]] )
					break ;
				for ( mmin = nv , i = H[u] ; ~i ; i = E[i].n )
					if ( E[i].c && mmin > d[E[i].v] ) {
						cur[u] = i ;
						mmin = d[E[i].v] ;
					}
				d[u] = mmin + 1 ;
				num[d[u]] ++ ;
				u = pre[u] ;
			}
		}
		return flow ;
	}
	
	void dfs ( int u ) {
		vis[u] = 1 ;
		for ( int i = H[u] ; ~i ; i = E[i].n )
			if ( E[i].c && !vis[E[i].v] )
				dfs ( E[i].v ) ;
	}
	
	void solve () {
		int x , p , v ;
		int val = 0 , cnt = 0 ;
		s = 0 , t = n + 1 , nv = t + 1 ;
		init () ;
		FOR ( u , 1 , n ) {
			scanf ( "%d%d" , &x , &p ) ;
			if ( x > 0 ) {
				val += x ;
				addedge ( s , u ,  x ) ;
			} else if ( x < 0 )
				addedge ( u , t , -x ) ;
			while ( p -- ) {
				scanf ( "%d" , &v ) ;
				addedge ( u , v , INF ) ;
			}
		}
		ISAP () ;
		if ( val - flow == 0 )
			printf ( "Refused\n" ) ;
		else {
			CLR ( vis , 0 ) ;
			dfs ( s ) ;
			FOR ( i , 1 , n )
				if ( vis[i] )
					++ cnt ;
			printf ( "%d %d\n" , val - flow , cnt ) ;
			FOR ( i , 1 , n )
				if ( vis[i] ) {
					-- cnt ;
					printf ( "%d%c" , i , cnt ? ' ' : '\n' ) ;
				}
		}
	}
} sap ;

int main () {
	while ( ~scanf ( "%d" , &sap.n ) && sap.n )
		sap.solve () ;
	return 0 ;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值