# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
308194 |
2020-09-30T08:57:02 Z |
HynDuf |
Valley (BOI19_valley) |
C++11 |
|
341 ms |
38008 KB |
#include <bits/stdc++.h>
#define task "V"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define Rep(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';}
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a));
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
#define jn ___jn
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;
const int N = 1e5 + 2;
const ll INF = 1e18;
int n, m, r, q, pa[N][18], dep[N];
bool market[N];
ll d[N], mn[N], mnA[N][18];
vii g[N];
ii edge[N];
void dfs(int u, int p)
{
dep[u] = dep[p] + 1;
pa[u][0] = p;
rep(k, 1, 17) pa[u][k] = pa[pa[u][k - 1]][k - 1];
mn[u] = INF;
for (ii v : g[u]) if (v.F != p)
{
d[v.F] = d[u] + v.S;
dfs(v.F, u);
mn[u] = min(mn[u], mn[v.F]);
}
if (market[u]) mn[u] = min(mn[u], d[u]);
}
int lca(int x, int y)
{
if (dep[x] < dep[y]) swap(x, y);
Rep(k, 17, 0) if (dep[pa[x][k]] >= dep[y]) x = pa[x][k];
if (x == y) return x;
Rep(k, 17, 0) if (pa[x][k] != pa[y][k]) x = pa[x][k], y = pa[y][k];
return pa[x][0];
}
void buildmnA(int u, int p)
{
mnA[u][0] = mn[u];
rep(k, 1, 17) mnA[u][k] = min(mnA[u][k - 1], mnA[pa[u][k - 1]][k - 1]);
for (ii v : g[u]) if (v.F != p) buildmnA(v.F, u);
}
int main()
{
#ifdef HynDuf
freopen(task".in", "r", stdin);
//freopen(task".out", "w", stdout);
#else
ios_base::sync_with_stdio(false); cin.tie(nullptr);
#endif
cin >> n >> m >> q >> r;
rep(i, 2, n)
{
int u, v, w;
cin >> u >> v >> w;
g[u].eb(v, w);
g[v].eb(u, w);
edge[i - 1] = {u, v};
}
rep(i, 1, m)
{
int u;
cin >> u;
market[u] = 1;
}
dfs(r, 0);
rep(i, 1, n) if (mn[i] != INF) mn[i] -= 2 * d[i];
buildmnA(r, 0);
while (q--)
{
int e, u, uu;
cin >> e >> u;
uu = u;
int v = (dep[edge[e].F] > dep[edge[e].S]) ? edge[e].F : edge[e].S;
if (lca(u, v) != v) cout << "escaped\n";
else
{
ll ans = mn[v];
Rep(k, 17, 0) if (dep[pa[u][k]] >= dep[v])
{
ans = min(ans, mnA[u][k]);
u = pa[u][k];
}
if (ans == INF) cout << "oo\n";
else cout << ans + d[uu] << '\n';
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2944 KB |
Output is correct |
2 |
Correct |
6 ms |
2944 KB |
Output is correct |
3 |
Correct |
7 ms |
2944 KB |
Output is correct |
4 |
Correct |
6 ms |
2944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2944 KB |
Output is correct |
2 |
Correct |
6 ms |
2944 KB |
Output is correct |
3 |
Correct |
7 ms |
2944 KB |
Output is correct |
4 |
Correct |
6 ms |
2944 KB |
Output is correct |
5 |
Correct |
4 ms |
3072 KB |
Output is correct |
6 |
Correct |
3 ms |
3072 KB |
Output is correct |
7 |
Correct |
5 ms |
3072 KB |
Output is correct |
8 |
Correct |
3 ms |
3072 KB |
Output is correct |
9 |
Correct |
4 ms |
3072 KB |
Output is correct |
10 |
Correct |
4 ms |
3072 KB |
Output is correct |
11 |
Correct |
3 ms |
3072 KB |
Output is correct |
12 |
Correct |
4 ms |
3072 KB |
Output is correct |
13 |
Correct |
3 ms |
3072 KB |
Output is correct |
14 |
Correct |
4 ms |
3072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
216 ms |
35064 KB |
Output is correct |
2 |
Correct |
281 ms |
34808 KB |
Output is correct |
3 |
Correct |
280 ms |
34680 KB |
Output is correct |
4 |
Correct |
325 ms |
36216 KB |
Output is correct |
5 |
Correct |
279 ms |
36216 KB |
Output is correct |
6 |
Correct |
341 ms |
37624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2944 KB |
Output is correct |
2 |
Correct |
6 ms |
2944 KB |
Output is correct |
3 |
Correct |
7 ms |
2944 KB |
Output is correct |
4 |
Correct |
6 ms |
2944 KB |
Output is correct |
5 |
Correct |
4 ms |
3072 KB |
Output is correct |
6 |
Correct |
3 ms |
3072 KB |
Output is correct |
7 |
Correct |
5 ms |
3072 KB |
Output is correct |
8 |
Correct |
3 ms |
3072 KB |
Output is correct |
9 |
Correct |
4 ms |
3072 KB |
Output is correct |
10 |
Correct |
4 ms |
3072 KB |
Output is correct |
11 |
Correct |
3 ms |
3072 KB |
Output is correct |
12 |
Correct |
4 ms |
3072 KB |
Output is correct |
13 |
Correct |
3 ms |
3072 KB |
Output is correct |
14 |
Correct |
4 ms |
3072 KB |
Output is correct |
15 |
Correct |
216 ms |
35064 KB |
Output is correct |
16 |
Correct |
281 ms |
34808 KB |
Output is correct |
17 |
Correct |
280 ms |
34680 KB |
Output is correct |
18 |
Correct |
325 ms |
36216 KB |
Output is correct |
19 |
Correct |
279 ms |
36216 KB |
Output is correct |
20 |
Correct |
341 ms |
37624 KB |
Output is correct |
21 |
Correct |
212 ms |
34540 KB |
Output is correct |
22 |
Correct |
227 ms |
34168 KB |
Output is correct |
23 |
Correct |
252 ms |
34040 KB |
Output is correct |
24 |
Correct |
281 ms |
35704 KB |
Output is correct |
25 |
Correct |
316 ms |
38008 KB |
Output is correct |
26 |
Correct |
204 ms |
34528 KB |
Output is correct |
27 |
Correct |
226 ms |
34424 KB |
Output is correct |
28 |
Correct |
251 ms |
34168 KB |
Output is correct |
29 |
Correct |
286 ms |
35320 KB |
Output is correct |
30 |
Correct |
315 ms |
36600 KB |
Output is correct |
31 |
Correct |
200 ms |
34680 KB |
Output is correct |
32 |
Correct |
223 ms |
34424 KB |
Output is correct |
33 |
Correct |
252 ms |
34424 KB |
Output is correct |
34 |
Correct |
292 ms |
35832 KB |
Output is correct |
35 |
Correct |
311 ms |
38008 KB |
Output is correct |
36 |
Correct |
260 ms |
35832 KB |
Output is correct |