QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#562228#8941. Even or Odd Spanning Treeucup-team3160#WA 117ms12040kbC++142.8kb2024-09-13 15:54:052024-09-13 15:54:14

Judging History

你现在查看的是最新测评结果

  • [2024-09-13 15:54:14]
  • 评测
  • 测评结果:WA
  • 用时:117ms
  • 内存:12040kb
  • [2024-09-13 15:54:05]
  • 提交

answer

# include <bits/stdc++.h>

using namespace std ;

int n , m ;
struct Edge
{
	int to , next , w ;
} g[1000005] ;
int fte[500005] , gsz ;
void addedge( int x , int y , int z )
{
	g[++ gsz] = ( Edge ) { y , fte[x] , z } ;
	fte[x] = gsz ;
}
int f[200005] ;
int get(int x )
{
	if ( f[x] == x ) return x ;
	return f[x] = get( f[x] ) ;
}
void merge( int x , int y )
{
	f[get( x )] = get( f[y] ) ;
}
typedef long long ll ;
const int INF = 1e9 +5 ;
int maxv[200005][21][2] , fa[200005][21] ;
vector < pair< int , pair< int , int > > > gr ;
int deap[200005] ;
void dfs1( int x , int father )
{
	deap[x] = deap[father] + 1 ;
	fa[x][0] = father ;
	for ( int j = 1 ; j <= 20 ; j++ )
	{
		fa[x][j] = fa[fa[x][j - 1]][j - 1] ;
		for ( int t = 0 ; t <= 1 ; t++ )
			maxv[x][j][t] = max( maxv[x][j - 1][t] , 
			maxv[fa[x][j - 1]][j - 1][t] ) ;
	}
	for ( int i = fte[x] ; i ; i = g[i].next )
	{
		int y = g[i].to ;
		if ( y == father ) continue ;
		maxv[y][0][g[i].w & 1] = g[i].w ;
		dfs1( y , x ) ;
	}
}
int query( int x , int y , int t )
{
	if (deap[x] < deap[y] ) swap( x , y ) ;
	int ans = 0 ;
	for ( int i = 20 ; i >= 0 ; i-- )
		if ( deap[fa[x][i]] >= deap[y] ) 
		{
			ans = max( ans , maxv[x][i][t] ) ;
			x = fa[x][i] ;
		}
	if ( x == y ) return ans ;
	for ( int i = 20 ; i >= 0 ; i-- )
		if ( fa[x][i] != fa[y][i] ) 
		{
			ans = max( ans , max( maxv[x][i][t] , maxv[y][i][t] ) ) ;
			x = fa[x][i] , y = fa[y][i] ;
		}
	ans = max( ans , max(maxv[x][0][t] , maxv[y][0][t] ) ) ;
	return ans ;
}
void solve()
{
	scanf("%d%d" , &n , &m) ;
	gr.clear() ;
	gsz = 0 ;
	for ( int i = 1 ; i <= n ; i++ ) fte[i] = 0 , f[i] = i ;
	for ( int i = 1 ; i <= n ; i++ )
	{
		maxv[i][0][0] = maxv[i][0][1] = 0 ;
	}
	for ( int i = 1 ; i <= m ; i++)
	{
		int x , y , z ;
		scanf("%d%d%d" , &x , &y , &z) ;
		gr.push_back( { z , { x , y } } ) ;
	}
	sort( gr.begin() , gr.end() ) ;
	int ss = 0 ;
	ll ans = 0 ;
	int tt = 0 ;
	for ( auto t : gr )
	{
		int x = t.second.first , y = t.second.second ;
		int z = t.first ;
//		printf("now:%d %d %d\n" , x , y , z) ;
		if ( get( x ) == get( y ) ) continue ;
		addedge( x , y , z ) , addedge( y , x , z ) ;
		merge( x , y ) ;
		ss ^= ( z & 1 ) ;
		ans += z ;
		tt++ ;
	}
	deap[1] = 0 ;
	dfs1( 1 , 1 ) ;
	ll ans2 = ans + INF ;
	for ( auto t : gr )
	{
		int x = t.second.first , y = t.second.second ;
		int z = t.first ;
		int nv = query( x , y , ( z & 1 ) ^ 1 ) ;
		if ( nv ) ans2 = min( ans2 , ans - nv + z ) ;
	}
	if ( tt != n - 1 ) puts("-1 -1") ;
	else
	{
		if ( ans2 == ans + INF ) ans2 = -1 ;
		if (! ans & 1 ) printf("%lld %lld\n" , ans , ans2) ;
		else printf("%lld %lld\n" , ans2 , ans) ;
	}
}

int main()
{
	int t ;
	scanf("%d" , &t) ;
	while ( t -- ) solve() ;
	return 0 ;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 10068kb

input:

3
2 1
1 2 5
3 1
1 3 1
4 4
1 2 1
1 3 1
1 4 1
2 4 2

output:

-1 5
-1 -1
4 3

result:

ok 6 numbers

Test #2:

score: -100
Wrong Answer
time: 117ms
memory: 12040kb

input:

10000
16 50
1 2 649251632
2 3 605963017
3 4 897721528
4 5 82446151
5 6 917109168
6 7 79647183
7 8 278566178
7 9 573504723
3 10 679859251
8 11 563113083
1 12 843328546
10 13 594049160
11 14 997882839
7 15 569431750
2 16 244494799
16 7 960172373
13 4 317888322
13 3 446883598
9 3 678142319
5 8 43208692...

output:

3159441841 3140067932
1309454727 1262790434
1307926149 1263932600
1189242112 1180186165
2261370885 2248358640
3776889482 3738936375
1093500444 1058677117
3433711014 3402127725
1201099898 1187873655
1410162043 1395482806
3486092007 3456885934
3988876469 3943951826
2535532661 2479987500
2930167271 290...

result:

wrong answer 1st numbers differ - expected: '3140067932', found: '3159441841'