UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#202835#2782. 观察笔记yizhiming1006273ms29240kbC++114.5kb2024-02-17 10:53:432024-02-17 12:08:51

answer

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cmath>
#include <map>
using namespace std;
int read(){
	int x=0,f=1;char ch = getchar();
	while(ch<'0'||ch>'9'){if(ch=='-'){f=-1;}ch = getchar();}
	while(ch>='0'&&ch<='9'){x = x*10+ch-'0';ch = getchar();}
	return x*f;
}
const int N = 2e5+5;
int fa[N],dep[N],top[N],son[N],siz[N],n;
vector<int>ed[N];
void dfs1(int u,int f){
	siz[u] = 1;
	for(auto x:ed[u]){
		if(x==f){
			continue;
		}
		fa[x] = u;
		dep[x] = dep[u]+1;
		dfs1(x,u);
		siz[u]+=siz[x];
		if(siz[son[u]]<siz[x]){
			son[u] = x;
		}
	}
}
int dfn[N],tt,id[N];
void dfs2(int u,int t){
	top[u] = t;
	dfn[u] = ++tt;
	id[tt] = u;
	if(!son[u]){
		return;
	}
	dfs2(son[u],t);
	for(auto x:ed[u]){
		if(x==son[u]||x==fa[u]){
			continue;
		}
		dfs2(x,x);
	}
}
int Lca(int u,int v){
	while(top[u]!=top[v]){
		if(dep[top[u]]<dep[top[v]]){
			swap(u,v);
		}
		u = fa[top[u]];
	}
	if(dep[u]<dep[v]){
		swap(u,v);
	}
	return v;
}
int inf = 2e9;
int L[N],R[N];
struct seg{
	struct aa{
		int lc,rc;
		int mx,mi;
	}node[N*2];
	int tot;
	void build(int &u,int l,int r){
		u = ++tot;
		node[u].mi = -inf;
		node[u].mx = inf;
		if(l==r){
			return;
		}
		int mid = (l+r)/2;
		build(node[u].lc,l,mid);
		build(node[u].rc,mid+1,r);
	}
	void upd(int u,int l,int r,int ll,int rr,int x,int y){
		if(ll>rr){
			return;
		}
		if(l==ll&&r==rr){
//			cout<<"LR:"<<l<<" "<<r<<" "<<x<<"\n"; 
			if(y==0){
				node[u].mi = max(node[u].mi,x);
			}else{
				node[u].mx = min(node[u].mx,x);
			}
			return;
		}
		int mid = (l+r)/2;
		if(rr<=mid){
			upd(node[u].lc,l,mid,ll,rr,x,y);
		}else if(ll>mid){
			upd(node[u].rc,mid+1,r,ll,rr,x,y);
		}else{
			upd(node[u].lc,l,mid,ll,mid,x,y);
			upd(node[u].rc,mid+1,r,mid+1,rr,x,y);
		}
	}
	void dfs(int u,int l,int r,int x,int y){
		x = max(node[u].mi,x);
		y = min(node[u].mx,y);
		if(l==r){
			L[id[l]] = x;
			R[id[l]] = y;
			return;
		}
		int mid = (l+r)/2;
		dfs(node[u].lc,l,mid,x,y);
		dfs(node[u].rc,mid+1,r,x,y);
	}
}T;
int rt;
void add(int u,int v,int x,int y){//y0最小值 
	while(top[u]!=top[v]){
		if(dep[top[u]]<dep[top[v]]){
			swap(u,v);
		}
		T.upd(rt,1,n,dfn[top[u]],dfn[u],x,y);
		u = fa[top[u]];
	}
	if(dep[u]<dep[v]){
		swap(u,v);
	}
	T.upd(rt,1,n,dfn[v]+1,dfn[u],x,y);
}
map<int,int>mp;
int s,t;
int head[N],tote=1;
struct bb{
	int nxt,to,val;
}edge[N*4];
struct flow{
	
	void link(int u,int v,int w){
//		cout<<"UV:"<<u<<" "<<v<<"\n";
		edge[++tote] = (bb){head[u],v,w};head[u] = tote;
		edge[++tote] = (bb){head[v],u,0};head[v] = tote;
	}
	queue<int>q;
	int d[N];
	bool bfs(){
		memset(d,0,sizeof(d));
		d[s] = 1;
		q.push(s);
		while(!q.empty()){
			int u = q.front();
			q.pop();
			for(int i=head[u];i;i=edge[i].nxt){
				int now = edge[i].to;
				if(!d[now]&&edge[i].val){
					d[now] = d[u]+1;
					q.push(now);
				}
			}
		}
		return d[t]; 
	}
	int dfs(int u,int f){
		if(u==t){
			return f;
		}
		int used = 0;
		for(int i=head[u];i&&f;i=edge[i].nxt){
			int now = edge[i].to;
			if(d[now]==d[u]+1&&edge[i].val){
				int w = dfs(now,min(f,edge[i].val));
				used+=w;f-=w;
				edge[i].val-=w;edge[i^1].val+=w;
			}
		}
		if(!used){
			d[u] = 0;
		}
		return used;
	}
	int ans;
	void dinic(){
		ans = 0;
		while(bfs()){
			ans+=dfs(s,inf);
		}
	}
}F;
int ans[N];
int main(){
//	freopen("ex_tree2.in","r",stdin);
//	freopen("me.txt","w",stdout); 
	n = read();
	for(int i=1;i<n;i++){
		int u,v;
		u = read();v = read();
		ed[u].push_back(v);
		ed[v].push_back(u);
	}
	dfs1(1,1);
	dfs2(1,1);
	T.build(rt,1,n);
	int m = read();
	s = 0;t = n+m+1;
	for(int i=1;i<=m;i++){
		char ch[5];int u,v,x;
		scanf("%s",ch);u = read();v = read();x = read();
		mp[x] = i;
		if(ch[0]=='m'){
			add(u,v,x,0);
		}else{
			add(u,v,x,1);
		}
		F.link(i+n,t,1);
	}
	T.dfs(rt,1,n,-inf,inf);
	
	for(int i=1;i<=n;i++){
		F.link(s,i,1);
		if(mp[L[i]]){
			F.link(i,mp[L[i]]+n,1);
		}
		if(mp[R[i]]){
			F.link(i,mp[R[i]]+n,1);
		}
//		cout<<"LR:"<<i<<" "<<L[i]<<" "<<R[i]<<"\n";
	}
	F.dinic();
//	cout<<F.ans<<"\n";
	for(int i=1;i<=n;i++){
		ans[i] = L[i];
		for(int j=head[i];j;j=edge[j].nxt){
			if(edge[j].val){
				continue;
			}
			int now = edge[j].to;
			if(now>n){
				if(now-n==mp[L[i]]){
					ans[i] = L[i];
				}else{
					ans[i] = R[i];
				}
			}
		}
	}
	for(int i=2;i<=n;i++){
		cout<<fa[i]<<" "<<i<<" "<<ans[i]<<"\n";
	}
	return 0;
}

详细

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

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 4ms
memory: 6788kb

input:

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

output:

1 2 1
2 3 100
3 4 0

result:

ok The tree satisfies all quests.

Test #2:

score: 0
Accepted
time: 0ms
memory: 6804kb

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:

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

result:

ok The tree satisfies all quests.

Test #3:

score: 0
Accepted
time: 0ms
memory: 6800kb

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:

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

result:

ok The tree satisfies all quests.

Test #4:

score: 0
Accepted
time: 3ms
memory: 6800kb

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:

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

result:

ok The tree satisfies all quests.

Test #5:

score: 0
Accepted
time: 3ms
memory: 6800kb

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:

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

result:

ok The tree satisfies all quests.

Subtask #2:

score: 20
Accepted

Test #6:

score: 20
Accepted
time: 171ms
memory: 25904kb

input:

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

output:

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

result:

ok The tree satisfies all quests.

Test #7:

score: 0
Accepted
time: 190ms
memory: 22488kb

input:

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

output:

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

result:

ok The tree satisfies all quests.

Test #8:

score: 0
Accepted
time: 170ms
memory: 23808kb

input:

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

output:

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

result:

ok The tree satisfies all quests.

Test #9:

score: 0
Accepted
time: 160ms
memory: 26764kb

input:

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

output:

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

result:

ok The tree satisfies all quests.

Test #10:

score: 0
Accepted
time: 177ms
memory: 23672kb

input:

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

output:

37592 2 238399617
14197 3 -2000000000
47049 4 957640303
54558 5 697078071
41187 6 -2000000000
29858 ...

result:

ok The tree satisfies all quests.

Test #11:

score: 0
Accepted
time: 164ms
memory: 23208kb

input:

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

output:

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

result:

ok The tree satisfies all quests.

Test #12:

score: 0
Accepted
time: 153ms
memory: 22492kb

input:

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

output:

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

result:

ok The tree satisfies all quests.

Subtask #3:

score: 30
Accepted

Test #13:

score: 30
Accepted
time: 96ms
memory: 18968kb

input:

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

output:

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

