Submission #1236077

#TimeUsernameProblemLanguageResultExecution timeMemory
1236077htoshiroRace (IOI11_race)C++20
Compilation error
0 ms0 KiB
#include <bits/stdc++.h>
#define int long long
#pragma GCC optimize("Ofast,unroll-loops")
using namespace std;
struct pi {
    int x, y;
};
vector<vector<pi>> adj;
vector<int> sz;
vector<bool> blocked;
vector<int> nxt;
int n, k;
int ans;
int dfs(int cur, int par) {
    sz[cur] = 1;
    for (auto &a : adj[cur]) {
        if (a.x != par && !blocked[a.x]) {
            sz[cur] += dfs(a.x, cur);
        }
    }
    return sz[cur];
}
int cent(int cur, int par, int am) {
    for (auto &a : adj[cur]) {
        if (a.x != par && !blocked[a.x] && sz[a.x] > am / 2) {
            return cent(a.x, cur, am);
        }
    }
    return cur;
}
void dfs2(int cur, int par, int dist, int depth, vector<pair<int, int>> &paths) {
    if (dist > k) return;
    paths.emplace_back(dist, depth);
    for (auto &a : adj[cur]) {
        if (a.x != par && !blocked[a.x]) {
            dfs2(a.x, cur, dist + a.y, depth + 1, paths);
        }
    }
}
int build(int cur, int par) {
    int total = dfs(cur, -1);
    int curc = cent(cur, -1, total);
    blocked[curc] = true;
    nxt[curc] = par;
    unordered_map<int, int> dist_count;
    dist_count[0] = 0; 
    int best = LLONG_MAX;
    for (auto &a : adj[curc]) {
        if (!blocked[a.x]) {
            vector<pair<int, int>> paths;
            dfs2(a.x, curc, a.y, 1, paths);
            for (auto &[d, l] : paths) {
                if (dist_count.count(k - d)) {
                    best = min(best, l + dist_count[k - d]);
                }
            }
            for (auto &[d, l] : paths) {
                if (!dist_count.count(d)) dist_count[d] = l;
                else dist_count[d] = min(dist_count[d], l);
            }
        }
    }
    for (auto &a : adj[curc]) {
        if (!blocked[a.x]) {
            best = min(best, build(a.x, curc));
        }
    }
    return best;
}
int best_path(int N, int K, int H[][2], int L[]){
    n = N;
    k = K;
    adj.resize(n);
    sz.resize(n);
    nxt.resize(n);
    blocked.assign(N, false);
    for (int i = 0; i < N-1; i++) {
        adj[H[i][0]].push_back({H[i][1], L[i]});
        adj[H[i][1]].push_back({H[i][0], L[i]});
    }
    ans = build(0, -1);
    return (ans == LLONG_MAX ? -1 : ans);
}
signed main(){
  int N,K;  
  scanf("%d %d", &N, &K);
  int H[N][2];
  int L[N];
  for (int i = 0; i < N - 1; i++)scanf("%d %d %d", &H[i][0], &H[i][1], &L[i]);
  int fin = best_path(N, K, H, L);
  printf("%d\n", fin);
  return 0;
}

Compilation message (stderr)

race.cpp: In function 'int main()':
race.cpp:86:11: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'long long int*' [-Wformat=]
   86 |   scanf("%d %d", &N, &K);
      |          ~^      ~~
      |           |      |
      |           int*   long long int*
      |          %lld
race.cpp:86:14: warning: format '%d' expects argument of type 'int*', but argument 3 has type 'long long int*' [-Wformat=]
   86 |   scanf("%d %d", &N, &K);
      |             ~^       ~~
      |              |       |
      |              int*    long long int*
      |             %lld
race.cpp:89:42: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'long long int*' [-Wformat=]
   89 |   for (int i = 0; i < N - 1; i++)scanf("%d %d %d", &H[i][0], &H[i][1], &L[i]);
      |                                         ~^         ~~~~~~~~
      |                                          |         |
      |                                          int*      long long int*
      |                                         %lld
race.cpp:89:45: warning: format '%d' expects argument of type 'int*', but argument 3 has type 'long long int*' [-Wformat=]
   89 |   for (int i = 0; i < N - 1; i++)scanf("%d %d %d", &H[i][0], &H[i][1], &L[i]);
      |                                            ~^                ~~~~~~~~
      |                                             |                |
      |                                             int*             long long int*
      |                                            %lld
race.cpp:89:48: warning: format '%d' expects argument of type 'int*', but argument 4 has type 'long long int*' [-Wformat=]
   89 |   for (int i = 0; i < N - 1; i++)scanf("%d %d %d", &H[i][0], &H[i][1], &L[i]);
      |                                               ~^                       ~~~~~
      |                                                |                       |
      |                                                int*                    long long int*
      |                                               %lld
race.cpp:91:12: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long long int' [-Wformat=]
   91 |   printf("%d\n", fin);
      |           ~^     ~~~
      |            |     |
      |            int   long long int
      |           %lld
race.cpp:86:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |   scanf("%d %d", &N, &K);
      |   ~~~~~^~~~~~~~~~~~~~~~~
race.cpp:89:39: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   89 |   for (int i = 0; i < N - 1; i++)scanf("%d %d %d", &H[i][0], &H[i][1], &L[i]);
      |                                  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/ld: /tmp/ccG8lGRi.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/cclskFP1.o:race.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/ccG8lGRi.o: in function `main':
grader.cpp:(.text.startup+0x28): undefined reference to `best_path(int, int, int (*) [2], int*)'
collect2: error: ld returned 1 exit status