제출 #493817

#제출 시각아이디문제언어결과실행 시간메모리
493817Marceantasy경주 (Race) (IOI11_race)C++17
컴파일 에러
0 ms0 KiB
#include "race.h"
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define int long long 
#define ar array

const int mxN = 2e5+5, M = 1e9+7;
int k, ans = 1e9, sz[mxN];
vector<pair<ll, ll>> adj[mxN];

// getting the size of our current tree
void dfs_size(int u, int p){
    sz[u] = 1;
    for(pair<int, int> v : adj[u]){
        if(v.first != p){
            dfs_size(v.first, u);
            sz[u] += sz[v.first];
        }
    }
}

// traversing subtrees without the centroid and check for possible answers
void dfs_get(map<int, int>& mp, int u, int p, int cnt, int len){
    if(cnt == k){
        ans = min(ans, len);
        return;
    }else{
        if(mp[cnt] == 0){
            mp[cnt] = len;
        }else{
            mp[cnt] = min(mp[cnt], len);
        }
    }
    if(mp[k-cnt] > 0){
        ans = min(ans, mp[k-cnt]+mp[cnt]);
    }

    for(pair<int, int> v : adj[u]){
        if(v.first != p){
            dfs_get(mp, v.first, u, cnt+v.second, len+1);
        }
    }
}

//finding the centroid
int dfs_find(int u, int p, int total_size){
    for(pair<int, int> v : adj[u]){
        if(v.first != p && sz[v.first] >= total_size/2) return dfs_find(v.first, u, total_size);
    }
    return u;
}

// get the size of the current tree, find its centroid and then check for possible answers considering
// that the centroid is in the answer path, then, check for the case when its not, deleting the centroid 
// and doing the same process for the centroid adjacent's trees

void find_centroid(int u = 0, int p = -1){
    memset(sz, 0, sizeof(sz));
    dfs_size(u, p);
    int total_size = sz[u];
    int C = dfs_find(u, p, total_size);
    map<int, int> mp;
    for(pair<int, int> v : adj[C]){
        dfs_get(mp, v.first, C, v.second, 1);
    }
    for(pair<int, int> v : adj[C]){
        find_centroid(v.first, C);
    }
}

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(make_pair(H[i][1], L[i])); 
        adj[H[i][1]].push_back(make_pair(H[i][0], L[i]));
    }
    find_centroid();
    return (ans >= 1e9?-1:ans);
}

/*
int main(){
    int _N, _K; 
    cin >> _N >> _K; 
    int _H[mxN][2], _L[mxN];

    for(int i = 0; i<_N-1; ++i){
        cin >> _H[i][0] >> _H[i][1] >> _L[i];
    }
    
    cout << best_path(_N, _K, _H, _L) << endl;
}
*/

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

/usr/bin/ld: /tmp/ccv4Cekb.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