UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#196166#2350. Treewosile100709ms27864kbC++112.0kb2023-10-19 10:20:152023-10-19 12:10:06

answer

#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
int n,q;
int a[100005];
int f[100005][25],dep[100005],mx[100005][25];
int w[100005];
pair<int,int>f1[100005];
pair<int,int>f2[100005];
int f3[100005],f4[100005];
vector<int>T[100005];
pair<int,int> add(pair<int,int>x,pair<int,int>y){
	return make_pair(x.first+y.first,min(x.second,y.second));
}
void dfs1(int x){
	f3[x]=f3[f[x][0]]+w[x];
	dep[x]=dep[f[x][0]]+1;
	for(int i=1;i<20;i++)f[x][i]=f[f[x][i-1]][i-1];
	mx[x][0]=w[x];
	for(int i=1;i<20;i++)mx[x][i]=max(mx[x][i-1],mx[f[x][i-1]][i-1]);
	for(int i=0;i<T[x].size();i++){
		f[T[x][i]][0]=x;
		dfs1(T[x][i]);
		f1[x]=min(f1[x],f1[T[x][i]]);
	}
	if(T[x].size()==0)f1[x]=make_pair(w[x],-w[x]);
	else f1[x]=add(f1[x],make_pair(w[x],-w[x]));
}
void dfs2(int x){
	f2[x]=min(f2[x],add(f2[f[x][0]],make_pair(w[x],-w[x])));
	f4[x]=max(f4[x],max(f4[f[x][0]],w[x]));
	for(int i=0;i<T[x].size();i++)dfs2(T[x][i]);
}
int lca(int x,int y){
	if(dep[x]>dep[y])swap(x,y);
	for(int i=19;i>=0;i--)if(dep[f[y][i]]>=dep[x])y=f[y][i];
	if(x==y)return x;
	for(int i=19;i>=0;i--)if(f[x][i]!=f[y][i])x=f[x][i],y=f[y][i];
	return f[x][0];
}
int getmx(int x,int y){
	int ans=0;
	for(int i=19;i>=0;i--)if(dep[f[x][i]]>=dep[y]-1){
		ans=max(ans,mx[x][i]);
		x=f[x][i];
	}
	return ans;
}
int main(){
//	freopen("T2.in","r",stdin);
//	freopen("T2.out","w",stdout);
	scanf("%d%d",&n,&q);
	for(int i=2;i<=n;i++){
		int x;
		scanf("%d",&x);
		T[x].push_back(i);
	}
	for(int i=1;i<=n;i++)scanf("%d",&w[i]);
	memset(f1,0x3f,sizeof(f1));
	memset(f2,0x3f,sizeof(f2));
	dfs1(1);
	for(int i=1;i<=n;i++)f2[i]=f1[i];
	dfs2(1);
	while(q--){
		int x,y;
		scanf("%d%d",&x,&y);
		int L=lca(x,y);
		pair<int,int>ans=make_pair(f3[x]-f3[f[L][0]]+f3[y]-f3[L],-max(getmx(x,L),getmx(y,L)));
		ans=min(ans,add(add(f2[x],f2[y]),make_pair(w[1],-w[1])));//leaf-leaf
		ans=min(ans,add(make_pair(f3[x],-f4[x]),f2[y]));//root-leaf
		ans=min(ans,add(make_pair(f3[y],-f4[y]),f2[x]));//leaf-root
		printf("%d %d\n",ans.first,-ans.second);
	}
	return 0;
}

详细

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

Test #1:

score: 10
Accepted
time: 0ms
memory: 5236kb

input:

300 1000
1 1 1 1 3 4 3 6 5 1 9 5 10 7 12 6 2 7 5 10 18 17 23 24 23 3 19 14 12 8 25 21 1 26 3 25 6 10...

output:

1746 1000
1932 820
2145 781
2365 836
1113 624
1570 787
301 174
2619 861
1641 869
913 558
1790 770
21...

result:

ok 2000 numbers

Test #2:

score: 10
Accepted
time: 0ms
memory: 5240kb

