Submission #786374

# Submission time Handle Problem Language Result Execution time Memory
786374 2023-07-18T07:14:08 Z 이동현(#10029) Paths (RMI21_paths) C++17
56 / 100
600 ms 26140 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define int long long
using namespace std;

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, k;
    cin >> n >> k;
    vector<vector<vector<int>>> way(n);
    for(int i = 1; i < n; ++i){
        int x, y, z;
        cin >> x >> y >> z;
        --x, --y;
        way[x].push_back({y, z});
        way[y].push_back({x, z});
    }

    auto sol = [&](int rt){
        vector<vector<int>> vc(n);
        vector<int> mx(n);

        auto dfs = [&](auto&&self, int x, int pr)->void{
            for(auto&nxt:way[x]){
                if(nxt[0] == pr){
                    continue;
                }

                self(self, nxt[0], x);
                mx[nxt[0]] += nxt[1];

                if((int)vc[nxt[0]].size() > (int)vc[x].size()){
                    swap(vc[x], vc[nxt[0]]);
                    swap(mx[x], mx[nxt[0]]);
                }
                if(!(int)vc[nxt[0]].size()){
                    continue;
                }
                
                for(auto&i:vc[nxt[0]]){
                    vc[x].push_back(i);
                }
                if(mx[x] < mx[nxt[0]]){
                    vc[x].push_back(mx[x]);
                    mx[x] = mx[nxt[0]];
                }
                else{
                    vc[x].push_back(mx[nxt[0]]);
                }
            }

            if(!(int)vc[x].size()){
                vc[x] = {0};
            }
        };

        dfs(dfs, rt, -1);

        int ans = mx[rt];
        sort(vc[rt].begin(), vc[rt].end());
        reverse(vc[rt].begin(), vc[rt].end());
        for(int i = 0; i < min((int)vc[rt].size(), k - 1); ++i){
            ans += vc[rt][i];
        }

        return ans;
    };

    for(int i = 0; i < n; ++i){
        cout << sol(i) << '\n';
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 6 ms 368 KB Output is correct
4 Correct 4 ms 340 KB Output is correct
5 Correct 5 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 4 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 6 ms 368 KB Output is correct
4 Correct 4 ms 340 KB Output is correct
5 Correct 5 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 4 ms 340 KB Output is correct
8 Correct 119 ms 556 KB Output is correct
9 Correct 62 ms 576 KB Output is correct
10 Correct 37 ms 524 KB Output is correct
11 Correct 96 ms 552 KB Output is correct
12 Correct 55 ms 528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 6 ms 368 KB Output is correct
4 Correct 4 ms 340 KB Output is correct
5 Correct 5 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 4 ms 340 KB Output is correct
8 Correct 119 ms 556 KB Output is correct
9 Correct 62 ms 576 KB Output is correct
10 Correct 37 ms 524 KB Output is correct
11 Correct 96 ms 552 KB Output is correct
12 Correct 55 ms 528 KB Output is correct
13 Correct 525 ms 840 KB Output is correct
14 Correct 341 ms 872 KB Output is correct
15 Correct 196 ms 888 KB Output is correct
16 Correct 421 ms 828 KB Output is correct
17 Correct 301 ms 772 KB Output is correct
18 Correct 254 ms 712 KB Output is correct
19 Correct 460 ms 828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1067 ms 26140 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 6 ms 368 KB Output is correct
4 Correct 4 ms 340 KB Output is correct
5 Correct 5 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 4 ms 340 KB Output is correct
8 Correct 119 ms 556 KB Output is correct
9 Correct 62 ms 576 KB Output is correct
10 Correct 37 ms 524 KB Output is correct
11 Correct 96 ms 552 KB Output is correct
12 Correct 55 ms 528 KB Output is correct
13 Correct 525 ms 840 KB Output is correct
14 Correct 341 ms 872 KB Output is correct
15 Correct 196 ms 888 KB Output is correct
16 Correct 421 ms 828 KB Output is correct
17 Correct 301 ms 772 KB Output is correct
18 Correct 254 ms 712 KB Output is correct
19 Correct 460 ms 828 KB Output is correct
20 Execution timed out 1067 ms 26140 KB Time limit exceeded
21 Halted 0 ms 0 KB -