ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#203758 | #525. 神树的权值 | snow_trace | 100 | 7539ms | 85132kb | C++11 | 1.2kb | 2024-03-14 10:36:48 | 2024-03-14 21:22:09 |
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
vector<int>p[300005];
vector<int>ss[300005];
int sz[300005],fa[300005],s[300005],tag[300005],ans[300005],ok[300005];int n;
vector<int>l[300005];
int find(int x){
if(fa[x] == x)return x;
return fa[x] = find(fa[x]);
}void merge(int x,int y){
if(sz[x]>sz[y])swap(x,y);
tag[x]-=tag[y];
for(int a:ss[x]){
ans[a]+=tag[x];
ss[y].push_back(a);
}sz[y]+=sz[x];fa[x] = y;
}
signed main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n;for(int i = 1;i<=n;i++)cin >> s[i];
for(int i = 1;i<=n;i++)tag[i] = i,fa[i] = i,sz[i] = 1,ss[i].push_back(i);
for(int i = 1;i<n;i++){
int a,b;cin >> a >> b;p[a].push_back(b),p[b].push_back(a);
}for(int i = 1;i<=n;i++)l[s[i]].push_back(i);
for(int i = 1;i<=n;i++){
for(int x:l[i]){
for(int xx:p[x]){
if(ok[xx])tag[find(xx)]+=x;
}
}for(int x:l[i]){
ok[x] = 1;
for(int xx:p[x]){
if(ok[xx]){
merge(find(xx),find(x));
}
}
}
}for(int i = 1;i<=n;i++)ans[i]+=tag[find(i)];
for(int i = 1;i<=n;i++)cout << ans[i] << " ";cout << endl;
return 0;
}/*
启发式合并不管咋做都对。
si有相同要注意点细节。
*/
Details
小提示:点击横条可展开更详细的信息
Test #1:
score: 5
Accepted
time: 12ms
memory: 22992kb
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: 0ms
memory: 23020kb
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: 4ms
memory: 23060kb
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: 9ms
memory: 23044kb
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: 3ms
memory: 23088kb
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: 8ms
memory: 23040kb
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: 923ms
memory: 76340kb
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: 327ms
memory: 68060kb
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: 314ms
memory: 69864kb
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: 288ms
memory: 69492kb
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: 335ms
memory: 70568kb
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: 313ms
memory: 68004kb
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: 626ms
memory: 79308kb
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: 761ms
memory: 82060kb
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: 553ms
memory: 81592kb
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: 722ms
memory: 85132kb
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: 478ms
memory: 74364kb
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: 555ms
memory: 79404kb
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: 494ms
memory: 77016kb
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: 814ms
memory: 78620kb
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