UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#203754#525. 神树的权值wosile1005605ms47540kbC++111.2kb2024-03-14 09:41:572024-03-14 21:21:32

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define eps 1e-8
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define N 300005
vector<int>T[N],v[N];
ll sum[N+N];
int s[N],vis[N],n;
int fa[N+N],ls[N+N],rs[N+N];
int find(int x){
	if(x==fa[x])return x;
	return fa[x]=find(fa[x]);
}
void dfs(int x){
	if(x<=n)return;
	sum[ls[x]]+=sum[x];
	sum[rs[x]]+=sum[x];
	dfs(ls[x]);
	dfs(rs[x]);
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&s[i]);
	for(int i=1;i<n;i++){
		int u,v;
		scanf("%d%d",&u,&v);
		T[u].push_back(v);
		T[v].push_back(u);
	}
	for(int i=1;i<=n;i++)v[s[i]].push_back(i);
	int tot=n;
	for(int i=1;i<=n+n;i++)fa[i]=i;
	for(int i=1;i<=n;i++){
		for(int x:v[i])sum[find(x)]+=x;
		// for(int x:v[i])printf("find(%d)=%d\n",x,find(x));
		for(int x:v[i])for(int u:T[x])if(vis[u])sum[find(u)]+=x;
		for(int x:v[i]){
			vis[x]=1;
			for(int u:T[x]){
				if(vis[u]){
					int f1=find(x);
					int f2=find(u);
					fa[f1]=fa[f2]=++tot;
					ls[tot]=f1,rs[tot]=f2;
				}
			}
		}
	}
	dfs(tot);
	for(int i=1;i<=n;i++)printf("%lld ",sum[i]);
	return 0;
}
// quod erat demonstrandum

详细

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

Test #1:

score: 5
Accepted
time: 6ms
memory: 15652kb

input:

4000
1300 1141 3352 1141 1141 921 902 351 892 351 892 3352 902 902 351 3352 94 921 902 3352 351 3352...

output:

780512 676960 3 687723 672704 420680 675057 675253 34880 909356 905223 12 14835 705101 5778 16 40384...

result:

ok 4000 tokens

Test #2:

score: 5
Accepted
time: 7ms
memory: 15668kb

input:

4000
3543 797 2334 3806 298 1802 3982 2016 1284 498 406 298 1042 3407 113 630 2998 638 3951 2252 254...

output:

604213 851503 224224 332335 862753 222355 57109 718522 298914 803354 538800 165686 297033 203256 296...

result:

ok 4000 tokens

Test #3:

score: 5
Accepted
time: 3ms
memory: 15680kb

input:

4000
2990 747 895 2954 144 3700 935 3656 2485 1970 2672 1088 3449 1470 3759 636 889 2112 3759 3107 3...

output:

942139 31930 26675 38668 53151 270915 70145 293401 1328631 37849 1195973 34095 490801 24088 218195 1...

result:

ok 4000 tokens

Test #4:

score: 5
Accepted
time: 6ms
memory: 15656kb

input:

4000
3280 2148 1768 1942 1664 762 54 1046 887 3354 1114 3240 1691 2705 1312 1773 3352 3491 1184 799 ...

output:

752572 721133 694919 707958 706734 772531 647145 766584 807572 719550 818400 323333 682097 780953 66...

result:

ok 4000 tokens

Test #5:

score: 5
Accepted
time: 4ms
memory: 15676kb

input:

4000
991 767 4000 749 2949 3982 4000 2819 3769 4000 3996 4000 4000 4000 3995 4000 4000 217 3994 4000...

output:

14219 6418 3 14135 4883 11886 7 15521 14298 10 2606 12 13 14 4684 16 17 3780 1401 20 9353 10197 2150...

result:

ok 4000 tokens

Test #6:

score: 5
Accepted
time: 3ms
memory: 15656kb

input:

4000
3987 3987 3987 3987 3987 3987 3987 3987 3987 3987 3987 3987 3987 3987 3987 3987 3987 3987 3987 ...

output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 1876 32 33 34 35 36...

result:

ok 4000 tokens

Test #7:

score: 5
Accepted
time: 472ms
memory: 46292kb

input:

300000
213419 172830 16007 171390 163180 90101 171587 140356 133818 214529 220424 190756 167337 1182...

output:

3091352230 1381353480 2327982315 3056938673 1368598277 1856909423 2760823545 1219023928 1871991577 1...

result:

ok 300000 tokens

Test #8:

score: 5
Accepted
time: 290ms
memory: 45780kb

input:

300000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ...

output:

45000150000 45000149999 45000149997 45000149994 45000149990 45000149985 45000149979 45000149972 4500...

result:

ok 300000 tokens

Test #9:

score: 5
Accepted
time: 362ms
memory: 46112kb

input:

300000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ...

output:

45000150000 45000149999 45000149997 45000149994 45000149990 45000149985 45000149979 45000149972 4500...

result:

ok 300000 tokens

Test #10:

score: 5
Accepted
time: 300ms
memory: 47540kb

input:

300000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ...

output:

45000150000 45000149999 45000149997 45000149994 45000149990 45000149985 45000149979 45000149972 4500...

result:

ok 300000 tokens

Test #11:

score: 5
Accepted
time: 394ms
memory: 45780kb

input:

300000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ...

output:

45000150000 45000149999 45000149997 45000149994 45000149990 45000149985 45000149979 45000149972 4500...

result:

ok 300000 tokens

Test #12:

score: 5
Accepted
time: 465ms
memory: 46332kb

input:

300000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ...

output:

45000150000 45000149999 45000149997 45000149994 45000149990 45000149985 45000149979 45000149972 4500...

result:

ok 300000 tokens

Test #13:

score: 5
Accepted
time: 363ms
memory: 39288kb

input:

300000
188986 300000 300000 300000 232970 299992 300000 300000 300000 49940 300000 300000 190938 300...

output:

557626 2 3 4 256911 230166 7 8 9 212314 11 12 263527 14 15 16 17 18 75179 20 21 22 542022 24 25 5313...

result:

ok 300000 tokens

Test #14:

score: 5
Accepted
time: 409ms
memory: 39732kb

input:

300000
299998 299998 299998 299998 299998 299998 299998 299998 299998 299998 299998 134578 299998 29...

output:

1 2 3 4 5 6 7 8 9 10 11 314680 13 14 15 16 367512 18 19 20 21 22 530857 24 25 26 27 28 29 30 31 32 3...

result:

ok 300000 tokens

Test #15:

score: 5
Accepted
time: 368ms
memory: 40232kb

input:

300000
300000 300000 300000 300000 300000 300000 12796 300000 228103 300000 300000 300000 300000 605...

output:

1 2 3 4 5 6 415331 8 256416 10 11 12 13 560122 359650 16 17 202825 19 20 21 298782 23 24 25 312151 2...

result:

ok 300000 tokens

Test #16:

score: 5
Accepted
time: 511ms
memory: 42280kb

input:

300000
195329 270293 58998 168596 157846 186055 292501 238089 67190 119516 215123 264843 223939 2262...

output:

4407707 3541923 9382633 4186553 3660873 6748975 6199972 5337399 6363319 8010280 7917641 2464028 4772...

result:

ok 300000 tokens

Test #17:

score: 5
Accepted
time: 532ms
memory: 37532kb

input:

300000
296761 296761 296761 296761 296761 296761 296761 296761 296761 296761 296761 296761 296761 29...

output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3...

result:

ok 300000 tokens

Test #18:

score: 5
Accepted
time: 382ms
memory: 38732kb

input:

300000
299996 299996 299996 299996 299996 299996 299996 299996 299996 299996 1186 299996 299996 2999...

output:

1 2 3 4 5 6 7 8 9 10 333294 12 13 14 15 16 17 18 19 20 21 22 890564 24 25 26 27 28 29 30 31 32 33 34...

result:

ok 300000 tokens

Test #19:

score: 5
Accepted
time: 344ms
memory: 37608kb

input:

300000
299331 299331 299331 299331 299331 197883 299331 299331 299331 287220 299331 299331 299331 29...

output:

1 2 3 4 5 183735 7 8 9 151619 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 410507 30 31 32 ...

result:

ok 300000 tokens

Test #20:

score: 5
Accepted
time: 384ms
memory: 38416kb

input:

300000
218957 261216 225614 49799 3770 148410 94735 266505 151799 184755 183689 34089 36870 124102 2...

output:

10408189 5122311 169803 3144356 1857797 2953570 2967036 440024 3600365 5117306 4447054 3458077 97329...

result:

ok 300000 tokens