이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define task ""
#define fi first
#define se second
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (b), _a = (a); i >= _a; --i)
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
const int dx[] = {-1, 0, 0, +1};
const int dy[] = {0, -1, +1, 0};
const long long INF = 1e18 + 7;
const int MAX = 100010;
int N, S, Q, E;
bool isShop[MAX];
vector<pair<int, int>> adj[MAX];
pair<int, pair<int, int>> edges[MAX];
int timer;
int tin[MAX], tout[MAX];
int h[MAX], anc[17][MAX];
long long dep[MAX], down[MAX], dp[17][MAX];
void dfs(int u, int p) { // calc down[_], dp[_]
tin[u] = ++timer;
if (isShop[u]) {
down[u] = 0;
}
for (pair<int, int> &x: adj[u]) if (x.fi != p) {
int v = x.fi, w = x.se;
dep[v] = dep[u] + w;
h[v] = h[u] + 1;
anc[0][v] = u;
dfs(v, u);
minimize(down[u], down[v] + w);
}
tout[u] = timer;
}
void reDfs(int u, int p) {
for (pair<int, int> &x: adj[u]) if (x.fi != p) {
int v = x.fi;
minimize(dp[0][v], down[u] - dep[u]);
for (int j = 1; j <= 16; ++j) {
anc[j][v] = anc[j - 1][anc[j - 1][v]];
dp[j][v] = min(dp[j - 1][v], dp[j - 1][anc[j - 1][v]]);
}
reDfs(v, u);
}
}
long long jump(int u, int p) {
long long res = down[u] - dep[u];
int k = h[u] - h[p];
for (int j = 0; (1 << j) <= k; ++j) {
if (k >> j & 1) {
res = min(res, dp[j][u]);
u = anc[j][u];
}
}
return res;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin >> N >> S >> Q >> E;
for (int i = 1; i < N; ++i) {
int u, v, w; cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
edges[i] = {w, {u, v}};
}
for (int i = 0; i < S; ++i) {
int x; cin >> x;
isShop[x] = true;
}
for (int i = 1; i <= N; ++i) {
down[i] = INF;
for (int j = 0; j <= 16; ++j) {
dp[j][i] = INF;
}
}
dfs(E, -1); reDfs(E, -1);
while (Q--) {
int i, R; cin >> i >> R;
int u = edges[i].se.fi, v = edges[i].se.se;
if (h[u] > h[v]) swap(u, v); // v is child of u
if (tin[v] <= tin[R] && tout[R] <= tout[v]) {
long long res = jump(R, v) + dep[R];
if (res >= 1e16) cout << "oo\n";
else cout << res << '\n';
} else cout << "escaped\n";
}
// cerr << "\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
valley.cpp: In function 'int main()':
valley.cpp:73:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:74:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |