제출 #1052116

#제출 시각아이디문제언어결과실행 시간메모리
1052116JosephARace (IOI11_race)C++17
컴파일 에러
0 ms0 KiB
#include<bits/stdc++.h> using namespace std; #define pb push_back #define SZ(x) (int)(x).size() typedef long long ll; struct E { int v; ll w; }; struct Tree{ int n, l; vector<vector<int>>adj; vector<ll>depth, parent, subtree, tin, tout; vector<vector<int>>up; int timer; Tree(int n){ this->n = n; adj.resize(n + 1); depth.resize(n + 1); parent.resize(n + 1); subtree.resize(n + 1); tin.resize(n + 1); tout.resize(n + 1); timer = 0; l = ceil(log2(n)); up.resize(n + 1, vector<int>(l + 1)); } void addEdge(int u, int v){ adj[u].pb(v); adj[v].pb(u); } void init(){ dfs(1, -1); dfs1(1, -1); } void dfs(int u, int p){ tin[u] = ++timer; up[u][0] = (p == -1 ? 1 : p); for(int i = 1; i <= l; i++){ up[u][i] = up[up[u][i - 1]][i - 1]; } for(auto v : adj[u]){ if(v != p){ depth[v] = depth[u] + 1; dfs(v, u); } } tout[u] = ++timer; } // O(nlogn) bool is_ancestor(int u, int v){ return tin[u] <= tin[v] && tout[u] >= tout[v]; } // O(1) int lca(int u, int v){ if(is_ancestor(u, v)) return u; if(is_ancestor(v, u)) return v; for(int i = l; i >= 0; i--){ if(!is_ancestor(up[u][i], v)){ u = up[u][i]; } } return up[u][0]; } // O(logn) int dist(int u, int v){ return depth[u] + depth[v] - 2 * depth[lca(u, v)]; } // O(logn) int get_kth_ancestor(int u, int k){ for(int i = l; i >= 0; i--){ if(k & (1 << i)){ u = up[u][i]; } } return u; } // O(logn) void dfs1(int u, int p){ subtree[u] = 1; for(auto v : adj[u]){ if(v != p){ dfs1(v, u); subtree[u] += subtree[v]; } } } // O(n) bool is_leaf(int u){ return u!=1 && SZ(adj[u]) == 1; } // O(1) }; void solve() { int n, k; cin >> n >> k; vector<tuple<int, int, ll>> edges; Tree t(n + 1); for(int i = 1; i < n; i++) { int u, v; cin >> u >> v; edges.pb({u, v, 0}); t.addEdge(u, v); } t.init(); for(int i = 0; i < n-1; i++) { ll w; cin >> w; get<2>(edges[i]) = w; } vector<E> adj[n + 1]; for(int i = 0; i < SZ(edges); i++) { int u, v; ll w; tie(u, v, w) = edges[i]; adj[u].pb({v, w}); adj[v].pb({u, w}); } vector<ll> dist(n + 1, 0LL), dp(n + 1, 0LL); function<void(int, int)> init = [&](int u, int p) { for(auto [v, w] : adj[u]) if(v != p) { dp[v] = dp[u] + w; init(v, u); } }; init(0, 0); auto distance = [&](int u, int v) { return t.dist(u, v); }; int ans = 1e9; map<ll, int> s[n + 1]; auto MERGE = [&](int u, int v) { if(s[v].find(k + dp[u]) != s[v].end()) ans = min(ans, distance(u, s[v][k + dp[u]])); if(s[u].size() < s[v].size()) swap(s[u], s[v]); for(auto [d, l] : s[v]) { if(s[u].find(-d + 2*dp[u] + k) != s[u].end()) ans = min(ans, distance(l, s[u][-d + 2*dp[u] + k])); if(!s[u].count(d)) s[u][d] = l; else s[u][d] = (t.depth[l] < t.depth[s[u][d]] ? l : s[u][d]); } }; function<void(int, int)> dfs = [&](int u, int p) { for(auto [v, w] : adj[u]) if(v != p) { dfs(v, u); MERGE(u, v); } s[u][dp[u]] = u; }; dfs(0, -1); cout << (ans == 1e9 ? -1 : ans); } int main() { ios::sync_with_stdio(0); cin.tie(0); solve(); }

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

/usr/bin/ld: /tmp/ccf4NMxQ.o: in function `main':
race.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccaN1d7Q.o:grader.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/ccaN1d7Q.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