编号 题目 状态 分数 总时间 内存 代码 / 答案文件 提交者 提交时间
#6513 #1084. 树联网 Accepted 100 1761 ms 45532 K C++ 17 / 890 B harkerbest 2024-11-16 15:40:53
显示原始代码
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;
using ll = long long;

int n;
vector<vector<pair<int, int>>> adj;
vector<int> subtree_size;
ll total_damage = 0;

void dfs(int u, int parent) {
    subtree_size[u] = 1;
    for (auto& edge : adj[u]) {
        int v = edge.first;
        int c = edge.second;
        if (v != parent) {
            dfs(v, u);
            subtree_size[u] += subtree_size[v];
            ll diff = abs(n - 2 * subtree_size[v]);
            total_damage += diff * c;
        }
    }
}

int main() {
    cin >> n;
    adj.resize(n + 1);
    subtree_size.resize(n + 1);
    for (int i = 0; i < n - 1; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        adj[a].emplace_back(b, c);
        adj[b].emplace_back(a, c);
    }
    dfs(1, 0);
    cout << total_damage << endl;
    return 0;
}
子任务 #1
Accepted
得分:100
测试点 #1
Accepted
得分:100
用时:5 ms
内存:436 KiB

输入文件(tree0.in

1000
274 165 347303
356 299 375039
246 206 777805
756 591 331410
629 35 299179
997 259 373583
<15283 bytes omitted>

答案文件(tree0.out

493314941306

用户输出

493314941306

系统信息

Exited with return code 0
测试点 #2
Accepted
得分:100
用时:4 ms
内存:276 KiB

输入文件(tree1.in

5
2 1 152103
3 1 176573
5 3 402893
4 3 860890

答案文件(tree1.out

4424231

用户输出

4424231

系统信息

Exited with return code 0
测试点 #3
Accepted
得分:100
用时:12 ms
内存:796 KiB

输入文件(tree2.in

10000
2518 1964 435014
524 203 36875
1538 1461 558868
2035 231 870997
8940 536 82862
4607 3784
<173823 bytes omitted>

答案文件(tree2.out

49981397987782

用户输出

49981397987782

系统信息

Exited with return code 0
测试点 #4
Accepted
得分:100
用时:4 ms
内存:272 KiB

输入文件(tree3.in

1

答案文件(tree3.out

0

用户输出

0

系统信息

Exited with return code 0
测试点 #5
Accepted
得分:100
用时:4 ms
内存:352 KiB

输入文件(tree4.in

77
40 25 64956
34 20 541862
9 4 836201
17 15 809421
49 3 659178
58 9 855176
6 1 456269
15 11
<926 bytes omitted>

答案文件(tree4.out

2643457848

用户输出

2643457848

系统信息

Exited with return code 0
测试点 #6
Accepted
得分:100
用时:7 ms
内存:516 KiB

输入文件(tree5.in

4396
3098 2392 562484
1048 755 372250
2073 1196 812710
1127 1062 947129
647 343 474455
2356 23
<74438 bytes omitted>

答案文件(tree5.out

9576682683804

用户输出

9576682683804

系统信息

Exited with return code 0
测试点 #7
Accepted
得分:100
用时:124 ms
内存:5468 KiB

输入文件(tree6.in

114514
34186 2390 597851
46343 28384 943641
55741 29626 711463
105659 18061 635679
60190 14496 
<2240948 bytes omitted>

答案文件(tree6.out

6546154052762816

用户输出

6546154052762816

系统信息

Exited with return code 0
测试点 #8
Accepted
得分:100
用时:1384 ms
内存:45532 KiB

输入文件(tree7.in

1000000
27843 7866 961704
104408 59430 131588
324055 180563 287371
207837 165968 334696
805756 
<21382013 bytes omitted>

答案文件(tree7.out

499761609565210958

用户输出

499761609565210958

系统信息

Exited with return code 0
测试点 #9
Accepted
得分:100
用时:45 ms
内存:2516 KiB

输入文件(tree8.in

50000
38835 11093 55630
1872 1154 355099
33535 4072 730286
22768 12306 766019
9227 6599 56342

<951594 bytes omitted>

答案文件(tree8.out

1248224228256776

用户输出

1248224228256776

系统信息

Exited with return code 0
测试点 #10
Accepted
得分:100
用时:172 ms
内存:8020 KiB

输入文件(tree9.in

170258
14147 6830 129354
48006 17638 560829
130743 94522 808868
106876 19926 189376
85980 31219
<3417309 bytes omitted>

答案文件(tree9.out

14523668420398970

用户输出

14523668420398970

系统信息

Exited with return code 0