input:

300 1000
1 2 3 3 4 2 7 3 7 6 1 6 3 7 13 2 2 2 19 15 3 1 19 12 20 23 26 28 29 23 4 31 4 13 26 5 17 36...

output:

1291 638
1694 767
1185 518
1344 689
2332 930
1490 975
1176 822
1844 484
2306 689
2116 890
1508 831
1...

result:

ok 2000 numbers

Test #3:

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

input:

300 1000
1 1 3 2 2 1 4 7 2 4 4 8 4 7 5 7 11 1 11 17 1 16 1 5 6 26 21 18 28 27 16 10 32 21 34 5 2 19 ...

output:

2388 938
2340 938
2324 938
3613 938
3509 938
1587 938
2123 938
2851 938
2352 938
2341 938
2211 950
3...

result:

ok 2000 numbers

Test #4:

score: 10
Accepted
time: 10ms
memory: 5620kb

input:

2000 10000
1 1 2 3 5 3 1 8 1 4 9 9 11 2 7 3 10 16 1 20 13 15 11 9 13 13 22 3 14 30 12 14 9 28 6 10 3...

output:

1750 597
2561 818
1328 467
2245 604
1844 762
1348 938
1362 463
1642 582
2235 847
2119 639
998 338
19...

result:

ok 20000 numbers

Test #5:

score: 10
Accepted
time: 7ms
memory: 5620kb

input:

2000 10000
1 2 1 3 4 1 2 3 8 6 3 8 4 6 9 16 13 16 18 13 20 5 8 24 13 9 21 25 22 25 21 12 16 25 14 31...

output:

3326 948
1492 617
1802 804
1084 739
1288 671
1460 459
1287 687
1298 633
1576 849
1833 956
1996 940
1...

result:

ok 20000 numbers

Test #6:

score: 10
Accepted
time: 5ms
memory: 5624kb

input:

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

output:

2003 946
2821 917
1155 864
1677 864
2719 864
3069 889
2660 864
2660 864
2212 864
1779 864
2301 864
2...

result:

ok 20000 numbers

Test #7:

score: 10
Accepted
time: 174ms
memory: 27860kb

input:

100000 100000
1 1 2 2 5 4 1 4 1 10 2 3 7 10 8 14 3 14 10 2 3 6 9 14 17 1 1 22 1 24 30 9 5 12 14 2 19...

output:

1238 642
1427 889
1568 628
1532 804
2569 905
2160 782
1295 583
1663 821
3072 829
1552 875
2216 828
2...

result:

ok 200000 numbers

Test #8:

score: 10
Accepted
time: 166ms
memory: 27860kb

input:

100000 100000
1 1 1 4 1 2 1 6 5 3 11 10 5 10 13 5 11 3 5 3 20 1 20 15 18 3 13 17 3 17 21 14 17 1 31 ...

output:

1711 890
3294 993
3240 951
1226 689
2941 689
1931 689
2919 689
2326 744
1384 689
2807 928
1231 689
2...

result:

ok 200000 numbers

Test #9:

score: 10
Accepted
time: 173ms
memory: 27864kb

input:

100000 100000
1 1 2 4 3 4 1 4 5 5 5 3 4 3 14 1 14 10 8 16 3 1 22 3 13 6 8 20 26 3 16 16 26 20 33 2 1...

output:

2391 830
2303 889
1511 596
811 596
1996 894
2347 888
996 596
3350 818
2292 900
744 596
2149 743
2145...

result:

ok 200000 numbers

Test #10:

score: 10
Accepted
time: 170ms
memory: 27856kb

input:

100000 100000
1 1 2 4 3 5 4 8 6 9 1 1 10 8 2 16 7 1 18 13 21 21 9 3 3 22 18 11 26 3 5 16 30 11 7 26 ...

output:

2159 823
1347 716
2098 998
2248 716
1975 716
3066 925
2446 734
2026 930
1409 716
2433 945
1221 716
2...

result:

ok 200000 numbers

Extra Test:

score: 0
Extra Test Passed