Submission #1030147

# Submission time Handle Problem Language Result Execution time Memory
1030147 2024-07-21T21:23:19 Z kl0989e Race (IOI11_race) C++17
100 / 100
639 ms 55380 KB
#include <bits/stdc++.h>
#include "race.h"
using namespace std;

#define ll long long
#define fi first
#define se second
#define pb push_back
#define vi vector<int>
#define vl vector<ll>
#define pi pair<int, int>
#define pl pair<ll,ll>
#define all(x) (x).begin(),(x).end()

const int maxn=2e5+10;
vector<vector<pi>> graph(maxn);
vi sze(maxn);
vi cen_num(maxn,-1);
int ans=1e9;
int needed;
map<int,int> added;
map<int,int> now;

void dfs2(int cur, int prev, int len, int dist) {
    if (now.find(len)==now.end() || now[len]>dist) {
        if (len==needed) {
            ans=min(ans,dist);
        }
        else {
            now[len]=dist;
        }
    }
    for (auto [to,w]:graph[cur]) {
        if (to==prev || len+w>needed || cen_num[to]!=-1) {
            continue;
        }
        dfs2(to,cur,len+w,dist+1);
    }
}

void dfs(int cur, int prev, int num, int cur_sze) {
    sze[cur]=1;
    bool is_cen=1;
    for (auto [to,_]:graph[cur]) {
        if (to==prev || cen_num[to]!=-1) {
            continue;
        }
        dfs(to,cur,num,cur_sze);
        if (cen_num[cur]!=-1) {
            return;
        }
        sze[cur]+=sze[to];
        if (sze[to]>cur_sze/2) {
            is_cen=0;
        }
    }
    if (!is_cen || cur_sze-sze[cur]>cur_sze/2) {
        return;
    }
    cen_num[cur]=num;
    for (auto [to,w]:graph[cur]) {
        if (cen_num[to]!=-1 || w>needed) {
            continue;
        }
        dfs2(to,cur,w,1);
        if (now.size()>added.size()) {
            swap(now,added);
        }
        for (auto [a,b]:now) {
            if (added.find(needed-a)!=added.end() && added[needed-a]+b<ans) {
                ans=added[needed-a]+b;
            }
        }
        for (auto [a,b]:now) {
            if (added.find(a)==added.end() || added[a]>b) {
                added[a]=b;
            }
        }
        now.clear();
    }
    added.clear();
    for (auto [to,_]:graph[cur]) {
        if (cen_num[to]!=-1) {
            continue;
        }
        if (prev==to) {
            dfs(to,cur,num+1,cur_sze-sze[cur]);
        }
        else {
            dfs(to,cur,num+1,sze[to]);
        }
    }
}

