제출 #848662

#제출 시각아이디문제언어결과실행 시간메모리
848662efedmrlr경주 (Race) (IOI11_race)C++17
컴파일 에러
0 ms0 KiB
#include <bits/stdc++.h>
#include "race.h"
using namespace std;
#define int long long int
#define MP make_pair
#define pb push_back
#define REP(i,n) for(int (i) = 0; (i) < (n); (i)++)


const double EPS = 0.00001;
const int INF = 1e9+500;
const int ALPH = 26;
const int MOD = 1e9+7;
const int M = 1e6+5;
int n,m,q, k;
array<int,2> cnt[M];
int ans = INF;
vector<int> vis, subt;
vector<vector<array<int,2> > > adj;
vector<int> centers;
int curc = 0;
inline void subtdfs(int node, int par) {
  subt[node] = 1;
  for(auto c : adj[node]) {
    if(c[0] == par) continue;
    if(vis[c[0]]) continue;
    subtdfs(c[0], node);
    subt[node] += subt[c[0]];
  }
  

}
inline int cdfs(int node, int par, int sz) {
  for(auto c : adj[node]) {
    if(c[0] == par) continue;
    if(vis[c[0]]) continue;
    if(subt[c[0]] > sz/2) {
      return cdfs(c[0], node, sz);
    }
  }
  return node;
}

inline void find_centroids() {
  subtdfs(1, 0);
  int tmp = cdfs(1, 0, subt[1]);
  centers.pb(tmp);
  int ind = 0;
  vis[tmp] = 1;
  centers.pb(-1);
  while(ind < centers.size()-1) {
    if(centers[ind] == -1) {
      centers.pb(-1);
      ind++;
      continue;
    }  
    for(auto c : adj[centers[ind]]) {
        if(vis[c[0]]) continue;
        subtdfs(c[0], 0);
        if(subt[c[0]] < 2) continue;
        tmp = cdfs(c[0], 0, subt[c[0]]);
        vis[tmp] = 1;
        centers.pb(tmp);
      }
      ind++;
    
  }

}

inline void dfs(int node ,int par, int cost, int leng) {
  if(leng > k) return;
  if(cnt[leng][1] != curc) {
    cnt[leng] = {cost, curc};
  }
  cnt[leng][0] = min(cnt[leng][0], cost);
  for(auto c : adj[node]) {
    if(c[0] == par) continue;
    if(vis[c[0]]) continue;
    dfs(c[0], node, cost + 1, leng + c[1]);
  }

}
inline void dfs2(int node, int par, int cost, int leng) {
  if(leng > k) return;
  if(cnt[k - leng][1] == curc) {
    ans = min(ans ,cnt[k - leng][0] + cost);
  }
  for(auto c : adj[node]) {
    if(c[0] == par) continue;
    if(vis[c[0]]) continue;
    dfs2(c[0], node, cost + 1, leng + c[1]);
  }
}

inline void find_paths() {
  for(int j = 0; j<k+1; j++) {
    cnt[j][0] = INF;
    cnt[j][1] = 0;
  }
  vis.clear();
  vis.resize(n+1);
  for(auto i : centers) {
    if(i == -1) {
      
      continue;
    }
    cnt[0][0] = 0; cnt[0][1] = i;
    curc = i;
    vis[i] = 1;
    for(auto c : adj[i]) {
      dfs2(c[0], i, 1, c[1]);
      dfs(c[0], i, 1, c[1]);
    }  

  }

}

int best_path(int N, int K, int H[][2], int L[])
{
  n = N;
  adj.resize(n+1); subt.resize(n+1); vis.resize(n+1,0);
  k = K;
  for(int i = 0; i<n-1; i++) {
    H[i][0]++;
    H[i][1]++;
    adj[H[i][0]].pb({H[i][1], L[i]});
    adj[H[i][1]].pb({H[i][0], L[i]});
  }
  find_centroids();
  find_paths();
  if(ans == INF) ans = -1;
  return ans;
}

컴파일 시 표준 에러 (stderr) 메시지

race.cpp: In function 'void find_centroids()':
race.cpp:51:13: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |   while(ind < centers.size()-1) {
      |         ~~~~^~~~~~~~~~~~~~~~~~
/usr/bin/ld: /tmp/ccNFsYhB.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