Submission #1113059

# Submission time Handle Problem Language Result Execution time Memory
1113059 2024-11-15T15:06:13 Z Matkap Race (IOI11_race) C++17
100 / 100
286 ms 38648 KB
/*
    Author:Matkap(prefix_sum)
*/
#include "race.h"
#include <bits/stdc++.h>
#include <array>

using namespace std;
#define endl "\n"
#define ar array
#define all(x) x.begin(),x.end()
const int INF = 1e9 + 7 , MOD = 1e9 + 7;

const int mxN = 2e5 + 7, mxK = 2e6 + 7;
vector<ar<int,2>> adj[mxN];
int rem[mxN];
int sub[mxN];
int cnt[mxK];
int ans = INF;
int k1;
vector<int> vec;
int find_subtree_size(int pos,int par = -1)
{
    sub[pos] = 1;
    for(auto [it, dist] : adj[pos])
    {
        if(it == par || rem[it]) continue;
        sub[pos] += find_subtree_size(it, pos);
    }
    return sub[pos];
}
int find_centroid(int sub_size, int pos,int par = -1)
{
    for(auto [it, dist] : adj[pos])
    {
        if(it == par || rem[it]) continue;
        if(sub[it] > sub_size / 2) return find_centroid(sub_size, it, pos);
    }
    return pos;
}
void solve(int pos,int par,int cur_path,int mode, int depth = 1)
{
    if(cur_path > k1) return;
    if(mode == 1) cnt[cur_path] = min(cnt[cur_path], depth), vec.push_back(cur_path);
    else ans = min(ans, cnt[k1 - cur_path] + depth);
    for(auto [it, dist] : adj[pos])
    {
        if(rem[it] || it == par) continue;
        solve(it,pos,cur_path + dist,mode,depth + 1);
    }
}
void build_decomp(int pos)
{
    int centroid = find_centroid(find_subtree_size(pos), pos);
    //cout << centroid << endl;
    rem[centroid] = 1;
    vec.clear();
    for(auto [it, dist] : adj[centroid])
    {
        if(!rem[it])
        {
            solve(it,-1,dist,0);
            solve(it,-1,dist,1);
        }
    }
    for(auto it : vec) cnt[it] = INF;
    cnt[0] = 0;
    for(auto [it, dist] : adj[centroid])
    {
        if(rem[it]) continue;
        build_decomp(it);
    }
}
int best_path(int n,int k, int h[][2], int l[])
{
    k1 = k;
    fill(cnt, cnt + mxK, INF);
    cnt[0] = 0;
    for(int i = 0;n - 1 > i;i++)
    {
        adj[h[i][0]].push_back({h[i][1], l[i]});
        adj[h[i][1]].push_back({h[i][0], l[i]});
    }
    build_decomp(0);
    if(ans == INF) return -1;
    return ans;
}
/*int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int h[1000][2];
    int l[1000];
    int n;
    int k;
    cin >> n >> k;
    for(int i = 0;n - 1 > i;i++) for(int j = 0;2 > j;j++) cin >> h[i][j];
    for(int i = 0;n - 1 > i;i++) cin >> l[i];
    cout << best_path(n, k, h, l) << endl;

}*/
# Verdict Execution time Memory Grader output
1 Correct 10 ms 12880 KB Output is correct
2 Correct 11 ms 12880 KB Output is correct
3 Correct 10 ms 12880 KB Output is correct
4 Correct 10 ms 12880 KB Output is correct
5 Correct 10 ms 12880 KB Output is correct
6 Correct 9 ms 12912 KB Output is correct
7 Correct 10 ms 12880 KB Output is correct
8 Correct 11 ms 12880 KB Output is correct
9 Correct 11 ms 12996 KB Output is correct
10 Correct 10 ms 12880 KB Output is correct
11 Correct 11 ms 12880 KB Output is correct
12 Correct 10 ms 13048 KB Output is correct
13 Correct 11 ms 12880 KB Output is correct
14 Correct 10 ms 12880 KB Output is correct
15 Correct 11 ms 13048 KB Output is correct
16 Correct 11 ms 12880 KB Output is correct
17 Correct 11 ms 12880 KB Output is correct
18 Correct 10 ms 12880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 12880 KB Output is correct
2 Correct 11 ms 12880 KB Output is correct
3 Correct 10 ms 12880 KB Output is correct
4 Correct 10 ms 12880 KB Output is correct
5 Correct 10 ms 12880 KB Output is correct
6 Correct 9 ms 12912 KB Output is correct
7 Correct 10 ms 12880 KB Output is correct
8 Correct 11 ms 12880 KB Output is correct
9 Correct 11 ms 12996 KB Output is correct
10 Correct 10 ms 12880 KB Output is correct
11 Correct 11 ms 12880 KB Output is correct
12 Correct 10 ms 13048 KB Output is correct
13 Correct 11 ms 12880 KB Output is correct
14 Correct 10 ms 12880 KB Output is correct
15 Correct 11 ms 13048 KB Output is correct
16 Correct 11 ms 12880 KB Output is correct
17 Correct 11 ms 12880 KB Output is correct
18 Correct 10 ms 12880 KB Output is correct
19 Correct 11 ms 12880 KB Output is correct
20 Correct 11 ms 12880 KB Output is correct
21 Correct 12 ms 12880 KB Output is correct
22 Correct 11 ms 12972 KB Output is correct
23 Correct 13 ms 12880 KB Output is correct
24 Correct 13 ms 12880 KB Output is correct
25 Correct 11 ms 12880 KB Output is correct
26 Correct 11 ms 12880 KB Output is correct
27 Correct 10 ms 12876 KB Output is correct
28 Correct 11 ms 12880 KB Output is correct
29 Correct 10 ms 12880 KB Output is correct
30 Correct 11 ms 12880 KB Output is correct
31 Correct 11 ms 12880 KB Output is correct
32 Correct 11 ms 12880 KB Output is correct
33 Correct 11 ms 12932 KB Output is correct
34 Correct 11 ms 12880 KB Output is correct
35 Correct 10 ms 12880 KB Output is correct
36 Correct 11 ms 12880 KB Output is correct
37 Correct 11 ms 12880 KB Output is correct
38 Correct 11 ms 12880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 12880 KB Output is correct
2 Correct 11 ms 12880 KB Output is correct
3 Correct 10 ms 12880 KB Output is correct
4 Correct 10 ms 12880 KB Output is correct
5 Correct 10 ms 12880 KB Output is correct
6 Correct 9 ms 12912 KB Output is correct
7 Correct 10 ms 12880 KB Output is correct
8 Correct 11 ms 12880 KB Output is correct
9 Correct 11 ms 12996 KB Output is correct
10 Correct 10 ms 12880 KB Output is correct
11 Correct 11 ms 12880 KB Output is correct
12 Correct 10 ms 13048 KB Output is correct
13 Correct 11 ms 12880 KB Output is correct
14 Correct 10 ms 12880 KB Output is correct
15 Correct 11 ms 13048 KB Output is correct
16 Correct 11 ms 12880 KB Output is correct
17 Correct 11 ms 12880 KB Output is correct
18 Correct 10 ms 12880 KB Output is correct
19 Correct 92 ms 19836 KB Output is correct
20 Correct 99 ms 18520 KB Output is correct
21 Correct 113 ms 18800 KB Output is correct
22 Correct 89 ms 20272 KB Output is correct
23 Correct 62 ms 20040 KB Output is correct
24 Correct 52 ms 19784 KB Output is correct
25 Correct 102 ms 21320 KB Output is correct
26 Correct 78 ms 26072 KB Output is correct
27 Correct 122 ms 27220 KB Output is correct
28 Correct 172 ms 38648 KB Output is correct
29 Correct 168 ms 36168 KB Output is correct
30 Correct 105 ms 26952 KB Output is correct
31 Correct 102 ms 27096 KB Output is correct
32 Correct 116 ms 24392 KB Output is correct
33 Correct 146 ms 26076 KB Output is correct
34 Correct 131 ms 26944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 12880 KB Output is correct
2 Correct 11 ms 12880 KB Output is correct
3 Correct 10 ms 12880 KB Output is correct
4 Correct 10 ms 12880 KB Output is correct
5 Correct 10 ms 12880 KB Output is correct
6 Correct 9 ms 12912 KB Output is correct
7 Correct 10 ms 12880 KB Output is correct
8 Correct 11 ms 12880 KB Output is correct
9 Correct 11 ms 12996 KB Output is correct
10 Correct 10 ms 12880 KB Output is correct
11 Correct 11 ms 12880 KB Output is correct
12 Correct 10 ms 13048 KB Output is correct
13 Correct 11 ms 12880 KB Output is correct
14 Correct 10 ms 12880 KB Output is correct
15 Correct 11 ms 13048 KB Output is correct
16 Correct 11 ms 12880 KB Output is correct
17 Correct 11 ms 12880 KB Output is correct
18 Correct 10 ms 12880 KB Output is correct
19 Correct 11 ms 12880 KB Output is correct
20 Correct 11 ms 12880 KB Output is correct
21 Correct 12 ms 12880 KB Output is correct
22 Correct 11 ms 12972 KB Output is correct
23 Correct 13 ms 12880 KB Output is correct
24 Correct 13 ms 12880 KB Output is correct
25 Correct 11 ms 12880 KB Output is correct
26 Correct 11 ms 12880 KB Output is correct
27 Correct 10 ms 12876 KB Output is correct
28 Correct 11 ms 12880 KB Output is correct
29 Correct 10 ms 12880 KB Output is correct
30 Correct 11 ms 12880 KB Output is correct
31 Correct 11 ms 12880 KB Output is correct
32 Correct 11 ms 12880 KB Output is correct
33 Correct 11 ms 12932 KB Output is correct
34 Correct 11 ms 12880 KB Output is correct
35 Correct 10 ms 12880 KB Output is correct
36 Correct 11 ms 12880 KB Output is correct
37 Correct 11 ms 12880 KB Output is correct
38 Correct 11 ms 12880 KB Output is correct
39 Correct 92 ms 19836 KB Output is correct
40 Correct 99 ms 18520 KB Output is correct
41 Correct 113 ms 18800 KB Output is correct
42 Correct 89 ms 20272 KB Output is correct
43 Correct 62 ms 20040 KB Output is correct
44 Correct 52 ms 19784 KB Output is correct
45 Correct 102 ms 21320 KB Output is correct
46 Correct 78 ms 26072 KB Output is correct
47 Correct 122 ms 27220 KB Output is correct
48 Correct 172 ms 38648 KB Output is correct
49 Correct 168 ms 36168 KB Output is correct
50 Correct 105 ms 26952 KB Output is correct
51 Correct 102 ms 27096 KB Output is correct
52 Correct 116 ms 24392 KB Output is correct
53 Correct 146 ms 26076 KB Output is correct
54 Correct 131 ms 26944 KB Output is correct
55 Correct 15 ms 13392 KB Output is correct
56 Correct 15 ms 13724 KB Output is correct
57 Correct 56 ms 20680 KB Output is correct
58 Correct 32 ms 20420 KB Output is correct
59 Correct 73 ms 25808 KB Output is correct
60 Correct 286 ms 35772 KB Output is correct
61 Correct 114 ms 27596 KB Output is correct
62 Correct 129 ms 28096 KB Output is correct
63 Correct 156 ms 28096 KB Output is correct
64 Correct 278 ms 28348 KB Output is correct
65 Correct 162 ms 28232 KB Output is correct
66 Correct 261 ms 35760 KB Output is correct
67 Correct 81 ms 29804 KB Output is correct
68 Correct 173 ms 28616 KB Output is correct
69 Correct 182 ms 28612 KB Output is correct
70 Correct 153 ms 27928 KB Output is correct