이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std ;
const long long inf = 1e15 , inf2 = 1e14 ;
const int MAX = 1e5 + 10 ;
long long tree[4 * MAX] , lazy[4 * MAX] ;
void prop(int node , int l , int r)
{
tree[node] += lazy[node] ;
if(l != r)
{
lazy[node << 1] += lazy[node] ;
lazy[node << 1 | 1] += lazy[node] ;
}
lazy[node] = 0 ;
return ;
}
void update(int node , int l , int r , int from , int to , long long val)
{
prop(node , l , r) ;
if(from > r || to < l)
return ;
if(l >= from && r <= to)
{
lazy[node] += val ;
prop(node , l , r) ;
return ;
}
int mid = (l + r) >> 1 ;
update(node << 1 , l , mid , from , to , val) ;
update(node << 1 | 1 , mid+1 , r , from , to , val) ;
tree[node] = min(tree[node << 1] , tree[node << 1 | 1]) ;
}
long long query(int node , int l , int r , int from , int to)
{
prop(node , l , r) ;
if(from > r || to < l || from > to)
return inf ;
if(l >= from && r <= to)
return tree[node] ;
int mid = (l + r) >> 1 ;
long long a = query(node << 1 , l , mid , from , to) ;
long long b = query(node << 1 | 1 , mid+1 , r , from , to) ;
return min(a , b) ;
}
int u[MAX] , v[MAX] , c[MAX] , mark[MAX] , in[MAX] , out[MAX] ;
int n , s , q , e ;
long long ans[MAX] ;
vector< vector< pair<int , int> > >adj(MAX) , Query(MAX) ;
int tim = 0 ;
void dfs1(int node , int par , long long dist)
{
in[node] = ++tim ;
if(mark[node])
update(1 , 1 , n , in[node] , in[node] , dist) ;
else
update(1 , 1 , n , in[node] , in[node] , inf) ;
for(auto &child : adj[node])
{
if(child.first == par)
continue ;
dfs1(child.first , node , dist+child.second) ;
}
out[node] = tim ;
return ;
}
void dfs2(int node , int par)
{
for(auto &i : Query[node])
{
int x = i.first ;
bool flag1 = (in[node] >= in[v[x]] && in[node] <= out[v[x]]) ;
bool flag2 = (in[e] >= in[v[x]] && in[e] <= out[v[x]]) ;
if(flag1 == flag2)
{
ans[i.second] = -2 ;
continue ;
}
if(flag1)
{
ans[i.second] = query(1 , 1 , n , in[v[x]] , out[v[x]]) ;
if(ans[i.second] > inf2)
ans[i.second] = -1 ;
}
else
{
long long a = query(1 , 1 , n , 1 , in[v[x]]-1) ;
long long b = query(1 , 1 , n , out[v[x]]+1 , n) ;
ans[i.second] = min(a , b) ;
if(ans[i.second] > inf2)
ans[i.second] = -1 ;
}
}
for(auto &child : adj[node])
{
if(child.first == par)
continue ;
update(1 , 1 , n , 1 , n , child.second) ;
update(1 , 1 , n , in[child.first] , out[child.first] , -2ll * child.second) ;
dfs2(child.first , node) ;
update(1 , 1 , n , 1 , n , -1ll * child.second) ;
update(1 , 1 , n , in[child.first] , out[child.first] , 2ll * child.second) ;
}
return ;
}
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n>>s>>q>>e ;
for(int i = 1 ; i <= n-1 ; ++i)
{
cin>>u[i]>>v[i]>>c[i] ;
adj[u[i]].emplace_back(v[i] , c[i]) ;
adj[v[i]].emplace_back(u[i] , c[i]) ;
}
for(int i = 0 ; i < s ; ++i)
{
int x ;
cin>>x ;
mark[x] = 1 ;
}
for(int i = 0 ; i < q ; ++i)
{
int x , y ;
cin>>x>>y ;
Query[y].emplace_back(x , i) ;
}
dfs1(1 , -1 , 0) ;
for(int i = 1 ; i <= n-1 ; ++i)
{
if(in[v[i]] < in[u[i]])
swap(u[i] , v[i]) ;
}
dfs2(1 , -1) ;
for(int i = 0 ; i < q ; ++i)
{
if(ans[i] >= 0)
cout<<ans[i]<<"\n" ;
else if(ans[i] == -1)
cout<<"oo\n" ;
else
cout<<"escaped\n" ;
}
return 0 ;
}
# | 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... |