QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#540780#8941. Even or Odd Spanning Treeucup-team4717#TL 638ms63800kbC++142.4kb2024-08-31 17:51:172024-08-31 17:51:17

Judging History

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

  • [2024-08-31 17:51:17]
  • 评测
  • 测评结果:TL
  • 用时:638ms
  • 内存:63800kb
  • [2024-08-31 17:51:17]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+5;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-48,ch=getchar();}
	return x*f;
}
int T,n,m;

struct Node{
	int u,v,w;
}e[N];

int t[N],val[N],f[N],sz[N],cnt,sum,idt;
inline int Find(int x){
	return x==f[x]?x:Find(f[x]);
}

vector<pair<int,int> > to[N];

bool cmp(Node a,Node b){
	return a.w<b.w;
}

void merge(int x,int y,int X,int Y,int w){
	to[X].emplace_back(Y,w);
	to[Y].emplace_back(X,w);
	if(sz[x]<sz[y]) f[x]=y,sz[y]+=sz[x];
	else f[y]=x,sz[x]+=sz[y];
	cnt++;
	sum+=w;
}

int fa[N],id[N],siz[N],idx;

void dfs(int u,int f){
	fa[u]=f,siz[u]=1,id[u]=++idx;
	for(pair<int,int> pl:to[u]){
		int v=pl.first,w=pl.second;
		if(v==f) continue;
		val[v]=w;
		dfs(v,u);
		siz[u]+=siz[v];
	}
}

int mn,wdf,sb,s1,s2;
bool flg=0;

void check(int x,int y){
	if(x==y) return;
	if(id[s1]>=id[x]&&id[s1]<=id[x]+siz[x]-1&&id[s2]>=id[x]&&id[s2]<=id[x]+siz[x]-1) return;	// if(t[x]>mn) mn=t[x],wdf=val[x];

	if((val[x]&1)^(sb&1)) mn=max(mn,val[x]),flg=1;
	check(fa[x],y);
}

/*
1
5 10
1 5 9
3 2 2
1 3 7
4 5 5
3 2 7
1 1 5
3 2 4
5 2 8
3 1 5
1 2 3
*/