int best_path(int n, int k, int edge[][2], int len[]) {
    for (int i=0; i<n-1; i++) {
        graph[edge[i][0]].pb({edge[i][1],len[i]});
        graph[edge[i][1]].pb({edge[i][0],len[i]});
    }
    needed=k;
    dfs(0,-1,0,n);
    return (ans==1e9?-1:ans);
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6748 KB Output is correct
2 Correct 3 ms 6492 KB Output is correct
3 Correct 3 ms 6492 KB Output is correct
4 Correct 3 ms 6492 KB Output is correct
5 Correct 3 ms 6492 KB Output is correct
6 Correct 3 ms 6600 KB Output is correct
7 Correct 3 ms 6492 KB Output is correct
8 Correct 3 ms 6748 KB Output is correct
9 Correct 3 ms 6492 KB Output is correct
10 Correct 3 ms 6492 KB Output is correct
11 Correct 3 ms 6492 KB Output is correct
12 Correct 3 ms 6492 KB Output is correct
13 Correct 3 ms 6492 KB Output is correct
14 Correct 3 ms 6748 KB Output is correct
15 Correct 3 ms 6492 KB Output is correct
16 Correct 3 ms 6744 KB Output is correct
17 Correct 3 ms 6748 KB Output is correct
18 Correct 3 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6748 KB Output is correct
2 Correct 3 ms 6492 KB Output is correct
3 Correct 3 ms 6492 KB Output is correct
4 Correct 3 ms 6492 KB Output is correct
5 Correct 3 ms 6492 KB Output is correct
6 Correct 3 ms 6600 KB Output is correct
7 Correct 3 ms 6492 KB Output is correct
8 Correct 3 ms 6748 KB Output is correct
9 Correct 3 ms 6492 KB Output is correct
10 Correct 3 ms 6492 KB Output is correct
11 Correct 3 ms 6492 KB Output is correct
12 Correct 3 ms 6492 KB Output is correct
13 Correct 3 ms 6492 KB Output is correct
14 Correct 3 ms 6748 KB Output is correct
15 Correct 3 ms 6492 KB Output is correct
16 Correct 3 ms 6744 KB Output is correct
17 Correct 3 ms 6748 KB Output is correct
18 Correct 3 ms 6492 KB Output is correct
19 Correct 3 ms 6488 KB Output is correct
20 Correct 3 ms 6636 KB Output is correct
21 Correct 4 ms 6748 KB Output is correct
22 Correct 3 ms 6744 KB Output is correct
23 Correct 3 ms 6748 KB Output is correct
24 Correct 3 ms 6748 KB Output is correct
25 Correct 4 ms 6748 KB Output is correct
26 Correct 3 ms 6748 KB Output is correct
27 Correct 3 ms 6604 KB Output is correct
28 Correct 4 ms 6736 KB Output is correct
29 Correct 4 ms 6748 KB Output is correct
30 Correct 4 ms 6660 KB Output is correct
31 Correct 5 ms 6748 KB Output is correct
32 Correct 4 ms 6744 KB Output is correct
33 Correct 4 ms 6748 KB Output is correct
34 Correct 3 ms 6748 KB Output is correct
35 Correct 3 ms 6764 KB Output is correct
36 Correct 4 ms 6748 KB Output is correct
37 Correct 4 ms 6748 KB Output is correct
38 Correct 4 ms 6748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6748 KB Output is correct
2 Correct 3 ms 6492 KB Output is correct
3 Correct 3 ms 6492 KB Output is correct
4 Correct 3 ms 6492 KB Output is correct
5 Correct 3 ms 6492 KB Output is correct
6 Correct 3 ms 6600 KB Output is correct
7 Correct 3 ms 6492 KB Output is correct
8 Correct 3 ms 6748 KB Output is correct
9 Correct 3 ms 6492 KB Output is correct
10 Correct 3 ms 6492 KB Output is correct
11 Correct 3 ms 6492 KB Output is correct
12 Correct 3 ms 6492 KB Output is correct
13 Correct 3 ms 6492 KB Output is correct
14 Correct 3 ms 6748 KB Output is correct
15 Correct 3 ms 6492 KB Output is correct
16 Correct 3 ms 6744 KB Output is correct
17 Correct 3 ms 6748 KB Output is correct
18 Correct 3 ms 6492 KB Output is correct
19 Correct 122 ms 11604 KB Output is correct
20 Correct 118 ms 12120 KB Output is correct
21 Correct 120 ms 12084 KB Output is correct
22 Correct 118 ms 12628 KB Output is correct
23 Correct 59 ms 12632 KB Output is correct
24 Correct 53 ms 12632 KB Output is correct
25 Correct 80 ms 19080 KB Output is correct
26 Correct 65 ms 25428 KB Output is correct
27 Correct 113 ms 17692 KB Output is correct
28 Correct 161 ms 44368 KB Output is correct
29 Correct 167 ms 42544 KB Output is correct
30 Correct 107 ms 17828 KB Output is correct
31 Correct 109 ms 17748 KB Output is correct
32 Correct 115 ms 17968 KB Output is correct
33 Correct 156 ms 16980 KB Output is correct
34 Correct 106 ms 17144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6748 KB Output is correct
2 Correct 3 ms 6492 KB Output is correct
3 Correct 3 ms 6492 KB Output is correct
4 Correct 3 ms 6492 KB Output is correct
5 Correct 3 ms 6492 KB Output is correct
6 Correct 3 ms 6600 KB Output is correct
7 Correct 3 ms 6492 KB Output is correct
8 Correct 3 ms 6748 KB Output is correct
9 Correct 3 ms 6492 KB Output is correct
10 Correct 3 ms 6492 KB Output is correct
11 Correct 3 ms 6492 KB Output is correct
12 Correct 3 ms 6492 KB Output is correct
13 Correct 3 ms 6492 KB Output is correct
14 Correct 3 ms 6748 KB Output is correct
15 Correct 3 ms 6492 KB Output is correct
16 Correct 3 ms 6744 KB Output is correct
17 Correct 3 ms 6748 KB Output is correct
18 Correct 3 ms 6492 KB Output is correct
19 Correct 3 ms 6488 KB Output is correct
20 Correct 3 ms 6636 KB Output is correct
21 Correct 4 ms 6748 KB Output is correct
22 Correct 3 ms 6744 KB Output is correct
23 Correct 3 ms 6748 KB Output is correct
24 Correct 3 ms 6748 KB Output is correct
25 Correct 4 ms 6748 KB Output is correct
26 Correct 3 ms 6748 KB Output is correct
27 Correct 3 ms 6604 KB Output is correct
28 Correct 4 ms 6736 KB Output is correct
29 Correct 4 ms 6748 KB Output is correct
30 Correct 4 ms 6660 KB Output is correct
31 Correct 5 ms 6748 KB Output is correct
32 Correct 4 ms 6744 KB Output is correct
33 Correct 4 ms 6748 KB Output is correct
34 Correct 3 ms 6748 KB Output is correct
35 Correct 3 ms 6764 KB Output is correct
36 Correct 4 ms 6748 KB Output is correct
37 Correct 4 ms 6748 KB Output is correct
38 Correct 4 ms 6748 KB Output is correct
39 Correct 122 ms 11604 KB Output is correct
40 Correct 118 ms 12120 KB Output is correct
41 Correct 120 ms 12084 KB Output is correct
42 Correct 118 ms 12628 KB Output is correct
43 Correct 59 ms 12632 KB Output is correct
44 Correct 53 ms 12632 KB Output is correct
45 Correct 80 ms 19080 KB Output is correct
46 Correct 65 ms 25428 KB Output is correct
47 Correct 113 ms 17692 KB Output is correct
48 Correct 161 ms 44368 KB Output is correct
49 Correct 167 ms 42544 KB Output is correct
50 Correct 107 ms 17828 KB Output is correct
51 Correct 109 ms 17748 KB Output is correct
52 Correct 115 ms 17968 KB Output is correct
53 Correct 156 ms 16980 KB Output is correct
54 Correct 106 ms 17144 KB Output is correct
55 Correct 15 ms 7260 KB Output is correct
56 Correct 10 ms 7004 KB Output is correct
57 Correct 79 ms 12000 KB Output is correct
58 Correct 30 ms 12488 KB Output is correct
59 Correct 237 ms 30972 KB Output is correct
60 Correct 631 ms 55380 KB Output is correct
61 Correct 167 ms 18260 KB Output is correct
62 Correct 184 ms 18764 KB Output is correct
63 Correct 191 ms 18860 KB Output is correct
64 Correct 639 ms 23888 KB Output is correct
65 Correct 118 ms 19536 KB Output is correct
66 Correct 406 ms 36892 KB Output is correct
67 Correct 78 ms 20160 KB Output is correct
68 Correct 283 ms 22828 KB Output is correct
69 Correct 271 ms 20564 KB Output is correct
70 Correct 242 ms 22612 KB Output is correct