Submission #1114889

# Submission time Handle Problem Language Result Execution time Memory
1114889 2024-11-19T17:48:52 Z DobromirAngelov Race (IOI11_race) C++14
100 / 100
572 ms 123920 KB
#include "race.h"
#include<bits/stdc++.h>
 
#define fi first
#define se second
 
using namespace std;
 
const int MAXN=2e5+5;
const int MAXK=1e6+5;
const int INF=1e9+5;
 
int n,k;
vector<pair<int,int> > adj[MAXN];
int subtr[MAXN];
vector<int> ct_adj[MAXN];
vector<pair<long long,int> > ancs[MAXN];
bool rem[MAXN];
int minCnt[MAXK];
vector<pair<int,int> > upd;
vector<int> hist;
int ans=INF;
 
int get_subtr_sizes(int v,int par)
{
    subtr[v]=1;
    for(auto [u,w]: adj[v])
    {
        if(u==par || rem[u]) continue;
        subtr[v]+=get_subtr_sizes(u,v);
    }
    return subtr[v];
}
 
int get_centroid(int v,int par,int sz)
{
    for(auto [u,w]: adj[v])
    {
        if(u==par || rem[u]) continue;
        if(subtr[u]*2>sz) return get_centroid(u,v,sz);
    }
    return v;
}
 
void get_dists(int v,int par,long long dist,int cnt)
{
    ancs[v].push_back({dist, cnt});
    for(auto [u,w]: adj[v])
    {
        if(u==par || rem[u]) continue;
        get_dists(u, v, dist+w, cnt+1);
    }
}
 
int build_centroid_decomp(int v)
{
    int centroid=get_centroid(v, -1, get_subtr_sizes(v,-1));
 
    for(auto [u,w]: adj[centroid])
    {
        if(rem[u]) continue;
        get_dists(u,centroid,w,1);
    }
 
    rem[centroid]=1;
 
    for(auto [u,w]: adj[centroid])
    {
        if(rem[u]) continue;
        int cur=build_centroid_decomp(u);
        ct_adj[centroid].push_back(cur);
    }
    return centroid;
}
 
void dfs2(int v,int ptr)
{
    auto [dist, cnt] = ancs[v][ptr];
    if(-dist+k>=0) ans=min(ans, minCnt[k-dist]+cnt);
    if(dist>0 && dist<=k)
    {
        upd.push_back({dist, cnt});
        hist.push_back(dist);
    }
    for(auto u: ct_adj[v])
    {
        dfs2(u,ptr);
    }
}
 
void dfs(int v,int depth)
{
    for(auto u: ct_adj[v])
    {
        dfs2(u,depth);
        for(auto [dist,cnt]: upd)
        {
            minCnt[dist]=min(minCnt[dist],cnt);
        }
        upd.clear();
    }
    for(auto dist: hist)
    {
        minCnt[dist]=INF;
    }
  	hist.clear();
 
    for(auto u: ct_adj[v])
    {
        dfs(u,depth+1);
    }
}
 
void init_dfs()
{
    minCnt[0]=0;
    for(int i=1;i<=k;i++) minCnt[i]=INF;
    upd.reserve(n);
    hist.reserve(n);
}
 
int best_path(int N, int K, int H[][2], int L[])
{
    n=N; k=K;
    for(int i=0;i<n-1;i++)
    {
        int u=H[i][0]; u++;
        int v=H[i][1]; v++;
        int w=L[i];
        adj[u].push_back({v,w});
        adj[v].push_back({u,w});
    }
 
    int root=build_centroid_decomp(1);
 
    init_dfs();
    dfs(root,0);
 
    if(ans>=INF) return -1;
    else return ans;
}

Compilation message

race.cpp: In function 'int get_subtr_sizes(int, int)':
race.cpp:27:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   27 |     for(auto [u,w]: adj[v])
      |              ^
race.cpp: In function 'int get_centroid(int, int, int)':
race.cpp:37:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   37 |     for(auto [u,w]: adj[v])
      |              ^
race.cpp: In function 'void get_dists(int, int, long long int, int)':
race.cpp:48:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   48 |     for(auto [u,w]: adj[v])
      |              ^
race.cpp: In function 'int build_centroid_decomp(int)':
race.cpp:59:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   59 |     for(auto [u,w]: adj[centroid])
      |              ^
race.cpp:67:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   67 |     for(auto [u,w]: adj[centroid])
      |              ^
race.cpp: In function 'void dfs2(int, int)':
race.cpp:78:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   78 |     auto [dist, cnt] = ancs[v][ptr];
      |          ^
