Submission #497845

# Submission time Handle Problem Language Result Execution time Memory
497845 2021-12-24T00:59:24 Z perchuts Race (IOI11_race) C++17
100 / 100
521 ms 37828 KB
#include <bits/stdc++.h>
#define maxn (int)(2e5 + 51)
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define endl '\n'
#define ll long long
#define pb push_back
#define ull unsigned long long
#define ii pair<int, int>
#define iii tuple<int, int, int>
#define inf (int)(2e9 + 1)
#define mod (int)(1e9 + 7)

using namespace std;
vector<ii> g[maxn];
int subsz[maxn], vis[maxn];

ii cnt[5 * maxn];

int dfs(int v, int p)
{
  subsz[v] = 1;
  for (auto [u, d] : g[v])
  {
    if (u != p && !vis[u])
      subsz[v] += dfs(u, v);
  }
  return subsz[v];
}
int ans = inf, curcentroid;

int find_centroid(int u, int p, int n)
{
  for (auto [v, d] : g[u])
  {
    if (v != p && !vis[v] && subsz[v] > n / 2)
      return find_centroid(v, u, n);
  }
  return u;
}
void solve(int u, int p, int d, int k, int len, int mode)
{
  if (d > k)
    return;
  if (mode && (curcentroid != cnt[d].second || (curcentroid == cnt[d].second && cnt[d].first > len)))
    cnt[d] = {len, curcentroid};

  if (!mode && (cnt[k - d].second == curcentroid || k == d))
    ans = min(ans, cnt[k - d].first + len);

  for (auto [v, ed] : g[u])
    if(v!=p&&vis[v]==0)
        solve(v, u, d + ed, k, len + 1, mode);
}

void build(int v, int k)
{
  int centroid = find_centroid(v, -1, dfs(v, -1));
  vis[centroid] = 1;
  curcentroid = centroid;
  for (auto [u, d] : g[centroid])
  {
    if (!vis[u])
    {
      solve(u, -1, d, k, 1, 0);
      solve(u, -1, d, k, 1, 1);
    }
  }
  for (auto [u, d] : g[centroid])
    if (!vis[u])
      build(u, k);
}

int best_path(int N, int K, int H[maxn][2], int L[maxn])
{
  int n=N,k=K;
  for (int i = 0; i < n - 1; i++)
  {
    int u = H[i][0], v = H[i][1], d = L[i];
    g[u].pb({v, d});
    g[v].pb({u, d});
  }
  for (int i = 1; i <= k; i++)
    cnt[i] = {inf, -1};
  cnt[0]={0,-1};
  build(0, k);
  return (ans == inf ? -1 : ans);
}

// int h[maxn][2],l[maxn];
// int main(){
//   int n,k;cin>>n>>k;
//   for(int i=0;i<n-1;i++){
    
