Submission #526259

#TimeUsernameProblemLanguageResultExecution timeMemory
526259EvangValley (BOI19_valley)C++17
100 / 100
255 ms40820 KiB
#include <bits/extc++.h> using namespace std; using namespace __gnu_pbds; #ifdef _DEBUG #define dout(x) clog << "Line " << __LINE__ << ": " << #x << "=" << (x) << el #else #define dout(x) #endif mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); #define uid(a,b) uniform_int_distribution<int>(a,b)(rng) #define ins insert #define ssize(x) (int((x).size())) #define bs(args...) binary_search(args) #define lb(args...) lower_bound(args) #define ub(args...) upper_bound(args) #define all(x) (x).begin(),(x).end() #define mp(a, b) make_pair(a, b) #define mt(args...) make_tuple(args) #define pb(x) push_back(x) #define eb(args...) emplace_back(args) #define ff first #define ss second #define die exit(0) template<typename T> using vc = vector<T>; template<typename T> using uset = unordered_set<T>; template<typename A, typename B> using umap = unordered_map<A, B>; template<typename T, typename Comp> using pq = std::priority_queue<T, vc<T>, Comp>; template<typename T> using maxpq = pq<T, less<T>>; template<typename T> using minpq = pq<T, greater<T>>; template<typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; using db = double; using ld = long double; using ll = long long; using ull = unsigned long long; using pi = pair<int, int>; using pll = pair<ll, ll>; using vi = vc<int>; using vll = vc<ll>; using vpi = vc<pi>; using vpll = vc<pll>; using str = string; constexpr char el = '\n'; constexpr char sp = ' '; constexpr int inf = 0x3f3f3f3f; constexpr ll llinf = 0x3f3f3f3f3f3f3f3fLL; // --------------------------------------------------------------------- const int LG = 20; const int N = 1e5+5; int n, s, q, e, up[LG][N], dep[N]; ll mag[LG][N], dis[N]; tuple<int, int, int> eg[N]; bool is_shop[N]; vpi adj[N]; int anc(int x, int k){ for(int i = 0; k > 0; ++i){ if(k&1) x = up[i][x]; k /= 2; } return x; } int lca(int a, int b){ a = anc(a, max(0, dep[a]-dep[b])); b = anc(b, max(0, dep[b]-dep[a])); if(a==b) return a; for(int i = LG-1; i >= 0; --i) if(up[i][a]!=up[i][b]){ a = up[i][a]; b = up[i][b]; } return up[0][a]; } void dfs(int v, int p){ up[0][v] = p; if(is_shop[v]) mag[0][v] = dis[v]; else mag[0][v] = llinf; for(auto[u, w]: adj[v]){ if(u==p) continue; dep[u] = dep[v] + 1; dis[u] = dis[v] + w; dfs(u, v); mag[0][v] = min(mag[0][v], mag[0][u]); } } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed; clog << fixed; clog << unitbuf; #ifdef _DEBUG freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("debug.txt", "w", stderr); #else //freopen(".in", "r", stdin); //freopen(".out", "w", stdout); #endif cin >> n >> s >> q >> e; for(int i = 1; i < n; ++i){ int a, b, w; cin >> a >> b >> w; adj[a].eb(b, w); adj[b].eb(a, w); eg[i] = {a, b, w}; } while(s--){ int x; cin >> x; is_shop[x] = 1; } dfs(e, 0); for(int i = 1; i <= n; ++i) if(mag[0][i]<llinf) mag[0][i] -= 2*dis[i]; #ifdef _DEBUG clog << "Line " << __LINE__ << ":\n"; for(int i = 1; i <= n; ++i) clog << dep[i] << sp << dis[i] << el; clog << el; #endif for(int i = 1; i < LG; ++i) for(int j = 1; j <= n; ++j){ up[i][j] = up[i-1][up[i-1][j]]; mag[i][j] = min(mag[i-1][j], mag[i-1][up[i-1][j]]); } while(q--){ int i, r; cin >> i >> r; auto[v, u, _] = eg[i]; if(lca(v, r)!=v || lca(u, r) != u){ cout << "escaped\n"; continue; } if(dep[v]<dep[u]) swap(v, u); u = r; ll mi = llinf; for(int i = LG-1; i >= 0; --i) if(dep[up[i][r]] >= dep[v]){ mi = min(mi, mag[i][r]); r = up[i][r]; } mi = min(mi, mag[0][r]); if(mi >= llinf) cout << "oo\n"; else cout << dis[u] + mi << el; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...