zoukankan      html  css  js  c++  java
  • luoguP5024 保卫王国

    题目链接

    问题分析

    其实是比较明显的动态DP。

    懒于再推一遍式子,直接用 最小权点覆盖=全集-最大权独立集,然后就和这道题一样了。题解可以看这里

    然后必须选或者不选的话,就直接把相应的点权变成$-infty$或$infty$就好了。如果是必须选,最后答案里不要忘了加回原来的值。

    参考程序

    #include <bits/stdc++.h>
    using namespace std;
    
    const long long Maxn = 100010;
    const long long INF = 10000000010;
    struct matrix {
    	long long A[ 2 ][ 2 ];
    	matrix() {
    		A[ 0 ][ 0 ] = A[ 0 ][ 1 ] = A[ 1 ][ 0 ] = A[ 1 ][ 1 ] = -INF;
    		return;
    	}
    	matrix( long long *T ) {
    		A[ 0 ][ 0 ] = A[ 0 ][ 1 ] = T[ 0 ];
    		A[ 1 ][ 0 ] = T[ 1 ];
    		A[ 1 ][ 1 ] = -INF;
    		return;
    	}
    	matrix( long long a, long long b, long long c, long long d ) {
    		A[ 0 ][ 0 ] = a; A[ 0 ][ 1 ] = b; A[ 1 ][ 0 ] = c; A[ 1 ][ 1 ] = d;
    		return;
    	}
    	inline matrix operator * ( const matrix Other ) const {
    		return matrix( max( A[ 0 ][ 0 ] + Other.A[ 0 ][ 0 ], A[ 0 ][ 1 ] + Other.A[ 1 ][ 0 ] ),
    				max( A[ 0 ][ 0 ] + Other.A[ 0 ][ 1 ], A[ 0 ][ 1 ] + Other.A[ 1 ][ 1 ] ),
    				max( A[ 1 ][ 0 ] + Other.A[ 0 ][ 0 ], A[ 1 ][ 1 ] + Other.A[ 1 ][ 0 ] ),
    				max( A[ 1 ][ 0 ] + Other.A[ 0 ][ 1 ], A[ 1 ][ 1 ] + Other.A[ 1 ][ 1 ] ) );
    	}
    };
    struct edge {
    	long long To, Next;
    	edge() : To( 0 ), Next( 0 ) {}
    	edge( long long _To, long long _Next ) : To( _To ), Next( _Next ) {}
    };
    edge Edge[ Maxn << 1 ];
    long long Start[ Maxn ], UsedEdge;
    inline void AddEdge( long long x, long long y ) {
    	Edge[ ++UsedEdge ] = ( edge ) { y, Start[ x ] };
    	Start[ x ] = UsedEdge;
    	return;
    }
    long long n, m, Cost[ Maxn ], NowCost[ Maxn ], Sum;
    char Ch[ 10 ];
    long long Deep[ Maxn ], Father[ Maxn ], Size[ Maxn ], Son[ Maxn ], Top[ Maxn ], Index[ Maxn ], Ref[ Maxn ], Dfn[ Maxn ], Used;
    long long Dp[ Maxn ][ 2 ], LDp[ Maxn ][ 2 ];
    matrix Tree[ Maxn << 2 ];
    
    void Build_Cut();
    void Build();
    void Change( long long u, long long Key );
    matrix Query( long long Ind, long long Left, long long Right, long long L, long long R );
    
    int main() {
    	scanf( "%lld%lld", &n, &m ); scanf( "%s", Ch );
    	for( long long i = 1; i <= n; ++i ) scanf( "%lld", &Cost[ i ] );
    	for( long long i = 1; i < n; ++i ) {
    		long long x, y; scanf( "%lld%lld", &x, &y );
    		AddEdge( x, y ); AddEdge( y, x );
    	}
    	Build_Cut();
    	Build();
    	for( long long i = 1; i <= m; ++i ) {
    		long long a, x, b, y;
    		scanf( "%lld%lld%lld%lld", &a, &x, &b, &y);
    		if( x == 0 ) { Sum -= Cost[ a ]; Sum += INF; Change( a, INF ); }
    		else { Sum -= Cost[ a ]; Sum += -INF; Change( a, -INF ); }
    		if( y == 0 ) { Sum -= Cost[ b ]; Sum += INF; Change( b, INF ); }
    		else { Sum -= Cost[ b ]; Sum += -INF; Change( b, -INF ); }
    
    		matrix Temp = Query( 1, 1, n, Index[ 1 ], Dfn[ 1 ] );
    		long long Ans = max( Temp.A[ 0 ][ 0 ], Temp.A[ 1 ][ 0 ] );
    		Ans = Sum - Ans;
    		if( x == 1 ) Ans = Ans + INF + Cost[ a ];
    		if( y == 1 ) Ans = Ans + INF + Cost[ b ];
    		if( Ans >= INF ) printf( "-1
    " ); else printf( "%lld
    ", Ans );
    
    		if( x == 0 ) { Sum -= INF; Sum += Cost[ a ]; Change( a, Cost[ a ] ); } 
    		else { Sum -= -INF; Sum += Cost[ a ]; Change( a, Cost[ a ] ); }
    		if( y == 0 ) { Sum -= INF; Sum += Cost[ b ]; Change( b, Cost[ b ] ); }
    		else { Sum -= -INF; Sum += Cost[ b ]; Change( b, Cost[ b ] ); }
    	}
    	return 0;
    }
    
    void Dfs1( long long u, long long Fa ) {
    	Deep[ u ] = Deep[ Fa ] + 1;
    	Father[ u ] = Fa;
    	Size[ u ] = 1;
    	for( long long t = Start[ u ]; t; t = Edge[ t ].Next ) {
    		long long v = Edge[ t ].To;
    		if( v == Fa ) continue;
    		Dfs1( v, u );
    		if( Size[ v ] > Size[ Son[ u ] ] ) Son[ u ] = v;
    		Size[ u ] += Size[ v ];
    	}
    	return;
    }
    
    void Dfs2( long long u, long long Fa ) {
    	if( Son[ u ] ) {
    		Top[ Son[ u ] ] = Top[ u ];
    		Index[ Son[ u ] ] = ++Used;
    		Ref[ Used ] = Son[ u ];
    		Dfs2( Son[ u ], u );
    	}
    	for( long long t = Start[ u ]; t; t = Edge[ t ].Next ) {
    		long long v = Edge[ t ].To;
    		if( v == Fa || v == Son[ u ] ) continue;
    		Top[ v ] = v; Index[ v ] = ++Used; Ref[ Used ] = v;
    		Dfs2( v, u );
    	}
    	return;
    }
    
    void Build_Cut() {
    	Dfs1( 1, 0 );
    	Top[ 1 ] = 1; Index[ 1 ] = ++Used; Ref[ Used ] = 1;
    	Dfs2( 1, 0 );
    	for( int i = 1; i <= n; ++i ) Dfn[ Top[ i ] ] = max( Dfn[ Top[ i ] ], Index[ i ] );
    	return;
    }
    
    void Dfs3( long long u, long long Fa ) {
    	LDp[ u ][ 1 ] = NowCost[ u ];
    	for( long long t = Start[ u ]; t; t = Edge[ t ].Next ) {
    		long long v = Edge[ t ].To;
    		if( v == Fa || v == Son[ u ] ) continue;
    		Dfs3( v, u );
    		LDp[ u ][ 0 ] += max( Dp[ v ][ 0 ], Dp[ v ][ 1 ] );
    		LDp[ u ][ 1 ] += Dp[ v ][ 0 ];
    	}
    	if( Son[ u ] ) Dfs3( Son[ u ], u );
    	Dp[ u ][ 0 ] = LDp[ u ][ 0 ] + max( Dp[ Son[ u ] ][ 0 ], Dp[ Son[ u ] ][ 1 ] );
    	Dp[ u ][ 1 ] = LDp[ u ][ 1 ] + Dp[ Son[ u ] ][ 0 ];
    	return;
    }
    
    void RealBuild( long long Ind, long long Left, long long Right ) {
    	if( Left == Right ) {
    		Tree[ Ind ] = matrix( LDp[ Ref[ Left ] ] );
    		return;
    	}
    	long long Mid = ( Left + Right ) >> 1;
    	RealBuild( Ind << 1, Left, Mid );
    	RealBuild( Ind << 1 | 1, Mid + 1, Right );
    	Tree[ Ind ] = Tree[ Ind << 1 ] * Tree[ Ind << 1 | 1 ];
    	return;
    }
    
    matrix Query( long long Ind, long long Left, long long Right, long long L, long long R ) {
    	if( L <= Left && Right <= R ) return Tree[ Ind ];
    	long long Mid = ( Left + Right ) >> 1;
    	if( R <= Mid ) return Query( Ind << 1, Left, Mid, L, R );
    	if( L > Mid ) return Query( Ind << 1 | 1, Mid + 1, Right, L, R );
    	return Query( Ind << 1, Left, Mid, L, R ) * Query( Ind << 1 | 1, Mid + 1, Right, L, R );
    }
    
    void Build() {
    	for( long long i = 1; i <= n; ++i ) NowCost[ i ] = Cost[ i ];
    	for( long long i = 1; i <= n; ++i ) Sum += NowCost[ i ];
    	Dfs3( 1, 0 );
    	RealBuild( 1, 1, n );
    	return;
    }
    
    void Update( long long Ind, long long Left, long long Right, long long x ) {
    	if( Left == Right ) {
    		Tree[ Ind ] = matrix( LDp[ Ref[ Left ] ] );
    		return;
    	}
    	long long Mid = ( Left + Right ) >> 1;
    	if( x <= Mid ) Update( Ind << 1, Left, Mid, x );
    	if( x > Mid ) Update( Ind << 1 | 1, Mid + 1, Right, x );
    	Tree[ Ind ] = Tree[ Ind << 1 ] * Tree[ Ind << 1 | 1 ];
    	return;
    }
    
    void Change( long long u, long long Key ) {
    	matrix Last = Query( 1, 1, n, Index[ Top[ u ] ], Dfn[ Top[ u ] ] );
    	LDp[ u ][ 1 ] += -NowCost[ u ] + Key; NowCost[ u ] = Key;
    	Update( 1, 1, n, Index[ u ] );
    	matrix Temp = Query( 1, 1, n, Index[ Top[ u ] ], Dfn[ Top[ u ] ] );
    	u = Father[ Top[ u ] ];
    	while( u ) {
    		matrix TTT = Query( 1, 1, n, Index[ Top[ u ] ], Dfn[ Top[ u ] ] );
    		LDp[ u ][ 0 ] += -max( Last.A[ 0 ][ 0 ], Last.A[ 1 ][ 0 ] ) + max( Temp.A[ 0 ][ 0 ], Temp.A[ 1 ][ 0 ] );
    		LDp[ u ][ 1 ] += -Last.A[ 0 ][ 0 ] + Temp.A[ 0 ][ 0 ];
    		Last = TTT;
    		Update( 1, 1, n, Index[ u ] );
    		Temp = Query( 1, 1, n, Index[ Top[ u ] ], Dfn[ Top[ u ] ] );
    		u = Father[ Top[ u ] ];
    	}
    	return;
    }
    
  • 相关阅读:
    PAT 1025. 反转链表 (25)
    PAT 1024. 科学计数法 (20)
    PAT 1076. Forwards on Weibo (30)
    C++——cout输出小数点后指定位数
    PTA 06-图3 六度空间 (30分)
    PTA 06-图2 Saving James Bond
    PTA
    浙大PTA
    浙大PTA
    随机密码生成
  • 原文地址:https://www.cnblogs.com/chy-2003/p/11561812.html
Copyright © 2011-2022 走看看