Submission #643563

# Submission time Handle Problem Language Result Execution time Memory
643563 2022-09-22T12:54:47 Z danikoynov Paths (RMI21_paths) C++14
56 / 100
600 ms 12096 KB
/**
 ____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|

**/

#include<bits/stdc++.h>
#define endl '\n'

using namespace std;
typedef long long ll;

void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
const int maxn = 1e5 + 10;

int n, k;
ll depth[maxn], mx_depth[maxn], num[maxn];
vector < pair < int, ll > > g[maxn];

void calc(int ver, int par)
{
    mx_depth[ver] = depth[ver];
    for (pair < int, ll > nb : g[ver])
    {
        if (nb.first == par)
            continue;
        depth[nb.first] = depth[ver] + nb.second;
        calc(nb.first, ver);
        mx_depth[ver] = max(mx_depth[ver], mx_depth[nb.first]);
    }
}

bool cmp(pair < int, ll > p1, pair < int, ll > p2)
{
    return mx_depth[p1.first] > mx_depth[p2.first];
}

void trav(int ver, int par, ll cur_depth)
{
    sort(g[ver].begin(), g[ver].end(), cmp);
    ///cout << ver << " " << par << " " << cur_depth << endl;
    bool leaf = true, tf = true;
    for (pair < int, ll > nb : g[ver])
    {
        if (nb.first == par)
            continue;
        leaf = false;
        if (tf)
        {
            tf = false;
            trav(nb.first, ver, cur_depth + nb.second);
        }
        else
        trav(nb.first, ver, nb.second);
    }

    if (leaf)
        num[ver] = cur_depth;
}

void solve()
{
    cin >> n >> k;
    for (int i = 1; i < n; i ++)
    {
        int v, u;
        ll l;
        cin >> v >> u >> l;
        g[v].push_back({u, l});
        g[u].push_back({v, l});
    }

    for (int root = 1; root <= n; root ++)
    {
        depth[root] = 0;
        for (int i = 1; i <= n; i ++)
            num[i] = 0;
        calc(root, - 1);
        trav(root, -1, 0);
        /**for (int i = 1; i <= n; i ++)
            cout << num[i] << " ";
        cout << endl;*/
        sort(num + 1, num + n + 1);
        ll ans = 0;
        for (int i = n; i > n - k; i --)
            ans = ans + num[i];
        cout << ans << endl;
    }


}

int main()
{
    solve();
    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 5 ms 2656 KB Output is correct
4 Correct 4 ms 2656 KB Output is correct
5 Correct 4 ms 2644 KB Output is correct
6 Correct 3 ms 2660 KB Output is correct
7 Correct 4 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 5 ms 2656 KB Output is correct
4 Correct 4 ms 2656 KB Output is correct
5 Correct 4 ms 2644 KB Output is correct
6 Correct 3 ms 2660 KB Output is correct
7 Correct 4 ms 2644 KB Output is correct
8 Correct 73 ms 2756 KB Output is correct
9 Correct 69 ms 2920 KB Output is correct
10 Correct 55 ms 2716 KB Output is correct
11 Correct 74 ms 2744 KB Output is correct
12 Correct 55 ms 2748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 5 ms 2656 KB Output is correct
4 Correct 4 ms 2656 KB Output is correct
5 Correct 4 ms 2644 KB Output is correct
6 Correct 3 ms 2660 KB Output is correct
7 Correct 4 ms 2644 KB Output is correct
8 Correct 73 ms 2756 KB Output is correct
9 Correct 69 ms 2920 KB Output is correct
10 Correct 55 ms 2716 KB Output is correct
11 Correct 74 ms 2744 KB Output is correct
12 Correct 55 ms 2748 KB Output is correct
13 Correct 312 ms 2972 KB Output is correct
14 Correct 263 ms 2900 KB Output is correct
15 Correct 208 ms 2912 KB Output is correct
16 Correct 328 ms 2824 KB Output is correct
17 Correct 247 ms 2848 KB Output is correct
18 Correct 154 ms 2828 KB Output is correct
19 Correct 303 ms 2856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1078 ms 12096 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 5 ms 2656 KB Output is correct
4 Correct 4 ms 2656 KB Output is correct
5 Correct 4 ms 2644 KB Output is correct
6 Correct 3 ms 2660 KB Output is correct
7 Correct 4 ms 2644 KB Output is correct
8 Correct 73 ms 2756 KB Output is correct
9 Correct 69 ms 2920 KB Output is correct
10 Correct 55 ms 2716 KB Output is correct
11 Correct 74 ms 2744 KB Output is correct
12 Correct 55 ms 2748 KB Output is correct
13 Correct 312 ms 2972 KB Output is correct
14 Correct 263 ms 2900 KB Output is correct
15 Correct 208 ms 2912 KB Output is correct
16 Correct 328 ms 2824 KB Output is correct
17 Correct 247 ms 2848 KB Output is correct
18 Correct 154 ms 2828 KB Output is correct
19 Correct 303 ms 2856 KB Output is correct
20 Execution timed out 1078 ms 12096 KB Time limit exceeded
21 Halted 0 ms 0 KB -