UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#202847#2782. 观察笔记augury10011576ms216188kbC++113.8kb2024-02-17 11:10:012024-02-17 12:10:10

answer

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
inline int read(){
	int ans=0;bool op=0;char ch=getchar();
	while(ch<'0'||'9'<ch){if(ch=='-')op=1;ch=getchar();}
	while('0'<=ch&&ch<='9'){ans=(ans<<1)+(ans<<3)+(ch^48);ch=getchar();}
	if(op)return -ans;
	return ans;
}
const int maxn=1e6+10;
const int inf=0x3f3f3f3f;
int n,m;
int s,t;
vector<int>tr[maxn];
priority_queue<int,vector<int>,greater<int> > mxh[maxn],mxd[maxn];
priority_queue<int> mnh[maxn],mnd[maxn];
int mx[maxn],mn[maxn];
int fa[maxn][20];
int dep[maxn];
struct edge{int v,f;};
vector<edge>e;
vector<int>g[maxn];
int val[maxn];
map<int,int>mp;
int dis[maxn],gap[maxn];
int eval[maxn];
void add(int u,int v,int f){
	g[u].push_back(e.size());
	e.push_back((edge){v,f});
	g[v].push_back(e.size());
	e.push_back((edge){u,0});
}
void init(int u){
	dep[u]=dep[fa[u][0]]+1;
	for(int i=1;i<20;i++)fa[u][i]=fa[fa[u][i-1]][i-1];
	for(int v:tr[u]){
		if(v==fa[u][0])continue;
		fa[v][0]=u;
		init(v);
	}
}
int lca(int u,int v){
	if(dep[u]<dep[v])swap(u,v);
	for(int i=19;i>=0;i--)if(dep[fa[u][i]]>=dep[v])u=fa[u][i];
	if(u==v)return u;
	for(int i=19;i>=0;i--)if(fa[u][i]!=fa[v][i])u=fa[u][i],v=fa[v][i];
	return fa[u][0];
}
void gval(int u){
	for(int v:tr[u]){
		if(v==fa[u][0])continue;
		gval(v);
		if(mxh[v].size()>mxh[u].size())swap(mxh[u],mxh[v]);
		if(mxd[v].size()>mxd[u].size())swap(mxd[u],mxd[v]);
		if(mnh[v].size()>mnh[u].size())swap(mnh[u],mnh[v]);
		if(mnd[v].size()>mnd[u].size())swap(mnd[u],mnd[v]);
		while(!mxh[v].empty())mxh[u].push(mxh[v].top()),mxh[v].pop();
		while(!mxd[v].empty())mxd[u].push(mxd[v].top()),mxd[v].pop();
		while(!mnh[v].empty())mnh[u].push(mnh[v].top()),mnh[v].pop();
		while(!mnd[v].empty())mnd[u].push(mnd[v].top()),mnd[v].pop();
	}
	while(!mxd[u].empty()&&mxh[u].top()==mxd[u].top())mxh[u].pop(),mxd[u].pop();
	while(!mnd[u].empty()&&mnh[u].top()==mnd[u].top())mnh[u].pop(),mnd[u].pop();
	if(!mxh[u].empty())mx[u]=mxh[u].top();
	if(!mnh[u].empty())mn[u]=mnh[u].top();
}
//isap
void bfs(){
	queue<int>q;
	memset(dep,0x3f,sizeof(dep));
	memset(gap,0,sizeof(gap));
	q.push(t),dep[t]=0;
	while(!q.empty()){
		int u=q.front();q.pop();
		++gap[dep[u]];
		for(int i:g[u]){
			if(e[i^1].f<=0||dep[e[i].v]<=dep[u]+1)continue;
			dep[e[i].v]=dep[u]+1;
			q.push(e[i].v);
		}
	}
}
int dfs(int u,int sum){
	if(u==t||!sum)return sum;
	int ret=0;
	for(int i:g[u]){
		if(e[i].f<=0||dep[e[i].v]+1!=dep[u])continue;
		int tmp=dfs(e[i].v,min(sum,e[i].f));
		e[i^1].f+=tmp;
		e[i].f-=tmp;
		ret+=tmp;
		sum-=tmp;
		if(!sum)return ret;
	}
	if(!--gap[dep[u]])dep[s]=n+1;
	++gap[++dep[u]];
	return ret;
}
int isap(){
	int ret=0;
	bfs();
	while(dep[s]<=n)ret+=dfs(s,inf);
	return ret;
}
signed main(){
	n=read();
	for(int i=1;i<n;i++){
		int x=read(),y=read();
		tr[x].push_back(y);
		tr[y].push_back(x);
	}
	init(1);
	m=read();
	s=0,t=n+m+1;
	for(int i=1;i<=m;i++){
		char op;
		cin>>op;
		int x=read(),y=read(),z=read()+1;
		val[i]=z;
		mp[z]=i;
		int l=lca(x,y);
		if(op=='M'){
			mxd[l].push(z);
			mxd[l].push(z);
			mxh[x].push(z);
			mxh[y].push(z);
		}
		if(op=='m'){
			mnd[l].push(z);
			mnd[l].push(z);
			mnh[x].push(z);
			mnh[y].push(z);
		}
	}
	for(int i=1;i<=m;i++)add(n+i,t,1);
	gval(1);
	for(int i=2;i<=n;i++){
		add(s,i,1);
		if(mx[i])add(i,n+mp[mx[i]],1);
		if(mn[i])add(i,n+mp[mn[i]],1);
	}
	isap();
	for(int i=2;i<=n;i++){
		for(int j:g[i]){
			if(!e[j].f&&e[j].v!=s){
				eval[i]=e[j].v-n;
			}
		}
	}
	for(int i=2;i<=n;i++){
		if(eval[i])cout<<i<<' '<<fa[i][0]<<' '<<val[eval[i]]-1<<'\n';
		else{
			if(mx[i])cout<<i<<' '<<fa[i][0]<<' '<<mx[i]-1<<'\n';
			else if(mn[i])cout<<i<<' '<<fa[i][0]<<' '<<mn[i]-1<<'\n';
			else cout<<i<<' '<<fa[i][0]<<' '<<114514<<'\n';
		}
	}
	return 0;
}