signed main(){	
	// freopen("j.in","r",stdin);
	// freopen("test.out","w",stdout);
	T=read();
	while(T--){
		n=read(),m=read(),cnt=sum=idt=idx=0;
		for(int i=1;i<=m;i++){
			e[i].u=read();
			e[i].v=read();
			e[i].w=read();
		}
		sort(e+1,e+1+m,cmp);
		for(int i=1;i<=n;i++) f[i]=i,sz[i]=1;
		for(int i=1;i<=m;i++){
			int x=Find(e[i].u),y=Find(e[i].v);
			if(x==y) continue;
			merge(x,y,e[i].u,e[i].v,e[i].w);
			
		}
		dfs(1,0);
		// cout<<cnt<<"\n";
		if(cnt<n-1){
			printf("-1 -1\n");
			for(int i=1;i<=n;i++) t[i]=val[i]=0,to[i].clear();
			continue;
		}
		int ans1=sum,ans2=1e16;idt=0;
		for(int i=1;i<=n;i++) f[i]=i,sz[i]=1,to[i].clear();
		for(int i=1;i<=m;i++){
			int x=Find(e[i].u),y=Find(e[i].v);
			if(x==y){
				mn=0;
				sb=e[i].w,flg=0;
				s1=e[i].u,s2=e[i].v;
				check(e[i].u,1);
				check(e[i].v,1);

				if(flg) ans2=min(ans2,ans1-mn+e[i].w);
				continue;
			}
			merge(x,y,e[i].u,e[i].v,e[i].w);		
		}
		if(ans2==1e16) ans2=-1;
		if(ans1&1) swap(ans1,ans2);
		printf("%lld %lld\n",ans1,ans2);
		for(int i=1;i<=n;i++) t[i]=val[i]=0,to[i].clear();
	}
	return 0;
}
/*
1
3 4
2 1 5
2 1 0
3 1 2
2 3 9
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0
Accepted
time: 77ms
memory: 30384kb

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:

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

result:

ok 20000 numbers

Test #3:

score: 0
Accepted
time: 146ms
memory: 30708kb

input:

100
1806 5000
1 2 139994119
2 3 184924112
3 4 670813536
4 5 443325848
5 6 767909046
6 7 531742851
7 8 364385440
8 9 918195219
9 10 731896671
10 11 671688362
11 12 558665746
12 13 154497842
13 14 28636459
14 15 570343686
15 16 419626123
16 17 817852951
17 18 517701907
18 19 952451718
19 20 141747977
...

output:

380509244078 380509217939
236564714828 236564588423
317690341848 317690193297
416922743878 416923542879
411997066938 411996924725
231780454372 231780901543
156856996316 156856345151
239278757000 239278493321
288480848080 288481502909
347301406524 347301269265
362642903994 362643090967
158361335208 1...

result:

ok 200 numbers

Test #4:

score: 0
Accepted
time: 235ms
memory: 34608kb

input:

10
15547 50000
1 2 762013057
2 3 473514078
3 4 73078629
4 5 932961699
5 6 157436174
6 7 190061912
7 8 140204258
8 9 737720271
9 10 386190699
10 11 92718916
11 12 68384258
12 13 389445848
13 14 906761733
14 15 644882062
15 16 429774551
16 17 330771475
17 18 815808541
18 19 239714301
19 20 350847429
2...

output:

2833348523676 2833348592065
4133065384586 4133065395925
3395129351174 3395129357911
4109233311456 4109233300341
4201582590330 4201582656213
4055209304322 4055209286787
4274470524320 4274470529139
4221591172170 4221591328195
3155641613234 3155641574871
3656162030214 3656162010817

result:

ok 20 numbers

Test #5:

score: 0
Accepted
time: 638ms
memory: 60692kb

input:

1
200000 500000
1 2 485160054
2 3 698975729
3 4 100346974
4 5 820234671
5 6 389996728
6 7 914128102
7 8 439507064
8 9 938065130
9 10 353829140
2 11 391113348
7 12 685484428
8 13 492562017
7 14 269259412
4 15 133636977
7 16 189855044
16 17 393115842
9 18 196148136
13 19 272676317
1 20 778859832
12 21...

output:

46934564700640 46934564707403

result:

ok 2 number(s): "46934564700640 46934564707403"

Test #6:

score: 0
Accepted
time: 591ms
memory: 62604kb

input:

1
200000 500000
1 2 825868392
2 3 96775645
3 4 594837991
4 5 160657859
5 6 313806062
6 7 711860421
7 8 618363510
8 9 784016759
9 10 261473589
10 11 931365544
11 12 372625381
12 13 728196426
13 14 641630335
14 15 993361561
15 16 731849490
16 17 802998444
17 18 222098249
18 19 16513016
19 20 354524457...

output:

46801911175000 46801911171749

result:

ok 2 number(s): "46801911175000 46801911171749"

Test #7:

score: 0
Accepted
time: 618ms
memory: 60652kb

input:

1
200000 500000
1 2 842585427
2 3 791026412
3 4 843625151
4 5 729586460
5 6 157528721
6 7 846617054
7 8 737344300
8 9 915918783
9 10 697382206
10 11 647361071
11 12 438594908
12 13 472804786
13 14 426537066
14 15 93901471
15 16 183637656
16 17 785203928
17 18 741855632
18 19 800547153
19 20 58723676...

output:

46131143829898 46131143836751

result:

ok 2 number(s): "46131143829898 46131143836751"

Test #8:

score: 0
Accepted
time: 535ms
memory: 60640kb

input:

1
200000 500000
1 2 21536574
2 3 104440599
3 4 507646483
4 5 853564547
5 6 30231389
6 7 81344061
7 8 85488147
8 9 425069154
9 10 511681492
10 11 443781660
11 12 377608322
12 13 453040416
13 14 233386703
14 15 723131321
15 16 438749889
16 17 801233515
17 18 454069326
18 19 846692740
19 20 972424264
2...

output:

45558621285148 45558621284239

result:

ok 2 number(s): "45558621285148 45558621284239"

Test #9:

score: 0
Accepted
time: 553ms
memory: 62728kb

input:

1
200000 500000
1 2 519568971
2 3 853629719
3 4 300838777
4 5 853468350
5 6 593058273
6 7 370503500
7 8 46893088
8 9 571232687
9 10 657711042
10 11 271196301
11 12 331144994
12 13 715838787
13 14 53042548
14 15 115867361
15 16 766702189
16 17 731685076
17 18 362857236
18 19 413501522
19 20 397702264...

output:

45173663952580 45173663950355

result:

ok 2 number(s): "45173663952580 45173663950355"

Test #10:

score: 0
Accepted
time: 74ms
memory: 30388kb

input:

10000
20 50
1 2 147473388
2 3 605402240
3 4 522464618
2 5 707965800
3 6 3583550
6 7 268408004
4 8 462945122
8 9 474541356
2 10 205726788
5 11 330841004
6 12 523365722
12 13 941306492
2 14 573980412
2 15 156244250
14 16 507216542
11 17 463384792
10 18 473275012
6 19 345891816
5 20 533722940
10 1 6981...

output:

4008710456 4017336847
4946409492 4992621285
4696218760 4704258231
4674921744 4695521831
4946679402 4945691427
4478698910 4497256515
4783526390 4814213127
6123208914 6165320103
4133051540 4120131291
4194209082 4200997377
5060476734 5053009795
2412375120 2417221069
3330898760 3347313551
4382613900 437...

result:

ok 20000 numbers

Test #11:

score: 0
Accepted
time: 121ms
memory: 30864kb

input:

100
1610 5000
1 2 904287524
2 3 75953162
3 4 12435440
4 5 361167090
5 6 644441712
6 7 581450442
7 8 861614552
8 9 252436848
9 10 846939964
10 11 181497206
11 12 455157234
12 13 980036724
13 14 562452946
14 15 167199106
15 16 976061800
16 17 264541878
17 18 450292904
18 19 570015780
19 20 941168460
2...

output:

310919021480 310918978087
169799021650 169798866427
165431158820 165431087973
179832505830 179832156959
144382457024 144382154811
306492431780 306492339197
312896210638 312896338249
207725167692 207724961945
226476662054 226476300761
284720963798 284721089955
46893319730 46911208805
53207690380 5322...

result:

ok 200 numbers

Test #12:

score: 0
Accepted
time: 218ms
memory: 34272kb

input:

10
17574 50000
1 2 166340794
2 3 373972798
3 4 522169738
4 5 581967928
5 6 507291538
6 7 664693732
7 8 181493270
8 9 761378548
9 10 555703930
10 11 520699512
11 12 768435200
12 13 107051412
13 14 521048410
14 15 567386926
15 16 450543048
16 17 756344634
17 18 103405654
18 19 25262168
19 20 913368074...

output:

3656979404008 3656979434619
354537184370 354544063625
341759707004 341768797573
3404306510292 3404306513389
2541263693920 2541263703985
3113759618770 3113759622361
1913085413634 1913085422989
6249974944498 6250083092549
2976390899750 2976390845957
3512683798248 3512786834297

result:

ok 20 numbers

Test #13:

score: 0
Accepted
time: 449ms
memory: 61572kb

input:

1
177205 500000
1 2 18290056
2 3 11655538
3 4 16963148
4 5 2641924
5 6 25637680
6 7 4730356
7 8 27146378
8 9 15652194
9 10 14890628
10 11 19365486
11 12 3820786
12 13 11452196
13 14 5102768
14 15 7439266
15 16 25369914
16 17 14116212
17 18 3698702
18 19 4063592
19 20 1996234
20 21 13130350
21 22 146...

output:

2252002554076 2252002553015

result:

ok 2 number(s): "2252002554076 2252002553015"

Test #14:

score: 0
Accepted
time: 463ms
memory: 63280kb

input:

1
172216 500000
1 2 956765280
2 3 973677722
3 4 837393398
4 5 946237454
5 6 949499478
6 7 888941620
7 8 948636246
8 9 854806028
9 10 886928282
10 11 993073674
11 12 998847262
12 13 837823962
13 14 960884892
14 15 939320696
15 16 918711588
16 17 954208380
17 18 992095856
18 19 948877780
19 20 9919445...

output:

75272260956632 75272252843281

result:

ok 2 number(s): "75272260956632 75272252843281"

Test #15:

score: 0
Accepted
time: 403ms
memory: 60544kb

input:

1
155293 500000
1 2 384804188
2 3 63364234
3 4 177584976
4 5 328074256
5 6 275790972
6 7 378072002
7 8 54303408
8 9 383022922
9 10 387817490
10 11 86278032
11 12 64413800
12 13 187511028
13 14 321677058
14 15 228514696
15 16 43055978
16 17 177073484
17 18 138589066
18 19 67320634
19 20 119232370
20 ...

output:

24663756079214 24663756073521

result:

ok 2 number(s): "24663756079214 24663756073521"

Test #16:

score: 0
Accepted
time: 431ms
memory: 62376kb

input:

1
191060 500000
1 2 588860046
2 3 621642264
3 4 882088964
4 5 572796092
5 6 891347164
6 7 684474878
7 8 691206420
8 9 637066324
9 10 667218786
10 11 573407158
11 12 762042024
12 13 843998480
13 14 806619014
14 15 817306462
15 16 682991688
16 17 905928094
17 18 849964156
18 19 780739198
19 20 9264754...

output:

67741268383444 67741175242067

result:

ok 2 number(s): "67741268383444 67741175242067"

Test #17:

score: 0
Accepted
time: 480ms
memory: 59828kb

input:

1
182135 500000
1 2 574248454
2 3 688014902
3 4 599110558
4 5 734847794
5 6 569915634
6 7 497264762
7 8 733196458
8 9 479258008
9 10 472456988
10 11 516257788
11 12 512444304
12 13 593266558
13 14 637089030
14 15 570997318
15 16 532382458
16 17 560609000
17 18 660606072
18 19 490130424
19 20 7457994...

output:

97218791529902 97218791529393

result:

ok 2 number(s): "97218791529902 97218791529393"

Test #18:

score: 0
Accepted
time: 446ms
memory: 60172kb

input:

1
185994 500000
1 2 567397670
2 3 842120068
3 4 140249904
4 5 220412752
5 6 218292640
6 7 589406450
7 8 253438466
8 9 635314634
9 10 531942838
10 11 519438844
11 12 843096034
12 13 807489190
13 14 577058708
14 15 770561234
15 16 282672936
16 17 402179824
17 18 638565940
18 19 241158952
19 20 5523583...

output:

31505497994524 31505497993817

result:

ok 2 number(s): "31505497994524 31505497993817"

Test #19:

score: 0
Accepted
time: 415ms
memory: 59252kb

input:

1
174215 500000
1 2 977507036
2 3 968147144
3 4 965117090
4 5 956825304
5 6 974030604
6 7 962462238
7 8 972448906
8 9 995064350
9 10 989785662
10 11 951491960
11 12 977681774
12 13 961174660
13 14 977438930
14 15 995524486
15 16 965860712
16 17 993531136
17 18 960289144
18 19 956670950
19 20 9607840...

output:

105346434958994 105346442987463

result:

ok 2 number(s): "105346434958994 105346442987463"

Test #20:

score: 0
Accepted
time: 412ms
memory: 63800kb

input:

1
178944 500000
1 2 86098280
2 3 118410448
3 4 318199280
4 5 255691258
5 6 215247486
6 7 193429068
7 8 127237618
8 9 271018410
9 10 251319020
10 11 260349578
11 12 65823910
12 13 321553940
13 14 355045416
14 15 229434824
15 16 293078860
16 17 290590024
17 18 312156114
18 19 359955124
19 20 254870764...

output:

31389456568860 31389456568233

result:

ok 2 number(s): "31389456568860 31389456568233"

Test #21:

score: 0
Accepted
time: 451ms
memory: 63452kb

input:

1
173433 500000
1 2 775495394
2 3 747097450
3 4 796067104
4 5 814079670
5 6 752018466
6 7 732441186
7 8 748111278
8 9 780095242
9 10 734862818
10 11 791458116
11 12 793224776
12 13 755841044
13 14 737438058
14 15 768691928
15 16 790627580
16 17 732414788
17 18 810139090
18 19 796036718
19 20 8237082...

output:

36862858735808 36863317911709

result:

ok 2 number(s): "36862858735808 36863317911709"

Test #22:

score: -100
Time Limit Exceeded

input:

1
167656 500000
1 2 456035944
2 3 456025212
3 4 456028928
4 5 456020494
5 6 456033838
6 7 456031288
7 8 456037462
8 9 456036874
9 10 456033928
10 11 456036458
11 12 456025538
12 13 456034270
13 14 456032110
14 15 456020716
15 16 456037822
16 17 456027056
17 18 456030992
18 19 456024426
19 20 4560270...

output:


result: