QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#134625#5718. Radio PrizeNicolas125841WA 2ms3656kbC++172.4kb2023-08-04 07:22:552023-08-04 07:22:58

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-04 07:22:58]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:3656kb
  • [2023-08-04 07:22:55]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

struct Node {
    int tsum;
    int size;
    int psum;
    int wsum;
};

void prep(vector<vector<pair<int, int>>> &adj, vector<int> &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;

    int ans = 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, int>>> &adj, vector<int> &taxes, vector<Node> &vals, vector<int> &ans, int n, int p, int pw){    
    int p_tsum = vals[p].tsum - vals[n].tsum;
    int p_size = vals[p].size - vals[n].size;
    int p_psum = vals[p].psum - vals[n].psum - pw * vals[n].tsum;
    int 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<int> taxes(n);
    for(int i = 0; i < n; i++)
        cin >> taxes[i];

    vector<vector<pair<int, int>>> adj(n, vector<pair<int, int>>());
    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});
    }

    vector<Node> vals(n, Node());

    int 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<int> ans(n);

    ans[lnode] = vals[lnode].psum + vals[lnode].wsum * taxes[lnode];

    push(adj, taxes, vals, ans, oonode, lnode, lweight);

    for(int v : ans)
        cout << v << "\n";
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3524kb

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: 1ms
memory: 3540kb

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: 3476kb

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: 0ms
memory: 3456kb

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: 3452kb

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: -100
Wrong Answer
time: 2ms
memory: 3656kb

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
-759336280
-1009306637
-1784804680
1106088805
1534689627
-2031542671
-871087273
-1338125391
1991715171
-2118997597
-766658810
-264886774
1713517377
-1316537569
847129236
1196094586
1122231318
1823141725
1455582480
1095480603
-160118301...

result:

wrong answer 7th lines differ - expected: '3535631016', found: '-759336280'