답안 #714065

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
714065 2023-03-23T19:18:23 Z speedyArda 경주 (Race) (IOI11_race) C++14
100 / 100
551 ms 40364 KB
#include "race.h"
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const int MAXN = 2e5+5;
vector< vector< pair<ll, int> > > adj(MAXN);
ll ans = -1;
int sz[MAXN];
int par_cent[MAXN];
ll cnt[(int)(1e6+5)];
bool visited[MAXN];
int k;
//int cent;
inline void minimize(ll &a, ll b)
{
    if(a == -1 || a > b)
        a = b;
}

int dfs_sz(int v, int p)
{
  sz[v] = 1;
  for(pair<ll, int> e : adj[v])
  {
    if(e.second == p || visited[e.second])
      continue;
    int tmp = dfs_sz(e.second, v);
    sz[v] += tmp;

  }

  return sz[v];
}

int dfs_cent(int v, int p, int n)
{
    //cout << v << "\n";
    for(pair<ll, int> e : adj[v])
    {
        if(e.second == p || visited[e.second])
          continue;
        if(sz[e.second] * 2 > n)
          return dfs_cent(e.second, v, n);
    }

    return v;
}

void get_cnt(int sum, bool filling, int u, int p = -1, int depth = 1)
{
    if(sum > k) return;
    if(filling)
        minimize(cnt[sum], depth);
    else {
        if(cnt[k - sum] != -1) {
            minimize(ans, depth + cnt[k - sum]);
            //cout << ans << "\n";
        }
    }
    for(auto [C, v]: adj[u]) {
        if(v != p && !visited[v]) get_cnt(sum + C, filling, v, u, depth + 1);
    }
}

void del(int sum, int u, int p = -1)
{
    if(sum > k) return;
    cnt[sum] = -1;
    for(auto [C, v]: adj[u]) {
        if(v != p && !visited[v])
            del(sum + C, v, u);
    }
}

void centroid(int v, int p)
{
    int n = dfs_sz(v, p);
    int cent = dfs_cent(v, p, n);
    //cout << cent << "\n";
    visited[cent] = true;
    cnt[0] = 0;
    for(auto [C, v]: adj[cent]) {
      if(!visited[v]) {
          get_cnt(C, false, v);
          get_cnt(C, true, v);
      }
    }

    for(auto [C,v] : adj[cent])
    {
        if(!visited[v])
        {
          del(C, v);
        }
    }
    for(auto [C, v] : adj[cent])
    {
      if(!visited[v])
        centroid(v, cent);
    }
}

int best_path(int N, int K, int H[][2], int L[])
{
  k = K;
  for(int i = 0; i < N - 1; i++)
  {
    adj[H[i][0]].push_back({L[i], H[i][1]});
    adj[H[i][1]].push_back({L[i], H[i][0]});
  }

  memset(cnt, -1, sizeof(cnt));

  centroid(0, -1);

  return ans;
}

Compilation message

