「NOI2014」魔法森林

题目链接

题目分析

实则毫无思路……

由于权值是两维的,首先尝试正常操作排序一维。然后问题就变成了求另外一维的最小。不难想到另外一维是一个最小生成树。每次的答案就是\(1\)\(n\)路径上的两个最大加起来(不一定是枚举的一维加上另一维的最大)。

然后考虑如何维护。动态加边的最小生成树自然用LCT维护。由于LCT维护的是点值,所以我们要把边拆成一个点加两条边,点上维护原来边上的值。然后直接套LCT维护max即可。

参考程序

由于尝试封装了一下,所以代码巨翔无比……大家可以直接跳过LCT部分。如果要学习LCT的可以戳这里

#include <bits/stdc++.h>
using namespace std;

	struct LCTChild {
		int Child[ 2 ];
	};
	struct LCT {
		int *Father, *Stack, *Tag;
		LCTChild *Child;
		LCT() {}
		LCT( int *_Father, LCTChild *_Child, int *_Stack, int *_Tag ) : Father( _Father ), Child( _Child ), Stack( _Stack ), Tag( _Tag ) {}
		void Clear() { memset( Father, 0, sizeof( Father ) ); memset( Tag, 0, sizeof( Tag ) ); memset( Child, 0, sizeof( Child ) ); return; }
		void Collect( int Index );
		void TagDown( int x );
		bool IsRoot( int x ) { return !( ( Child[ Father[ x ] ].Child[ 0 ] == x ) || ( Child[ Father[ x ] ].Child[ 1 ] == x ) ); }
		void Rotate( int C ) {
			int B = Father[ C ]; int A = Father[ B ]; int Tag = Child[ B ].Child[ 1 ] == C;
			if( !IsRoot( B ) ) Child[ A ].Child[ Child[ A ].Child[ 1 ] == B ] = C; Father[ C ] = A;
			Father[ Child[ C ].Child[ Tag ^ 1 ] ] = B; Child[ B ].Child[ Tag ] = Child[ C ].Child[ Tag ^ 1 ];
			Child[ C ].Child[ Tag ^ 1 ] = B; Father[ B ] = C;
			Collect( B ); Collect( C );
			return;
		}
		void Splay( int x ) {
			int Num = 0; Stack[ ++Num ] = x; int t = x; for( ; !IsRoot( t ); t = Father[ t ] ) Stack[ ++Num ] = Father[ t ]; for( int i = Num; i >= 1; --i ) TagDown( Stack[ i ] );
			while( !IsRoot( x ) ) { 
				int y = Father[ x ]; int z = Father[ y ]; if( !IsRoot( y ) ) if( ( Child[ z ].Child[ 0 ] == y ) ^ ( Child[ y ].Child[ 0 ] == z ) ) Rotate( x ); else Rotate( y ); Rotate( x );
			}
			Collect( x );
			return;
		}
		void Access( int x ) { for( int i = 0; x; i = x, x = Father[ x ] ) { Splay( x ); Child[ x ].Child[ 1 ] = i; Collect( x ); } return; }
		void MakeRoot( int x ) { Access( x ); Splay( x ); Tag[ x ] ^= 1; return; }
		int FindRoot( int x ) { Access( x ); Splay( x ); TagDown( x ); while( Child[ x ].Child[ 0 ] ) { x = Child[ x ].Child[ 0 ]; TagDown( x ); } Splay( x ); return x; }
		void Split( int x, int y ) { MakeRoot( x ); Access( y ); Splay( y ); return; }
		void Link( int x, int y ) { 
			MakeRoot( x ); if( FindRoot( y ) == x ) return; Father[ x ] = y; return; 
		}
		void Cut( int x, int y ) {
			MakeRoot( x ); if( FindRoot( y ) != x || Child[ y ].Child[ 0 ] || Father[ y ] != x ) return; Father[ y ] = Child[ x ].Child[ 1 ] = 0; Collect( x ); return; 
		}
	};
