QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#134627 | #5718. Radio Prize | Nicolas125841 | AC ✓ | 57ms | 27924kb | C++17 | 2.4kb | 2023-08-04 07:26:29 | 2023-08-04 07:26:33 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Node {
ll tsum;
ll size;
ll psum;
ll wsum;
};
void prep(vector<vector<pair<int, ll>>> &adj, vector<ll> &taxes, vector<Node> &vals, int n, int p){
vals[n].tsum = taxes[n];
vals[n].size = 1;
vals[n].psum = 0;
vals[n].wsum = 0;
for(auto &v : adj[n]){
if(v.first != p){
prep(adj, taxes, vals, v.first, n);
vals[n].tsum += vals[v.first].tsum;
vals[n].size += vals[v.first].size;
vals[n].psum += vals[v.first].psum + v.second * vals[v.first].tsum;
vals[n].wsum += vals[v.first].wsum + v.second * vals[v.first].size;
}
}
}
void push(vector<vector<pair<int, ll>>> &adj, vector<ll> &taxes, vector<Node> &vals, vector<ll> &ans, int n, int p, ll pw){
ll p_tsum = vals[p].tsum - vals[n].tsum;
ll p_size = vals[p].size - vals[n].size;
ll p_psum = vals[p].psum - vals[n].psum - pw * vals[n].tsum;
ll p_wsum = vals[p].wsum - vals[n].wsum - pw * vals[n].size;
vals[n].tsum += p_tsum;
vals[n].size += p_size;
vals[n].psum += p_psum + pw * p_tsum;
vals[n].wsum += p_wsum + pw * p_size;
ans[n] = vals[n].psum + vals[n].wsum * taxes[n];
for(auto &v : adj[n]){
if(v.first != p){
push(adj, taxes, vals, ans, v.first, n, v.second);
}
}
}
int main(){
cin.tie(NULL)->sync_with_stdio(false);
int n;
cin >> n;
vector<ll> taxes(n);
for(int i = 0; i < n; i++)
cin >> taxes[i];
vector<vector<pair<int, ll>>> adj(n, vector<pair<int, ll>>());
for(int i = 0; i < n-1; i++){
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
if(n == 1){
cout << "0\n";
return 0;
}
vector<Node> vals(n, Node());
ll lnode, oonode, lweight;
for(int i = 0; i < n; i++){
if(adj[i].size() == 1){
lnode = i;
oonode = adj[i][0].first;
lweight = adj[i][0].second;
break;
}
}
prep(adj, taxes, vals, lnode, -1);
vector<ll> ans(n);
ans[lnode] = vals[lnode].psum + vals[lnode].wsum * taxes[lnode];
push(adj, taxes, vals, ans, oonode, lnode, lweight);
for(ll v : ans)
cout << v << "\n";
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3576kb
input:
5 2 5 3 4 1 1 2 2 2 4 5 4 3 3 5 2 6
output:
130 159 191 163 171
result:
ok 5 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 3464kb
input:
6 4 3 3 4 3 3 1 3 2 2 1 1 1 4 6 4 5 6 6 4 2
output:
209 206 232 209 336 232
result:
ok 6 lines
Test #3:
score: 0
Accepted
time: 1ms
memory: 3472kb
input:
6 5 3 1 4 2 9 1 2 4 1 3 5 2 4 3 2 5 2 3 6 1
output:
251 219 169 343 247 515
result:
ok 6 lines
Test #4:
score: 0
Accepted
time: 1ms
memory: 3520kb
input:
20 40 25 508 503 649 573 327 25 789 275 403 75 545 840 434 976 613 686 385 394 13 14 886 13 5 918 13 19 878 13 4 448 13 11 446 13 20 155 13 7 688 13 1 168 13 9 722 13 16 984 13 6 946 13 15 840 13 10 996 13 17 139 13 8 21 13 12 530 13 18 456 13 2 230 13 3 479
output:
7841480 8224040 19561727 18938254 30721592 29285148 19184286 6245855 30057068 22190730 17097428 12034040 11730690 34894470 23965020 40727624 15097277 22410516 23349440 12462455
result:
ok 20 lines
Test #5:
score: 0
Accepted
time: 1ms
memory: 3572kb
input:
20 797 756 909 933 709 21 306 500 829 987 257 891 131 59 482 240 131 369 305 783 8 14 964 7 20 985 18 4 890 2 13 610 13 17 870 3 15 442 10 8 963 11 19 350 15 11 859 4 10 151 17 5 30 19 2 870 20 16 735 9 7 421 14 1 799 16 6 940 1 3 83 6 18 415 5 12 35
output:
82744514 105218319 90065375 100200991 125862190 44024326 97083381 65686016 168953945 102556236 55425516 145226549 59198424 36457291 65265048 70194641 66825714 68191281 61240162 133205051
result:
ok 20 lines
Test #6:
score: 0
Accepted
time: 2ms
memory: 3764kb
input:
2000 273 716 154 256 300 307 782 691 965 234 241 862 990 934 245 806 703 848 71 851 237 387 26 579 764 508 583 578 807 717 64 377 973 320 228 847 202 79 839 830 303 494 837 309 251 556 290 565 955 178 264 611 392 96 859 604 149 346 760 598 671 563 713 911 364 189 685 605 520 746 844 243 208 899 482 ...
output:
905245307 1417620819 1216467880 1890377090 1619079013 1165043217 3535631016 3285660659 2510162616 1106088805 1534689627 2263424625 3423880023 2956841905 1991715171 2175969699 3528308486 4030080522 1713517377 2978429727 847129236 1196094586 1122231318 1823141725 1455582480 1095480603 2693784279 15837...
result:
ok 2000 lines
Test #7:
score: 0
Accepted
time: 2ms
memory: 4036kb
input:
2000 532 953 286 641 51 958 541 222 581 160 664 760 138 950 771 402 525 613 715 19 819 704 634 436 42 607 277 55 954 708 293 420 501 244 4 199 197 809 511 740 449 294 836 970 815 27 24 720 527 818 368 470 985 887 430 114 879 642 744 324 838 11 288 778 712 249 727 837 661 6 374 643 266 646 220 650 82...
output:
675339797380 1290693128892 771181489882 802401705476 388203435690 1025867996600 515877904074 372420921720 693121505548 350261818840 788402982804 823401610342 328309345062 1053735905952 1186136438942 630178657250 509157157660 606193859534 890402340168 377624305006 929268618368 808613413452 5623283053...
result:
ok 2000 lines
Test #8:
score: 0
Accepted
time: 36ms
memory: 14976kb
input:
100000 166 470 274 38 308 126 898 380 515 353 758 428 800 293 43 768 186 264 254 78 603 455 75 31 925 767 64 492 897 500 659 676 852 395 356 713 129 28 591 463 43 205 97 422 111 851 554 382 257 477 397 21 951 143 436 694 598 565 494 984 310 85 461 172 756 360 585 718 329 116 900 109 573 462 326 847 ...
output:
47436372238 54136637938 66045919432 69661272034 40973632498 36256390480 71726867776 90495678448 95843922553 122967655885 101039235256 52814256814 107792998498 68539431079 38678627713 163615056112 53391024214 105861958690 72261696682 55111631476 143764718551 115785916813 33360949753 52333209751 19812...
result:
ok 100000 lines
Test #9:
score: 0
Accepted
time: 55ms
memory: 27924kb
input:
100000 201 484 644 454 92 647 530 521 268 657 48 745 486 209 762 241 464 852 55 454 733 291 608 744 190 910 587 878 47 804 633 934 224 424 780 425 674 787 842 335 486 790 101 241 206 68 594 566 895 497 558 312 111 433 647 257 526 347 185 526 720 459 196 453 527 993 248 936 329 447 125 350 727 181 13...
output:
886395450917227 1239648585601779 1604049654939926 1791359196745518 762793966281023 1989308836817989 2022123213667199 1498524176017370 1284433719206884 1468859131548805 861423218378140 1565194805792002 1236459386768544 1775615232633333 2229201730039592 933439536059870 1221279877820147 234679963329788...
result:
ok 100000 lines
Test #10:
score: 0
Accepted
time: 52ms
memory: 16016kb
input:
100000 845 814 332 436 81 793 587 185 431 855 576 382 47 614 530 362 913 278 210 495 562 812 642 487 919 956 350 669 414 114 925 42 181 50 837 406 428 490 832 42 908 152 927 526 31 368 259 306 516 120 467 771 854 301 337 652 960 112 642 741 149 429 508 429 813 609 248 329 882 893 50 669 200 961 618 ...
output:
1939487454200 1643933970761 731713512635 1301731424933 848436650050 1571689418248 1458928257370 949992680924 1087296211294 1760659074748 1673749976843 1210253047443 706124455200 1463473496403 1053915261963 1117592007451 1832880085060 1061890006367 968527776423 1217822516502 1563848456255 18830300285...
result:
ok 100000 lines
Test #11:
score: 0
Accepted
time: 54ms
memory: 16044kb
input:
100000 45 482 813 461 393 433 107 651 28 612 809 416 338 889 494 736 827 199 305 716 414 818 150 559 242 940 87 45 732 42 837 137 568 498 480 946 719 196 910 657 490 401 423 497 706 362 934 702 868 53 704 476 820 59 699 557 661 133 667 824 317 694 332 209 905 303 781 366 337 938 690 868 219 767 497 ...
output:
820732206828 1518111005126 1436536576952 1433964866054 1355164687148 1318927463622 742997566594 1585878558944 686770452836 1508976648536 2003050730834 1383501577870 1275802713782 2013999417690 1468867361728 1848860296728 1821130537808 964380811682 1184770000436 1903368108058 1349533833400 1727797456...
result:
ok 100000 lines
Test #12:
score: 0
Accepted
time: 57ms
memory: 27784kb
input:
100000 203 286 735 13 323 411 11 78 114 609 411 897 289 111 291 358 698 790 663 535 289 454 838 19 700 254 148 939 787 653 604 453 308 609 328 573 142 23 863 139 506 553 902 837 275 936 308 510 908 299 658 223 383 566 163 486 227 631 892 300 32 882 76 929 810 711 753 891 173 633 605 132 714 582 121 ...
output:
2477533138102000 2875618728022000 3425548098566000 2427589758244000 2315882023322000 2529573479466000 2262620015230000 2103306886586000 1669787195042000 3342089282132000 2345463350908000 6038927298238000 2826894472526000 2076137755176000 1989322485048000 3953135113976000 5863978537836000 40395801902...
result:
ok 100000 lines