race.cpp: In function 'void dfs(int, int)':
race.cpp:96:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   96 |         for(auto [dist,cnt]: upd)
      |                  ^
# Verdict Execution time Memory Grader output
1 Correct 6 ms 20992 KB Output is correct
2 Correct 4 ms 20984 KB Output is correct
3 Correct 4 ms 20816 KB Output is correct
4 Correct 4 ms 20816 KB Output is correct
5 Correct 4 ms 20816 KB Output is correct
6 Correct 4 ms 20816 KB Output is correct
7 Correct 5 ms 20816 KB Output is correct
8 Correct 6 ms 20816 KB Output is correct
9 Correct 5 ms 20816 KB Output is correct
10 Correct 5 ms 20816 KB Output is correct
11 Correct 4 ms 20816 KB Output is correct
12 Correct 4 ms 20816 KB Output is correct
13 Correct 4 ms 20816 KB Output is correct
14 Correct 5 ms 18768 KB Output is correct
15 Correct 4 ms 20816 KB Output is correct
16 Correct 5 ms 20816 KB Output is correct
17 Correct 4 ms 20816 KB Output is correct
18 Correct 5 ms 20816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 20992 KB Output is correct
2 Correct 4 ms 20984 KB Output is correct
3 Correct 4 ms 20816 KB Output is correct
4 Correct 4 ms 20816 KB Output is correct
5 Correct 4 ms 20816 KB Output is correct
6 Correct 4 ms 20816 KB Output is correct
7 Correct 5 ms 20816 KB Output is correct
8 Correct 6 ms 20816 KB Output is correct
9 Correct 5 ms 20816 KB Output is correct
10 Correct 5 ms 20816 KB Output is correct
11 Correct 4 ms 20816 KB Output is correct
12 Correct 4 ms 20816 KB Output is correct
13 Correct 4 ms 20816 KB Output is correct
14 Correct 5 ms 18768 KB Output is correct
15 Correct 4 ms 20816 KB Output is correct
16 Correct 5 ms 20816 KB Output is correct
17 Correct 4 ms 20816 KB Output is correct
18 Correct 5 ms 20816 KB Output is correct
19 Correct 3 ms 20816 KB Output is correct
20 Correct 3 ms 20816 KB Output is correct
21 Correct 5 ms 21240 KB Output is correct
22 Correct 5 ms 23120 KB Output is correct
23 Correct 7 ms 21072 KB Output is correct
24 Correct 6 ms 21072 KB Output is correct
25 Correct 6 ms 23120 KB Output is correct
26 Correct 5 ms 23120 KB Output is correct
27 Correct 8 ms 19540 KB Output is correct
28 Correct 11 ms 16208 KB Output is correct
29 Correct 6 ms 18256 KB Output is correct
30 Correct 7 ms 18680 KB Output is correct
31 Correct 6 ms 21072 KB Output is correct
32 Correct 7 ms 21072 KB Output is correct
33 Correct 9 ms 21248 KB Output is correct
34 Correct 5 ms 23120 KB Output is correct
35 Correct 5 ms 23120 KB Output is correct
36 Correct 7 ms 21072 KB Output is correct
37 Correct 5 ms 23120 KB Output is correct
38 Correct 7 ms 23120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 20992 KB Output is correct
2 Correct 4 ms 20984 KB Output is correct
3 Correct 4 ms 20816 KB Output is correct
4 Correct 4 ms 20816 KB Output is correct
5 Correct 4 ms 20816 KB Output is correct
6 Correct 4 ms 20816 KB Output is correct
7 Correct 5 ms 20816 KB Output is correct
8 Correct 6 ms 20816 KB Output is correct
9 Correct 5 ms 20816 KB Output is correct
10 Correct 5 ms 20816 KB Output is correct
11 Correct 4 ms 20816 KB Output is correct
12 Correct 4 ms 20816 KB Output is correct
13 Correct 4 ms 20816 KB Output is correct
14 Correct 5 ms 18768 KB Output is correct
15 Correct 4 ms 20816 KB Output is correct
16 Correct 5 ms 20816 KB Output is correct
17 Correct 4 ms 20816 KB Output is correct
18 Correct 5 ms 20816 KB Output is correct
19 Correct 221 ms 54344 KB Output is correct
20 Correct 224 ms 54148 KB Output is correct
21 Correct 236 ms 53804 KB Output is correct
22 Correct 176 ms 49716 KB Output is correct
23 Correct 158 ms 57396 KB Output is correct
24 Correct 95 ms 43064 KB Output is correct
25 Correct 214 ms 61772 KB Output is correct
26 Correct 156 ms 63928 KB Output is correct
27 Correct 242 ms 63412 KB Output is correct
28 Correct 478 ms 121020 KB Output is correct
29 Correct 488 ms 119996 KB Output is correct
30 Correct 204 ms 63560 KB Output is correct
31 Correct 238 ms 63568 KB Output is correct
32 Correct 336 ms 59340 KB Output is correct
33 Correct 387 ms 86088 KB Output is correct
34 Correct 419 ms 90788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 20992 KB Output is correct
2 Correct 4 ms 20984 KB Output is correct
3 Correct 4 ms 20816 KB Output is correct
4 Correct 4 ms 20816 KB Output is correct
5 Correct 4 ms 20816 KB Output is correct
6 Correct 4 ms 20816 KB Output is correct
7 Correct 5 ms 20816 KB Output is correct
8 Correct 6 ms 20816 KB Output is correct
9 Correct 5 ms 20816 KB Output is correct
10 Correct 5 ms 20816 KB Output is correct
11 Correct 4 ms 20816 KB Output is correct
12 Correct 4 ms 20816 KB Output is correct
13 Correct 4 ms 20816 KB Output is correct
14 Correct 5 ms 18768 KB Output is correct
15 Correct 4 ms 20816 KB Output is correct
16 Correct 5 ms 20816 KB Output is correct
17 Correct 4 ms 20816 KB Output is correct
18 Correct 5 ms 20816 KB Output is correct
19 Correct 3 ms 20816 KB Output is correct
20 Correct 3 ms 20816 KB Output is correct
21 Correct 5 ms 21240 KB Output is correct
22 Correct 5 ms 23120 KB Output is correct
23 Correct 7 ms 21072 KB Output is correct
24 Correct 6 ms 21072 KB Output is correct
25 Correct 6 ms 23120 KB Output is correct
26 Correct 5 ms 23120 KB Output is correct
27 Correct 8 ms 19540 KB Output is correct
28 Correct 11 ms 16208 KB Output is correct
29 Correct 6 ms 18256 KB Output is correct
30 Correct 7 ms 18680 KB Output is correct
31 Correct 6 ms 21072 KB Output is correct
32 Correct 7 ms 21072 KB Output is correct
33 Correct 9 ms 21248 KB Output is correct
34 Correct 5 ms 23120 KB Output is correct
35 Correct 5 ms 23120 KB Output is correct
36 Correct 7 ms 21072 KB Output is correct
37 Correct 5 ms 23120 KB Output is correct
38 Correct 7 ms 23120 KB Output is correct
39 Correct 221 ms 54344 KB Output is correct
40 Correct 224 ms 54148 KB Output is correct
41 Correct 236 ms 53804 KB Output is correct
42 Correct 176 ms 49716 KB Output is correct
43 Correct 158 ms 57396 KB Output is correct
44 Correct 95 ms 43064 KB Output is correct
45 Correct 214 ms 61772 KB Output is correct
46 Correct 156 ms 63928 KB Output is correct
47 Correct 242 ms 63412 KB Output is correct
48 Correct 478 ms 121020 KB Output is correct
49 Correct 488 ms 119996 KB Output is correct
50 Correct 204 ms 63560 KB Output is correct
51 Correct 238 ms 63568 KB Output is correct
52 Correct 336 ms 59340 KB Output is correct
53 Correct 387 ms 86088 KB Output is correct
54 Correct 419 ms 90788 KB Output is correct
55 Correct 12 ms 23120 KB Output is correct
56 Correct 14 ms 23360 KB Output is correct
57 Correct 126 ms 57940 KB Output is correct
58 Correct 35 ms 32036 KB Output is correct
59 Correct 171 ms 66128 KB Output is correct
60 Correct 544 ms 123920 KB Output is correct
61 Correct 228 ms 63816 KB Output is correct
62 Correct 250 ms 66120 KB Output is correct
63 Correct 297 ms 66120 KB Output is correct
64 Correct 472 ms 91988 KB Output is correct
65 Correct 404 ms 78904 KB Output is correct
66 Correct 572 ms 120196 KB Output is correct
67 Correct 107 ms 44836 KB Output is correct
68 Correct 272 ms 61836 KB Output is correct
69 Correct 246 ms 62536 KB Output is correct
70 Correct 220 ms 59720 KB Output is correct