Submission #496277

# Submission time Handle Problem Language Result Execution time Memory
496277 2021-12-21T04:42:10 Z Nalrimet Valley (BOI19_valley) C++17
59 / 100
3000 ms 26192 KB
//#pragma GCC target("avx2")
//#pragma GCC optimization("O3")
//#pragma GCC optimization("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>

#define ll long long
#define pb push_back
#define F first
#define S second

using namespace std;

const int N = 2 * 1e5 + 5;
const int MOD = 1e9 + 7;
const int K = 18;
const long long inf = 1e18;

int n, m, h[N], q, a[N], b[N], t[N], r[N], parent[N], par[N][K], s, e, x[N], son;
vector<pair<int, int>> g[N];
long long d[N];

void dfs(int v, int pr){
	par[v][0] = pr;

	for(auto to : g[v]){
		if(to.F != pr){
			d[to.F] = d[v] + to.S;
			h[to.F] = h[v] + 1;
			dfs(to.F, v);
		}
	}
}

bool bit(int x, int p){
	return x & (1 << p);
}

int lift(int v, int k){
	for(int i = 0; i < K; i++){
		if (bit(k, i)) {
			v = par[v][i];
		}
	}
	return v;
}

int lca(int a, int b){
	if(h[a] < h[b]){
		swap(a, b);
	}
	a = lift(a, h[a] - h[b]);
	if(a == b){
		return a;
	}
	for(int i = K - 1; i >= 0; --i){
		if(par[a][i] != par[b][i]){
			a = par[a][i];
			b = par[b][i];
		}
	}
	return par[a][0];
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

	cin >> n >> s >> q >> e;

	for(int i = 1, u, v, w; i < n; i++){
		cin >> u >> v >> w;
		g[u].pb({v, w});
		g[v].pb({u, w});
		a[i] = u;
		b[i] = v;
	}

    dfs(1, 1);

    for(int i = 1; i < K; i++){
    	for(int v = 1; v <= n; v++){
    		par[v][i] = par[par[v][i - 1]][i - 1];
    	}
    }

    for(int i = 1; i <= s; ++i){
        cin >> x[i];
    }

    for(int i = 1; i <= q; ++i){
        cin >> t[i] >> r[i];
        if(par[a[t[i]]][0] == b[t[i]]) son = a[t[i]];
        else son = b[t[i]];
        if(lca(son, r[i]) == son){
            if(lca(son, e) == son) {cout << "escaped\n"; continue;}
            else {
                ll res = inf;
                if(s == n){
                    res = 0;
                }
                else
                for(int j = 1; j <= s; ++j){
                    if(lca(x[j], son) == son){
                        res = min(res, d[x[j]] + d[r[i]] - d[lca(r[i], x[j])] * 2);
                    }
                }
                if(res == inf) cout << "oo\n";
                else cout << res << '\n';
            }
        }
        else{
            if(lca(son, e) != son) {cout << "escaped\n"; continue;}
            else{
                ll res = inf;
                if(s == n){
                    res = 0;
                }
                else
                for(int j = 1; j <= s; ++j){
                    if(lca(x[j], son) != son){
                        res = min(res, d[x[j]] + d[r[i]] - d[lca(r[i], x[j])] * 2);
                    }
                }
                if(res == inf) cout << "oo\n";
                else cout << res << '\n';
            }
        }
    }

	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 6 ms 5196 KB Output is correct
2 Correct 7 ms 5196 KB Output is correct
3 Correct 14 ms 5196 KB Output is correct
4 Correct 6 ms 5196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5196 KB Output is correct
2 Correct 7 ms 5196 KB Output is correct
3 Correct 14 ms 5196 KB Output is correct
4 Correct 6 ms 5196 KB Output is correct
5 Correct 3 ms 5196 KB Output is correct
6 Correct 5 ms 5196 KB Output is correct
7 Correct 4 ms 5196 KB Output is correct
8 Correct 5 ms 5196 KB Output is correct
9 Correct 7 ms 5116 KB Output is correct
10 Correct 7 ms 5196 KB Output is correct
11 Correct 3 ms 5196 KB Output is correct
12 Correct 3 ms 5196 KB Output is correct
13 Correct 3 ms 5196 KB Output is correct
14 Correct 11 ms 5220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 131 ms 19728 KB Output is correct
2 Correct 120 ms 19620 KB Output is correct
3 Correct 141 ms 19360 KB Output is correct
4 Correct 141 ms 20804 KB Output is correct
5 Correct 174 ms 20804 KB Output is correct
6 Correct 157 ms 23832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5196 KB Output is correct
2 Correct 7 ms 5196 KB Output is correct
3 Correct 14 ms 5196 KB Output is correct
4 Correct 6 ms 5196 KB Output is correct
5 Correct 3 ms 5196 KB Output is correct
6 Correct 5 ms 5196 KB Output is correct
7 Correct 4 ms 5196 KB Output is correct
8 Correct 5 ms 5196 KB Output is correct
9 Correct 7 ms 5116 KB Output is correct
10 Correct 7 ms 5196 KB Output is correct
11 Correct 3 ms 5196 KB Output is correct
12 Correct 3 ms 5196 KB Output is correct
13 Correct 3 ms 5196 KB Output is correct
14 Correct 11 ms 5220 KB Output is correct
15 Correct 131 ms 19728 KB Output is correct
16 Correct 120 ms 19620 KB Output is correct
17 Correct 141 ms 19360 KB Output is correct
18 Correct 141 ms 20804 KB Output is correct
19 Correct 174 ms 20804 KB Output is correct
20 Correct 157 ms 23832 KB Output is correct
21 Correct 130 ms 19344 KB Output is correct
22 Correct 117 ms 19108 KB Output is correct
23 Correct 135 ms 19524 KB Output is correct
24 Correct 152 ms 23236 KB Output is correct
25 Correct 159 ms 26192 KB Output is correct
26 Correct 383 ms 22644 KB Output is correct
27 Correct 499 ms 22292 KB Output is correct
28 Correct 788 ms 22252 KB Output is correct
29 Correct 1103 ms 23788 KB Output is correct
30 Correct 1092 ms 25376 KB Output is correct
31 Execution timed out 3017 ms 20592 KB Time limit exceeded
32 Halted 0 ms 0 KB -