# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
476180 |
2021-09-25T07:57:49 Z |
Karliver |
Valley (BOI19_valley) |
C++17 |
|
287 ms |
64576 KB |
#include <bits/stdc++.h>
#define FIXED_FLOAT(x) std::fixed <<std::setprecision(20) << (x)
#define all(v) (v).begin(), (v).end()
using namespace std;
#define forn(i,n) for (int i = 0; i < (n); ++i)
#define rforn(i, n) for(int i = (n) - 1;i >= 0;--i)
using ll = long long;
int mod = (ll)1e9 + 7;
#define PI acos(-1)
typedef pair<int, int> pairs;
const int INF = 1e9 + 1;
const int N = 2e5 + 100;
const double eps = 1e-7;
const ll inf = 1e16;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <typename XPAX>
void ckma(XPAX &x, XPAX y) {
x = (x < y ? y : x);
}
template <typename XPAX>
void ckmi(XPAX &x, XPAX y) {
x = (x > y ? y : x);
}
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
VV<pairs> g(N);
int F[N][20];
pair<ll, int> dis[N][20];
V<bool> shop(N, false);
V<ll> ms(N);
V<int> dep(N, 0);
int T = 0;
V<array<int, 2>> D(N);
V<int> tin(N), tout(N);
V<ll> ts(N);
void dfs1(int v, int p) {
tin[v] = ++T;
F[v][0] = p;
for(int i = 1;i < 20;++i)
F[v][i] = F[F[v][i - 1]][i - 1];
for(auto [c,w] : g[v])
if(c != p) {
ts[c] = ts[v] + w;
dep[c] = dep[v] + 1;
dfs1(c, v);
}
tout[v] = T;
}
void dfs2(int v, int p) {
if(shop[v])
ms[v] = 0;
else ms[v] = inf;
for(auto [c, w] : g[v]) {
if(c == p)continue;
dfs2(c, v);
ckmi(ms[v], ms[c] + w);
}
}
void dfs3(int v, int p, int cost) {
dis[v][0] = {ms[p] + cost, p};
for(int i = 1;i < 20;++i) {
int id = dis[F[v][i - 1]][i - 1].second;
dis[v][i] = min(dis[v][i - 1], {ms[id] + abs(ts[id] - ts[v]), id});
}
for(auto [c, w] : g[v])
if(c != p)dfs3(c, v, w);
}
bool isb(int x, int y) {
return tin[x] <= tin[y] && tout[y] <= tout[x];
}
ll jp(int x, int k) {
ll ret = ms[x];
rforn(j, 20) if(k >> j & 1) {
ckmi(ret, dis[x][j].first);
x = F[x][j];
}
return ret;
}
void solve() {
int n, s, q, e;
cin >> n >> s >> q >> e;
for(int i = 1;i <= n - 1;++i) {
int a, b, c;
cin >> a >> b >> c;
g[a].push_back({b, c});
g[b].push_back({a, c});
D[i] = {a, b};
}
forn(i, s) {
int x;
cin >> x;
shop[x] = 1;
}
dfs1(e, e);
dfs2(e, e);
dfs3(e, e, 0);
//debug(dis[9][1]);
while(q--) {
int id, R;
cin >> id >> R;
int x = D[id][0], y = D[id][1];
if(!isb(x, R) || !isb(y, R)) {
cout << "escaped" << '\n';
continue;
}
if(tin[x] < tin[y])swap(x, y);
int k = dep[R] - dep[x];
assert(k >= 0);
ll res = jp(R, k);
if(res == inf)cout << "oo" << '\n';
else cout << res << '\n';
}
}
void test_case() {
int t;
cin >> t;
forn(p, t) {
//cout << "Case #" << p + 1 << ": ";
solve();
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
12236 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
12236 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
202 ms |
55828 KB |
Output is correct |
2 |
Correct |
259 ms |
59328 KB |
Output is correct |
3 |
Correct |
233 ms |
59208 KB |
Output is correct |
4 |
Correct |
262 ms |
61788 KB |
Output is correct |
5 |
Correct |
274 ms |
61796 KB |
Output is correct |
6 |
Correct |
287 ms |
64576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
12236 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |