萌新求助,WA 50分
查看原帖
萌新求助,WA 50分
562582
吾名有灵楼主2021/9/27 23:15

感觉没啥问题啊,能有哪位大佬帮忙看看嘛?

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

const int N = 200005 ;
int n , m , res , siz ;
int t[N][2] , fa[N] , rev[N] , q[N] , top , tin[N] , wq[N] ;

struct Edge
{
	int u , v , a , b ;
	inline friend bool operator < ( Edge x , Edge y )
	{
		return x .a < y .a ;
	}
} ed[N] ;

void pushup ( int x )
{
	tin [ x ] = wq [ x ] ;
	if ( t [ x ] [ 0 ] && ed [ tin [ t [ x ] [ 0 ] ] ] .b > ed [ tin [ x ] ] .b )
		tin [ x ] = tin [ t [ x ] [ 0 ] ] ;
	if ( t [ x ] [ 1 ] && ed [ tin [ t [ x ] [ 1 ] ] ] .b > ed [ tin [ x ] ] .b )
		tin [ x ] = tin [ t [ x ] [ 1 ] ] ;
}

void pushdown ( int x )
{
	if ( rev [ x ] )
	{
		rev [ t [ x ] [ 0 ] ] ^= 1 ;
		rev [ t [ x ] [ 1 ] ] ^= 1 ;
		rev [ x ] ^= 1 ;
		swap ( t [ x ] [ 0 ] , t [ x ] [ 1 ] ) ;
	}
}

bool isroot ( int x )
{
	return t [ fa [ x ] ] [ 0 ] != x && t [ fa [ x ] ] [ 1 ] != x ;
}

void rotate ( int x )
{
	int y = fa [ x ] , z = fa [ y ] , l , r ;
	if ( t [ y ] [ 0 ] == x )
		l = 0 ;
	else
		l = 1 ;
	r = l ^ 1 ;
	if ( ! isroot ( y ) )
	{
		if ( t [ z ] [ 0 ] == y )
			t [ z ] [ 0 ] = x ;
		else
			t [ z ] [ 1 ] = x ;
	}
	fa [ x ] = z ;
	fa [ y ] = x ;
	fa [ t [ x ] [ r ] ] = y ;
	t [ y ] [ l ] = t [ x ] [ r ] ;
	t [ x ] [ r ] = y ;
	pushup ( y ) ;
	pushup ( x ) ;
}

void splay ( int x )
{
	q [ top = 1 ] = x ;
	for ( int i = x ; ! isroot ( i ) ; i = fa [ i ] )
		q [ ++ top ] = fa [ i ] ;
	for ( int i = top ; i ; -- i )
		pushdown ( q [ i ] ) ;
	while ( ! isroot ( x ) )
	{
		int y = fa [ x ] , z = fa [ y ] ;
		if ( ! isroot ( y ) )
		{
			if ( ( t [ y ] [ 0 ] == x ) ^ ( t [ z ] [ 0 ] == y ) )
				rotate ( x ) ;
			else
				rotate ( y ) ;
		}
		rotate ( x ) ;
	}
}

void access ( int x )
{
	for ( int y = 0 ; x ; y = x , x = fa [ x ] )
	{
		splay ( x ) ;
		t [ x ] [ 1 ] = y ;
		pushup ( x ) ;
	}
}

void makeroot ( int x )
{
	access ( x ) ;
	splay ( x ) ;
	rev [ x ] ^= 1 ;
}

int find ( int x )
{
	access ( x ) ;
	splay ( x ) ;
	while ( t [ x ] [ 0 ] )
		x = t [ x ] [ 0 ] ;
	return x ;
}

void split ( int x , int y )
{
	makeroot ( x ) ;
	access ( y ) ;
	splay ( y ) ;
}

void cut ( int x , int y )
{
	split ( x , y ) ;
	if ( t [ x ] [ 1 ] == 0 && t [ y ] [ 0 ] == x )
	{
		t [ y ] [ 0 ] = 0 ;
		fa [ x ] = 0 ;
	}
}

void link ( int x , int y )
{
	makeroot ( x ) ;
	fa [ x ] = y ;
}

int main ( )
{
//	freopen ( "2387_2.in" , "r" , stdin ) ;
	cin >> n >> m ;
	for ( int i = 1 ; i <= m ; ++ i )
	{
		cin >> ed [ i ] .u >> ed [ i ] .v >> ed [ i ] .a >> ed [ i ] .b ;
		res += ed [ i ] .a + ed [ i ] .b ;
	}
	sort ( ed + 1 , ed + 1 + m ) ;
//	ed [ 0 ] .a = ed [ 0 ] .b = res ;
	int ft = 1 ;
	for ( int i = 1 ; i <= m ; ++ i )
	{
		int u = ed [ i ] .u , v = ed [ i ] .v ;
		if ( u == v )
			continue ;
//		cout << u << " - " << v << " a : " << ed [ i ] .a << " , " << " b : " << ed [ i ] .b << endl ;
		if ( find ( u ) == find ( v ) )
		{
			split ( u , v ) ;
			int k = tin [ v ] ;
			if ( ed [ k ] .b < ed [ i ] .b )
				continue ;
			cut ( ed [ k ] .u , k + n ) ;
			cut ( k + n , ed [ k ] .v ) ;
			-- siz ;
		}
		wq [ i + n ] = i ;
		link ( u , n + i ) ;
		link ( n + i , v ) ;
		++ siz ;
		if ( find ( 1 ) == find ( n ) )
		{
//			cout << i << " to " << tin [ n ] << endl ;
//			cout << " max : " << ed [ tin [ n ] ] .u << " - " << ed [ tin [ n ] ] .v << " a : " << ed [ tin [ n ] ] .a << " b : " << ed [ tin [ n ] ] .b << endl ;
			split ( 1 , n ) ;
			res = min ( res , ed [ i ] .a + ed [ tin [ n ] ] .b ) ;
			ft = 0 ;
		}
	}
	if ( ft )
		puts ( "-1" ) ;
	else
		cout << res << endl ;
	return 0 ;
}

恳求纠错

2021/9/27 23:15
加载中...