详细

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 40ms
memory: 180964kb

input:

4
1 2
2 3
3 4
3
M 1 2 1
m 1 4 0
M 2 3 100

output:

2 1 1
3 2 100
4 3 0

result:

ok The tree satisfies all quests.

Test #2:

score: 0
Accepted
time: 36ms
memory: 180968kb

input:

20
16 20
6 19
14 6
6 8
4 12
1 3
10 11
5 13
8 4
1 14
10 16
14 5
12 17
20 18
18 7
19 10
3 2
4 9
17 15
...

output:

2 3 19
3 1 935
4 8 55
5 14 230
6 14 272
7 18 992
8 6 944
9 4 49
10 19 202
11 10 986
12 4 32
13 5 449...

result:

ok The tree satisfies all quests.

Test #3:

score: 0
Accepted
time: 36ms
memory: 180976kb

input:

15
2 12
9 1
10 9
13 8
10 3
14 15
1 5
10 4
1 7
15 11
8 14
13 10
5 2
9 6
14
M 8 10 223
m 12 10 349
M 2...

output:

2 5 647
3 10 268
4 10 347
5 1 848
6 9 601
7 1 524
8 13 223
9 1 488
10 9 349
11 15 808
12 2 835
13 10...

result:

ok The tree satisfies all quests.

Test #4:

score: 0
Accepted
time: 47ms
memory: 180968kb

input:

20
20 9
20 13
20 15
20 19
20 16
12 5
20 2
20 12
12 7
15 8
15 3
12 17
15 14
12 11
20 18
15 6
20 1
20 ...

output:

2 20 152
3 15 385
4 12 776
5 12 664
6 15 448
7 12 37
8 15 492
9 20 890
10 20 892
11 12 879
12 20 834...

result:

ok The tree satisfies all quests.

Test #5:

score: 0
Accepted
time: 43ms
memory: 180972kb

input:

15
13 7
14 13
8 15
4 12
12 2
6 4
9 11
2 3
3 14
10 5
11 10
15 6
5 1
1 8
14
M 2 6 311
m 3 4 257
M 7 9 ...

output:

2 12 311
3 2 425
4 6 299
5 1 740
6 15 39
7 13 343
8 1 533
9 11 917
10 5 757
11 10 802
12 4 257
13 14...

result:

ok The tree satisfies all quests.

Subtask #2:

score: 20
Accepted

Test #6:

score: 20
Accepted
time: 378ms
memory: 210440kb

input:

70000
36376 47051
11574 39233
76 67751
7516 19293
24964 52274
12425 60549
1514 62264
26392 67475
615...

output:

2 46979 571006141
3 18188 362480923
4 38613 122568857
5 23713 14170591
6 46678 593586345
7 57363 386...

result:

ok The tree satisfies all quests.

Test #7:

score: 0
Accepted
time: 317ms
memory: 205060kb

input:

68073
3678 50006
548 37118
33802 61788
35444 63869
6330 66009
42336 65057
4760 43429
13668 35927
207...

output:

2 45261 765585361
3 38482 289997997
4 28663 398060305
5 9293 119625754
6 57576 835622722
7 45918 754...

result:

ok The tree satisfies all quests.

Test #8:

score: 0
Accepted
time: 335ms
memory: 206876kb

input:

69249
22477 52932
16382 62228
35328 43625
10043 42795
45609 52329
884 52971
6828 19918
19090 50163
9...

output:

2 53925 250097850
3 50981 239868330
4 46753 583973540
5 61942 11322451
6 17301 192895851
7 23382 740...

result:

ok The tree satisfies all quests.

Test #9:

score: 0
Accepted
time: 380ms
memory: 212152kb

input:

70000
51457 60817
15895 33245
28759 28785
44463 50438
2847 17964
23533 35668
15324 69984
347 9289
13...

output:

2 47643 116520391
3 38137 225508978
4 69258 720503482
5 6877 307290987
6 30158 369629574
7 48136 924...

result:

ok The tree satisfies all quests.

Test #10:

score: 0
Accepted
time: 329ms
memory: 206464kb

input:

69215
26169 42939
31867 42304
29941 45941
4906 56219
21588 31311
14394 40419
32261 48516
39391 61624...

output:

2 37592 238399617
3 14197 120519670
4 47049 957640303
5 54558 697078071
6 41187 499794462
7 29858 65...

result:

ok The tree satisfies all quests.

Test #11:

score: 0
Accepted
time: 348ms
memory: 205764kb

input:

70000
23126 47880
22044 42289
23334 48254
43549 63303
18193 40452
7916 67890
23452 58035
4558 19518
...

output:

2 61339 329315910
3 62906 639846108
4 31868 705523787
5 66126 120633990
6 46920 381456193
7 21650 72...

result:

ok The tree satisfies all quests.

Test #12:

score: 0
Accepted
time: 336ms
memory: 204540kb

input:

68812
36430 41576
25536 43758
11123 36847
16198 59255
51654 58790
22259 22691
39160 65556
5517 56448...

output:

2 40328 379200327
3 21629 568360762
4 37168 474928940
5 28630 98345850
6 24097 962853156
7 39260 120...

result:

ok The tree satisfies all quests.

Subtask #3:

score: 30
Accepted

Test #13:

score: 30
Accepted
time: 261ms
memory: 198428kb

input:

68552
18630 12310
11943 55569
66712 55554
29951 29185
21924 14373
60806 65516
22666 38447
44859 4719...

output:

2 64660 104540451
3 14987 970966891
4 15397 679998695
5 23566 917134075
6 59305 389289265
7 61645 80...

result:

ok The tree satisfies all quests.

Test #14:

score: 0
Accepted
time: 214ms
memory: 198576kb

input:

69341
7234 68519
60365 61090
31642 62947
33556 30432
49417 24749
7455 57812
3935 49354
13676 8816
44...

output:

2 63242 819295390
3 62366 114514
4 38801 114514
5 33928 114514
6 40556 652713816
7 60010 665978199
8...

result:

ok The tree satisfies all quests.

Test #15:

score: 0
Accepted
time: 192ms
memory: 202984kb

input:

69627
44417 20079
2212 2256
20694 20078
29952 33809
34618 56017
57358 56953
19636 35703
23431 55054
...

output:

2 2189 304944182
3 19633 997161171
4 42687 183569149
5 35240 21378616
6 30239 114514
7 52131 4097949...

result:

ok The tree satisfies all quests.

Test #16:

score: 0
Accepted
time: 206ms
memory: 205648kb

input:

69716
19812 34691
3991 42825
36863 69301
31407 53270
9579 67566
45550 14399
7439 30956
14490 4918
65...

output:

2 33568 114514
3 40310 114514
4 32690 736156237
5 46743 557441250
6 17790 605226641
7 645 252750639
...

result:

ok The tree satisfies all quests.

Test #17:

score: 0
Accepted
time: 360ms
memory: 200024kb

input:

69065
31844 39759
32536 2987
45070 5069
6496 19017
2443 23444
9234 37349
57074 29284
30191 46109
222...

output:

2 21681 911231464
3 50476 568355210
4 56148 874571658
5 37644 784754740
6 30347 114514
7 24141 11451...

result:

ok The tree satisfies all quests.

Test #18:

score: 0
Accepted
time: 315ms
memory: 201132kb

input:

69723
34468 7626
17694 47711
28050 41225
50690 2077
25392 22482
56598 55402
50575 61869
60470 9057
1...

output:

2 64037 957138674
3 15833 114514
4 44046 952592091
5 22541 883271478
6 56423 970852381
7 43025 11451...

result:

ok The tree satisfies all quests.

Test #19:

score: 0
Accepted
time: 259ms
memory: 199532kb

input:

70000
16125 36929
48782 16769
25245 7413
23311 12291
49612 8001
47467 34721
57468 59658
25961 60721
...

output:

2 33870 988646729
3 46125 427637357
4 24548 822750902
5 38035 114514
6 18879 114514
7 62504 73744742...

result:

ok The tree satisfies all quests.

Test #20:

score: 0
Accepted
time: 278ms
memory: 198988kb

input:

70000
42487 1662
56043 2270
15555 3550
12937 48145
67751 69766
38327 60738
3504 52709
46172 57534
52...

output:

2 5239 722094944
3 27396 827419625
4 28998 949156151
5 7507 237286501
6 21267 640460596
7 38082 1145...

result:

ok The tree satisfies all quests.

Subtask #4:

score: 40
Accepted

Test #21:

score: 40
Accepted
time: 372ms
memory: 206804kb

input:

68718
48698 56355
26673 53562
22191 38302
9907 60441
36143 50623
4968 41248
6112 25449
13311 29389
4...

output:

2 6229 469170113
3 43394 379806167
4 42912 603997617
5 53998 461087865
6 8973 926219352
7 63039 4207...

result:

ok The tree satisfies all quests.

Test #22:

score: 0
Accepted
time: 316ms
memory: 203612kb

input:

68351
7794 28509
17454 56757
25869 31154
48509 60494
57008 63392
48399 54805
500 45672
13679 24297
4...

output:

2 62217 524103547
3 66124 13160434
4 58855 83745638
5 50389 310666822
6 61858 99794692
7 9950 563093...

result:

ok The tree satisfies all quests.

Test #23:

score: 0
Accepted
time: 332ms
memory: 204008kb

input:

68608
16586 21105
26066 42997
9317 51600
6447 27391
7241 25342
44509 61542
13449 34955
31062 58204
1...

output:

2 23096 491507338
3 4386 367013057
4 58185 179911770
5 28185 920989316
6 39311 853067953
7 23952 972...

result:

ok The tree satisfies all quests.

Test #24:

score: 0
Accepted
time: 369ms
memory: 203920kb

input:

70000
15450 52420
27995 33091
54301 55349
42529 59045
3993 41421
23912 28040
7479 29087
3200 23123
4...

output:

2 6913 886627792
3 50023 295682219
4 66891 708322297
5 5878 114514
6 3312 417794374
7 3562 41338231
...

result:

ok The tree satisfies all quests.

Test #25:

score: 0
Accepted
time: 442ms
memory: 213556kb

input:

69443
3119 37587
11719 41925
62120 68518
325 16504
33410 47518
14212 38054
39326 49169
6037 26911
27...

output:

2 30261 428934095
3 28943 505429336
4 19270 766925249
5 45722 625092949
6 13003 997064770
7 52546 38...

result:

ok The tree satisfies all quests.

Test #26:

score: 0
Accepted
time: 452ms
memory: 216188kb

input:

70000
18467 50823
6751 43499
2615 58976
29245 59747
26117 36758
45855 60741
5596 14210
26883 68569
1...

output:

2 5957 426419257
3 57480 731905538
4 37288 690710671
5 13580 618028077
6 1622 911031342
7 25369 8560...

result:

ok The tree satisfies all quests.

Test #27:

score: 0
Accepted
time: 453ms
memory: 214596kb

input:

70000
56454 58972
3468 9488
55815 65804
25635 43038
27774 65128
18869 32795
41656 55415
46484 53613
...

output:

2 1352 197093981
3 21302 458326772
4 68219 446695227
5 31105 63893946
6 48799 280379504
7 12641 4954...

result:

ok The tree satisfies all quests.

Test #28:

score: 0
Accepted
time: 470ms
memory: 212300kb

input:

69053
9744 30745
4161 28106
11812 61449
26939 48917
21829 47424
5004 43865
194 41309
6471 10782
4242...

output:

2 41722 96931254
3 20396 564877323
4 59233 326164392
5 26441 906955654
6 27545 768921614
7 42783 697...

result:

ok The tree satisfies all quests.

Test #29:

score: 0
Accepted
time: 433ms
memory: 207756kb

input:

70000
5123 11428
30655 42495
44237 60582
39780 66881
38325 62373
15800 38712
7934 32883
28400 35856
...

output:

2 15155 557682059
3 115 722749146
4 69216 182720702
5 32462 918206286
6 2260 26798112
7 17420 576351...

result:

ok The tree satisfies all quests.

Test #30:

score: 0
Accepted
time: 399ms
memory: 208000kb

input:

70000
40734 47870
32269 61900
667 29463
29493 48527
28859 51897
15857 51935
56351 66994
10111 41910
...

output:

2 39783 124002421
3 54909 505387325
4 22947 626340432
5 60114 127998523
6 60768 88881419
7 60996 298...

result:

ok The tree satisfies all quests.

Test #31:

score: 0
Accepted
time: 516ms
memory: 205412kb

input:

69362
12368 31749
53990 59241
3208 47167
4341 47451
3508 63262
15193 47712
50663 59088
48806 50995
7...

output:

2 43149 910287264
3 29824 687708919
4 28176 861589919
5 64751 87799824
6 23067 176083919
7 44118 642...

result:

ok The tree satisfies all quests.

Test #32:

score: 0
Accepted
time: 492ms
memory: 205508kb

input:

70000
33405 41273
19185 24987
23253 45306
6980 26326
37744 46568
8285 32647
6942 19183
32613 37912
1...

output:

2 58386 642145538
3 7904 114514
4 27951 385875197
5 67220 967743289
6 23604 819583773
7 4116 1029616...

result:

ok The tree satisfies all quests.

Test #33:

score: 0
Accepted
time: 455ms
memory: 207036kb

input:

70000
51248 63766
7801 41456
46200 59897
52162 53625
7209 36727
29767 57075
21431 31494
24503 28270
...

output:

2 56638 233836212
3 4389 683391388
4 60699 539980647
5 7068 51442444
6 27777 501838278
7 35246 13371...

result:

ok The tree satisfies all quests.

Test #34:

score: 0
Accepted
time: 386ms
memory: 205300kb

input:

70000
16540 68179
13426 56479
44394 64514
37795 60179
21707 37795
26995 64283
8903 12858
40404 51536...

output:

2 64746 767907877
3 13833 803441252
4 19467 898171989
5 5213 451481829
6 1077 479588840
7 29771 9550...

result:

ok The tree satisfies all quests.

Test #35:

score: 0
Accepted
time: 398ms
memory: 205380kb

input:

70000
43232 57751
57491 64724
27481 64693
5802 28728
6687 22940
15194 47019
61163 65135
60387 64069
...

output:

2 19200 28048819
3 13192 461202579
4 492 355219033
5 62833 981854280
6 36374 62972803
7 61898 712535...

result:

ok The tree satisfies all quests.

Test #36:

score: 0
Accepted
time: 581ms
memory: 204524kb

input:

70000
38211 46952
67528 68964
13572 46398
655 15618
12389 38687
34448 48912
54042 68269
19849 34345
...

output:

2 16966 876394466
3 41447 744441669
4 33302 470704232
5 48623 30672313
6 68314 184441920
7 47552 730...

result:

ok The tree satisfies all quests.

Extra Test:

score: 0
Extra Test Passed