result:

ok The tree satisfies all quests.

Test #14:

score: 0
Accepted
time: 112ms
memory: 19152kb

input:

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

output:

63242 2 819295390
62366 3 -2000000000
38801 4 -2000000000
33928 5 -2000000000
40556 6 652713816
6001...

result:

ok The tree satisfies all quests.

Test #15:

score: 0
Accepted
time: 105ms
memory: 21528kb

input:

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

output:

2189 2 -2000000000
19633 3 997161171
42687 4 -2000000000
35240 5 -2000000000
30239 6 -2000000000
521...

result:

ok The tree satisfies all quests.

Test #16:

score: 0
Accepted
time: 104ms
memory: 23012kb

input:

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

output:

33568 2 -2000000000
40310 3 -2000000000
32690 4 736156237
46743 5 -2000000000
17790 6 -2000000000
64...

result:

ok The tree satisfies all quests.

Test #17:

score: 0
Accepted
time: 123ms
memory: 19940kb

input:

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

output:

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

result:

ok The tree satisfies all quests.

Test #18:

score: 0
Accepted
time: 113ms
memory: 20456kb

input:

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

output:

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

result:

ok The tree satisfies all quests.

Test #19:

score: 0
Accepted
time: 124ms
memory: 19440kb

input:

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

output:

33870 2 988646729
46125 3 -2000000000
24548 4 822750902
38035 5 -2000000000
18879 6 -2000000000
6250...

result:

ok The tree satisfies all quests.

Test #20:

score: 0
Accepted
time: 126ms
memory: 19408kb

input:

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

output:

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

result:

ok The tree satisfies all quests.

Subtask #4:

score: 40
Accepted

Test #21:

score: 40
Accepted
time: 266ms
memory: 23440kb

input:

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

output:

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

result:

ok The tree satisfies all quests.

Test #22:

score: 0
Accepted
time: 183ms
memory: 21780kb

input:

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

output:

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

result:

ok The tree satisfies all quests.

Test #23:

score: 0
Accepted
time: 256ms
memory: 21936kb

input:

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

output:

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

result:

ok The tree satisfies all quests.

Test #24:

score: 0
Accepted
time: 199ms
memory: 22196kb

input:

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

output:

6913 2 886627792
50023 3 295682219
66891 4 708322297
5878 5 -2000000000
3312 6 417794374
3562 7 4133...

result:

ok The tree satisfies all quests.

Test #25:

score: 0
Accepted
time: 383ms
memory: 27696kb

input:

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

output:

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

result:

ok The tree satisfies all quests.

Test #26:

score: 0
Accepted
time: 389ms
memory: 29240kb

input:

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

output:

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

result:

ok The tree satisfies all quests.

Test #27:

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

input:

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

output:

1352 2 197093981
21302 3 458264002
68219 4 446695227
31105 5 63888301
48799 6 280359950
12641 7 4954...

result:

ok The tree satisfies all quests.

Test #28:

score: 0
Accepted
time: 264ms
memory: 27244kb

input:

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

output:

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

result:

ok The tree satisfies all quests.

Test #29:

score: 0
Accepted
time: 282ms
memory: 24248kb

input:

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

output:

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

result:

ok The tree satisfies all quests.

Test #30:

score: 0
Accepted
time: 268ms
memory: 24256kb

input:

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

output:

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

result:

ok The tree satisfies all quests.

Test #31:

score: 0
Accepted
time: 228ms
memory: 23176kb

input:

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

output:

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

result:

ok The tree satisfies all quests.

Test #32:

score: 0
Accepted
time: 255ms
memory: 23236kb

input:

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

output:

58386 2 642145538
7904 3 -2000000000
27951 4 385875197
67220 5 967743289
23604 6 -2000000000
4116 7 ...

result:

ok The tree satisfies all quests.

Test #33:

score: 0
Accepted
time: 265ms
memory: 23848kb

input:

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

output:

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

result:

ok The tree satisfies all quests.

Test #34:

score: 0
Accepted
time: 238ms
memory: 23180kb

input:

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

output:

64746 2 767907877
13833 3 795830906
19467 4 898171989
5213 5 -2000000000
1077 6 479588840
29771 7 85...

result:

ok The tree satisfies all quests.

Test #35:

score: 0
Accepted
time: 242ms
memory: 23188kb

input:

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

output:

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

result:

ok The tree satisfies all quests.

Test #36:

score: 0
Accepted
time: 251ms
memory: 22744kb

input:

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

output:

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

result:

ok The tree satisfies all quests.

Extra Test:

score: 0
Extra Test Passed