제출 #1052125

#제출 시각아이디문제언어결과실행 시간메모리
1052125JosephA경주 (Race) (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) }; int best_path(int n, int k, int edges_[][2], int weights[]) { vector<tuple<int, int, ll>> edges; Tree t(n + 1); for(int i = 1; i < n; i++) { edges.pb({edges[i][0], edges_[i][1], 0LL}); t.addEdge(edges[i][0], edges[i][1]); } t.init(); for(int i = 0; i < n-1; i++) { get<2>(edges[i]) = weights[i]; } 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); return (ans == 1e9 ? -1 : ans); }

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

race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:91:27: error: no match for 'operator[]' (operand types are '__gnu_cxx::__alloc_traits<std::allocator<std::tuple<int, int, long long int> >, std::tuple<int, int, long long int> >::value_type' {aka 'std::tuple<int, int, long long int>'} and 'int')
   91 |         edges.pb({edges[i][0], edges_[i][1], 0LL});
      |                           ^
race.cpp:91:50: error: no matching function for call to 'std::vector<std::tuple<int, int, long long int> >::push_back(<brace-enclosed initializer list>)'
   91 |         edges.pb({edges[i][0], edges_[i][1], 0LL});
      |                                                  ^
In file included from /usr/include/c++/10/vector:67,
                 from /usr/include/c++/10/functional:62,
                 from /usr/include/c++/10/pstl/glue_algorithm_defs.h:13,
                 from /usr/include/c++/10/algorithm:74,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
                 from race.cpp:1:
/usr/include/c++/10/bits/stl_vector.h:1187:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::tuple<int, int, long long int>; _Alloc = std::allocator<std::tuple<int, int, long long int> >; std::vector<_Tp, _Alloc>::value_type = std::tuple<int, int, long long int>]'
 1187 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/include/c++/10/bits/stl_vector.h:1187:35: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const value_type&' {aka 'const std::tuple<int, int, long long int>&'}
 1187 |       push_back(const value_type& __x)
      |                 ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/10/bits/stl_vector.h:1203:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = std::tuple<int, int, long long int>; _Alloc = std::allocator<std::tuple<int, int, long long int> >; std::vector<_Tp, _Alloc>::value_type = std::tuple<int, int, long long int>]'
 1203 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/include/c++/10/bits/stl_vector.h:1203:30: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'std::vector<std::tuple<int, int, long long int> >::value_type&&' {aka 'std::tuple<int, int, long long int>&&'}
 1203 |       push_back(value_type&& __x)
      |                 ~~~~~~~~~~~~~^~~
race.cpp:92:27: error: no match for 'operator[]' (operand types are '__gnu_cxx::__alloc_traits<std::allocator<std::tuple<int, int, long long int> >, std::tuple<int, int, long long int> >::value_type' {aka 'std::tuple<int, int, long long int>'} and 'int')
   92 |         t.addEdge(edges[i][0], edges[i][1]);
      |                           ^
race.cpp:92:40: error: no match for 'operator[]' (operand types are '__gnu_cxx::__alloc_traits<std::allocator<std::tuple<int, int, long long int> >, std::tuple<int, int, long long int> >::value_type' {aka 'std::tuple<int, int, long long int>'} and 'int')
   92 |         t.addEdge(edges[i][0], edges[i][1]);
      |                                        ^