//     int a,b,x;cin>>a>>b>>x;
//     l[i]=x;
//     h[i][0]=a;
//     h[i][1]=b;
//   }
//   cout<<best_path(n,k,h,l)<<endl;
// }
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 5000 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 4 ms 5008 KB Output is correct
9 Correct 3 ms 4976 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4988 KB Output is correct
13 Correct 4 ms 5008 KB Output is correct
14 Correct 3 ms 4984 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 5000 KB Output is correct
17 Correct 3 ms 4992 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 5000 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 4 ms 5008 KB Output is correct
9 Correct 3 ms 4976 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4988 KB Output is correct
13 Correct 4 ms 5008 KB Output is correct
14 Correct 3 ms 4984 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 5000 KB Output is correct
17 Correct 3 ms 4992 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Correct 3 ms 5000 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 3 ms 5068 KB Output is correct
22 Correct 8 ms 12236 KB Output is correct
23 Correct 6 ms 10956 KB Output is correct
24 Correct 6 ms 11852 KB Output is correct
25 Correct 8 ms 11592 KB Output is correct
26 Correct 5 ms 7628 KB Output is correct
27 Correct 6 ms 11284 KB Output is correct
28 Correct 5 ms 6604 KB Output is correct
29 Correct 6 ms 7572 KB Output is correct
30 Correct 6 ms 7948 KB Output is correct
31 Correct 6 ms 10180 KB Output is correct
32 Correct 6 ms 10572 KB Output is correct
33 Correct 9 ms 11212 KB Output is correct
34 Correct 6 ms 9620 KB Output is correct
35 Correct 9 ms 11340 KB Output is correct
36 Correct 9 ms 12364 KB Output is correct
37 Correct 8 ms 11212 KB Output is correct
38 Correct 5 ms 9036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 5000 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 4 ms 5008 KB Output is correct
9 Correct 3 ms 4976 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4988 KB Output is correct
13 Correct 4 ms 5008 KB Output is correct
14 Correct 3 ms 4984 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 5000 KB Output is correct
17 Correct 3 ms 4992 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Correct 108 ms 11916 KB Output is correct
20 Correct 113 ms 11972 KB Output is correct
21 Correct 115 ms 11924 KB Output is correct
22 Correct 123 ms 12080 KB Output is correct
23 Correct 93 ms 12312 KB Output is correct
24 Correct 48 ms 12140 KB Output is correct
25 Correct 104 ms 14660 KB Output is correct
26 Correct 92 ms 17592 KB Output is correct
27 Correct 184 ms 19292 KB Output is correct
28 Correct 265 ms 30600 KB Output is correct
29 Correct 281 ms 29820 KB Output is correct
30 Correct 151 ms 19256 KB Output is correct
31 Correct 173 ms 19268 KB Output is correct
32 Correct 188 ms 19468 KB Output is correct
33 Correct 228 ms 18208 KB Output is correct
34 Correct 210 ms 19168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 5000 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 4 ms 5008 KB Output is correct
9 Correct 3 ms 4976 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4988 KB Output is correct
13 Correct 4 ms 5008 KB Output is correct
14 Correct 3 ms 4984 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 5000 KB Output is correct
17 Correct 3 ms 4992 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Correct 3 ms 5000 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 3 ms 5068 KB Output is correct
22 Correct 8 ms 12236 KB Output is correct
23 Correct 6 ms 10956 KB Output is correct
24 Correct 6 ms 11852 KB Output is correct
25 Correct 8 ms 11592 KB Output is correct
26 Correct 5 ms 7628 KB Output is correct
27 Correct 6 ms 11284 KB Output is correct
28 Correct 5 ms 6604 KB Output is correct
29 Correct 6 ms 7572 KB Output is correct
30 Correct 6 ms 7948 KB Output is correct
31 Correct 6 ms 10180 KB Output is correct
32 Correct 6 ms 10572 KB Output is correct
33 Correct 9 ms 11212 KB Output is correct
34 Correct 6 ms 9620 KB Output is correct
35 Correct 9 ms 11340 KB Output is correct
36 Correct 9 ms 12364 KB Output is correct
37 Correct 8 ms 11212 KB Output is correct
38 Correct 5 ms 9036 KB Output is correct
39 Correct 108 ms 11916 KB Output is correct
40 Correct 113 ms 11972 KB Output is correct
41 Correct 115 ms 11924 KB Output is correct
42 Correct 123 ms 12080 KB Output is correct
43 Correct 93 ms 12312 KB Output is correct
44 Correct 48 ms 12140 KB Output is correct
45 Correct 104 ms 14660 KB Output is correct
46 Correct 92 ms 17592 KB Output is correct
47 Correct 184 ms 19292 KB Output is correct
48 Correct 265 ms 30600 KB Output is correct
49 Correct 281 ms 29820 KB Output is correct
50 Correct 151 ms 19256 KB Output is correct
51 Correct 173 ms 19268 KB Output is correct
52 Correct 188 ms 19468 KB Output is correct
53 Correct 228 ms 18208 KB Output is correct
54 Correct 210 ms 19168 KB Output is correct
55 Correct 10 ms 5652 KB Output is correct
56 Correct 10 ms 5644 KB Output is correct
57 Correct 74 ms 12176 KB Output is correct
58 Correct 34 ms 11864 KB Output is correct
59 Correct 87 ms 19056 KB Output is correct
60 Correct 515 ms 37828 KB Output is correct
61 Correct 159 ms 19524 KB Output is correct
62 Correct 235 ms 27204 KB Output is correct
63 Correct 258 ms 27204 KB Output is correct
64 Correct 521 ms 23748 KB Output is correct
65 Correct 230 ms 20332 KB Output is correct
66 Correct 403 ms 35684 KB Output is correct
67 Correct 148 ms 27864 KB Output is correct
68 Correct 252 ms 27844 KB Output is correct
69 Correct 250 ms 28100 KB Output is correct
70 Correct 259 ms 27348 KB Output is correct