const int Maxn = 50010;
const int Maxm = 100010;
struct edge {
	int X, Y, A, B;
};
edge Edge[ Maxm ];
int N, M;
LCTChild Child[ Maxn + Maxm ];
int Father[ Maxn + Maxm ], Stack[ Maxn + Maxm ], Tag[ Maxn + Maxm ];
LCT Lct( Father, Child, Stack, Tag );
struct rec {
	int A, B;
};
rec Max[ Maxn + Maxm ], Value[ Maxn + Maxm ];
int From[ Maxn + Maxm ];
void LCT::Collect( int Index ) {
	Max[ Index ].A = max( Max[ Child[ Index ].Child[ 0 ] ].A, Max[ Child[ Index ].Child[ 1 ] ].A );
	Max[ Index ].B = max( Max[ Child[ Index ].Child[ 0 ] ].B, Max[ Child[ Index ].Child[ 1 ] ].B );
	if( Max[ Child[ Index ].Child[ 0 ] ].B > Max[ Child[ Index ].Child[ 1 ] ].B ) From[ Index ] = From[ Child[ Index ].Child[ 0 ] ];
	else From[ Index ] = From[ Child[ Index ].Child[ 1 ] ];
	Max[ Index ].A = max( Max[ Index ].A, Value[ Index ].A );
	if( Value[ Index ].B > Max[ Index ].B ) From[ Index ] = Index;
	Max[ Index ].B = max( Max[ Index ].B, Value[ Index ].B );
	return; 
}

void LCT::TagDown( int x ) {
	if( Tag[ x ] ) { 
		Tag[ x ] = 0; 
		std::swap( Child[ x ].Child[ 0 ], Child[ x ].Child[ 1 ] );
		Tag[ Child[ x ].Child[ 0 ] ] ^= 1; Tag[ Child[ x ].Child[ 1 ] ] ^= 1; 
	} 
	return;
}

bool Cmp( edge X, edge Y ) {
	return X.A < Y.A || X.A == Y.A && X.B < Y.B;
}

int main() {
	scanf( "%d%d", &N, &M );
	for( int i = 1; i <= M; ++i )
		scanf( "%d%d%d%d", &Edge[ i ].X, &Edge[ i ].Y, &Edge[ i ].A, &Edge[ i ].B );
	std::sort( Edge + 1, Edge + M + 1, Cmp );
	for( int i = 1; i <= M; ++i ) {
		Value[ i + N ].A = Max[ i + N ].A = Edge[ i ].A;
		Value[ i + N ].B = Max[ i + N ].B = Edge[ i ].B;
		From[ i + N ] = i + N;
	}
	int Ans = -1;
	for( int i = 1; i <= M; ++i ) {
		Lct.MakeRoot( Edge[ i ].X );
		if( Lct.FindRoot( Edge[ i ].Y ) != Edge[ i ].X ) {
			Lct.Link( Edge[ i ].X, i + N );
			Lct.Link( i + N, Edge[ i ].Y );
		} else {
			Lct.Split( Edge[ i ].Y, Edge[ i ].X );
			int T = From[ Edge[ i ].X ] - N;
			if( Edge[ T ].B > Edge[ i ].B ) {
				Lct.Cut( Edge[ T ].X, T + N );
				Lct.Cut( T + N, Edge[ T ].Y );
				Lct.Link( Edge[ i ].X, i + N );
				Lct.Link( i + N, Edge[ i ].Y );
			}
		}
		Lct.MakeRoot( 1 );
		if( Lct.FindRoot( N ) == 1 ) {
			Lct.Split( N, 1 );
			Ans = ( Ans == -1 ) ? ( Max[ 1 ].A + Max[ 1 ].B ) : min( Ans, Max[ 1 ].A + Max[ 1 ].B );
		}
	}
	printf( "%d\n", Ans );
	return 0;
}
posted @ 2019-07-26 18:46  chy_2003  阅读(133)  评论(0编辑  收藏  举报