race.cpp: In function 'void get_cnt(int, bool, int, int, int)':
race.cpp:60:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   60 |     for(auto [C, v]: adj[u]) {
      |              ^
race.cpp: In function 'void del(int, int, int)':
race.cpp:69:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   69 |     for(auto [C, v]: adj[u]) {
      |              ^
race.cpp: In function 'void centroid(int, int)':
race.cpp:82:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   82 |     for(auto [C, v]: adj[cent]) {
      |              ^
race.cpp:89:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   89 |     for(auto [C,v] : adj[cent])
      |              ^
race.cpp:96:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   96 |     for(auto [C, v] : adj[cent])
      |              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12756 KB Output is correct
2 Correct 10 ms 12844 KB Output is correct
3 Correct 7 ms 12756 KB Output is correct
4 Correct 7 ms 12756 KB Output is correct
5 Correct 8 ms 12756 KB Output is correct
6 Correct 8 ms 12756 KB Output is correct
7 Correct 8 ms 12756 KB Output is correct
8 Correct 10 ms 12756 KB Output is correct
9 Correct 10 ms 12844 KB Output is correct
10 Correct 7 ms 12860 KB Output is correct
11 Correct 8 ms 12756 KB Output is correct
12 Correct 7 ms 12756 KB Output is correct
13 Correct 7 ms 12756 KB Output is correct
14 Correct 9 ms 12756 KB Output is correct
15 Correct 7 ms 12860 KB Output is correct
16 Correct 7 ms 12756 KB Output is correct
17 Correct 10 ms 12756 KB Output is correct
18 Correct 7 ms 12756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12756 KB Output is correct
2 Correct 10 ms 12844 KB Output is correct
3 Correct 7 ms 12756 KB Output is correct
4 Correct 7 ms 12756 KB Output is correct
5 Correct 8 ms 12756 KB Output is correct
6 Correct 8 ms 12756 KB Output is correct
7 Correct 8 ms 12756 KB Output is correct
8 Correct 10 ms 12756 KB Output is correct
9 Correct 10 ms 12844 KB Output is correct
10 Correct 7 ms 12860 KB Output is correct
11 Correct 8 ms 12756 KB Output is correct
12 Correct 7 ms 12756 KB Output is correct
13 Correct 7 ms 12756 KB Output is correct
14 Correct 9 ms 12756 KB Output is correct
15 Correct 7 ms 12860 KB Output is correct
16 Correct 7 ms 12756 KB Output is correct
17 Correct 10 ms 12756 KB Output is correct
18 Correct 7 ms 12756 KB Output is correct
19 Correct 9 ms 12756 KB Output is correct
20 Correct 9 ms 12756 KB Output is correct
21 Correct 9 ms 12884 KB Output is correct
22 Correct 7 ms 12884 KB Output is correct
23 Correct 8 ms 12836 KB Output is correct
24 Correct 7 ms 12836 KB Output is correct
25 Correct 7 ms 12884 KB Output is correct
26 Correct 8 ms 12884 KB Output is correct
27 Correct 7 ms 12884 KB Output is correct
28 Correct 7 ms 12840 KB Output is correct
29 Correct 7 ms 12884 KB Output is correct
30 Correct 7 ms 12884 KB Output is correct
31 Correct 7 ms 12844 KB Output is correct
32 Correct 7 ms 12884 KB Output is correct
33 Correct 7 ms 12840 KB Output is correct
34 Correct 7 ms 12912 KB Output is correct
35 Correct 7 ms 12884 KB Output is correct
36 Correct 7 ms 12840 KB Output is correct
37 Correct 7 ms 12884 KB Output is correct
38 Correct 7 ms 12872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12756 KB Output is correct
2 Correct 10 ms 12844 KB Output is correct
3 Correct 7 ms 12756 KB Output is correct
4 Correct 7 ms 12756 KB Output is correct
5 Correct 8 ms 12756 KB Output is correct
6 Correct 8 ms 12756 KB Output is correct
7 Correct 8 ms 12756 KB Output is correct
8 Correct 10 ms 12756 KB Output is correct
9 Correct 10 ms 12844 KB Output is correct
10 Correct 7 ms 12860 KB Output is correct
11 Correct 8 ms 12756 KB Output is correct
12 Correct 7 ms 12756 KB Output is correct
13 Correct 7 ms 12756 KB Output is correct
14 Correct 9 ms 12756 KB Output is correct
15 Correct 7 ms 12860 KB Output is correct
16 Correct 7 ms 12756 KB Output is correct
17 Correct 10 ms 12756 KB Output is correct
18 Correct 7 ms 12756 KB Output is correct
19 Correct 140 ms 19628 KB Output is correct
20 Correct 129 ms 19552 KB Output is correct
21 Correct 196 ms 19632 KB Output is correct
22 Correct 139 ms 19644 KB Output is correct
23 Correct 69 ms 19988 KB Output is correct
24 Correct 53 ms 19128 KB Output is correct
25 Correct 119 ms 22440 KB Output is correct
26 Correct 99 ms 25396 KB Output is correct
27 Correct 163 ms 26876 KB Output is correct
28 Correct 271 ms 38020 KB Output is correct
29 Correct 240 ms 37112 KB Output is correct
30 Correct 161 ms 26856 KB Output is correct
31 Correct 159 ms 26808 KB Output is correct
32 Correct 189 ms 26908 KB Output is correct
33 Correct 215 ms 25616 KB Output is correct
34 Correct 186 ms 25648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12756 KB Output is correct
2 Correct 10 ms 12844 KB Output is correct
3 Correct 7 ms 12756 KB Output is correct
4 Correct 7 ms 12756 KB Output is correct
5 Correct 8 ms 12756 KB Output is correct
6 Correct 8 ms 12756 KB Output is correct
7 Correct 8 ms 12756 KB Output is correct
8 Correct 10 ms 12756 KB Output is correct
9 Correct 10 ms 12844 KB Output is correct
10 Correct 7 ms 12860 KB Output is correct
11 Correct 8 ms 12756 KB Output is correct
12 Correct 7 ms 12756 KB Output is correct
13 Correct 7 ms 12756 KB Output is correct
14 Correct 9 ms 12756 KB Output is correct
15 Correct 7 ms 12860 KB Output is correct
16 Correct 7 ms 12756 KB Output is correct
17 Correct 10 ms 12756 KB Output is correct
18 Correct 7 ms 12756 KB Output is correct
19 Correct 9 ms 12756 KB Output is correct
20 Correct 9 ms 12756 KB Output is correct
21 Correct 9 ms 12884 KB Output is correct
22 Correct 7 ms 12884 KB Output is correct
23 Correct 8 ms 12836 KB Output is correct
24 Correct 7 ms 12836 KB Output is correct
25 Correct 7 ms 12884 KB Output is correct
26 Correct 8 ms 12884 KB Output is correct
27 Correct 7 ms 12884 KB Output is correct
28 Correct 7 ms 12840 KB Output is correct
29 Correct 7 ms 12884 KB Output is correct
30 Correct 7 ms 12884 KB Output is correct
31 Correct 7 ms 12844 KB Output is correct
32 Correct 7 ms 12884 KB Output is correct
33 Correct 7 ms 12840 KB Output is correct
34 Correct 7 ms 12912 KB Output is correct
35 Correct 7 ms 12884 KB Output is correct
36 Correct 7 ms 12840 KB Output is correct
37 Correct 7 ms 12884 KB Output is correct
38 Correct 7 ms 12872 KB Output is correct
39 Correct 140 ms 19628 KB Output is correct
40 Correct 129 ms 19552 KB Output is correct
41 Correct 196 ms 19632 KB Output is correct
42 Correct 139 ms 19644 KB Output is correct
43 Correct 69 ms 19988 KB Output is correct
44 Correct 53 ms 19128 KB Output is correct
45 Correct 119 ms 22440 KB Output is correct
46 Correct 99 ms 25396 KB Output is correct
47 Correct 163 ms 26876 KB Output is correct
48 Correct 271 ms 38020 KB Output is correct
49 Correct 240 ms 37112 KB Output is correct
50 Correct 161 ms 26856 KB Output is correct
51 Correct 159 ms 26808 KB Output is correct
52 Correct 189 ms 26908 KB Output is correct
53 Correct 215 ms 25616 KB Output is correct
54 Correct 186 ms 25648 KB Output is correct
55 Correct 15 ms 13568 KB Output is correct
56 Correct 16 ms 13668 KB Output is correct
57 Correct 87 ms 21328 KB Output is correct
58 Correct 38 ms 20276 KB Output is correct
59 Correct 106 ms 26712 KB Output is correct
60 Correct 514 ms 40364 KB Output is correct
61 Correct 178 ms 29948 KB Output is correct
62 Correct 197 ms 29876 KB Output is correct
63 Correct 271 ms 29892 KB Output is correct
64 Correct 551 ms 29820 KB Output is correct
65 Correct 228 ms 30260 KB Output is correct
66 Correct 406 ms 38252 KB Output is correct
67 Correct 128 ms 28948 KB Output is correct
68 Correct 283 ms 30056 KB Output is correct
69 Correct 248 ms 29872 KB Output is correct
70 Correct 241 ms 29276 KB Output is correct