이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define gcd __gcd
#define vsz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
///#define int long long
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}
const int N = 2e5+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;
const int lg = 21;
int n, cntShop, Exit, q, timerDFS;
int tin[N], tout[N], par[lg][N], h[N];
ll near[N], val[lg][N], dist[N];
bool isShop[N];
pair<int,int> E[N];
vector<pair<int,int>> adj[N];
void dfs(int x, int p){
tin[x] = ++timerDFS;
near[x] = (isShop[x] ? 0 : INF);
for(pair<int,int> e : adj[x])if(e.fi != p){
int v = e.fi; ll w = e.se;
h[v] = h[x] + 1;
dist[v] = dist[x] + w;
par[0][v] = x;
for(int i = 1; i < lg; i++) par[i][v] = par[i-1][par[i-1][v]];
dfs(v, x);
ckmn(near[x], near[v] + w);
}
tout[x] = ++timerDFS;
return;
}
ll get_val(int x, int asc){
assert(h[x] >= h[asc]);
int k = h[x] - h[asc] + 1; ll res = INF;
for(int i = 0; i < lg; i++){
if(k >> i & 1){
ckmn(res, val[i][x]);
x = par[i][x];
}
}
return res;
}
bool in(int u, int v){
return tin[u] <= tin[v] and tout[v] <= tout[u];
}
void solve()
{
cin >> n >> cntShop >> q >> Exit;
for(int i = 1; i < n; i++){
int u,v,w; cin >> u >> v >> w;
adj[u].pb(make_pair(v, w));
adj[v].pb(make_pair(u, w));
E[i] = make_pair(u, v);
}
for(int i = 1; i <= cntShop; i++){
int v; cin >> v;
isShop[v] = true;
}
dfs(Exit, -1);
for(int i = 1; i <= n; i++){
/// dist(u,v) = dist[u] + dist[v] - 2 * dist[lca(u,v)]
/// near[i] = dist[v] - dist[lca(u,v)] so val[0][i] will be -dist[lca(u,v)] + near[i]
if(near[i] != INF) val[0][i] = -1 * dist[i] + near[i];
else{
val[0][i] = INF;
}
}
for(int i = 1; i < lg; i++){
for(int j = 1; j <= n; j++){
val[i][j] = min(val[i-1][j], val[i-1][par[i-1][j]]);
}
}
for(int i = 1; i < n; i++){
if(h[E[i].fi] > h[E[i].se]) swap(E[i].fi, E[i].se);
}
while(q--){
int i,R; cin >> i >> R;
int v = E[i].se;
if(!in(v, R)){
cout << "escaped\n";
}
else{
ll answer = get_val(R, v);
if(answer == INF){
cout << "oo\n";
}
else cout << answer + dist[R] <<"\n";
}
}
return;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP","r",stdin);
freopen(name".OUT","w",stdout);
}
int t = 1; //cin >> t;
while(t--) solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
valley.cpp: In function 'int main()':
valley.cpp:131:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
131 | freopen(name".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
valley.cpp:132:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
132 | freopen(name".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... |