This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int mxN = 1e5;
const long long int mxV = 1e15;
bool shop[mxN];
int depth[mxN];
int nidx[mxN];
int ssize[mxN];
long long int dp[mxN][18];
int dpidx[mxN][18];
long long int dis[mxN][18];
void setup(int node, int parent, int cnt, int d, vector<vector<pair<int,int>>> &connect){
nidx[node] = cnt;
ssize[node] = 1;
depth[node] = d;
pair<int,int> next;
long long int mini = mxV;
for (int i =0; i < connect[node].size(); i++){
next = connect[node][i];
if (next.first == parent){
continue;
}
setup(next.first,node,cnt+ssize[node],d+1, connect);
ssize[node] += ssize[next.first];
mini = min(mini, dp[next.first][0] + next.second);
}
if (shop[node]){
mini = 0;
}
dis[node][0] = 0;
dpidx[node][0] = node;
dp[node][0] = mini;
}
void gdp(int node, int parent, int last, vector<vector<pair<int,int>>> &connect){
if (parent != -1){
dis[node][1] = last;
dpidx[node][1] = parent;
dp[node][1] = min(dp[node][0], dp[parent][0] + last);
}
for (int i = 2; i < 18; i++){
if (dpidx[node][i-1] == -1){
break;
}
dp[node][i] = min(dp[node][i-1], dp[dpidx[node][i-1]][i-1] + dis[node][i-1]);
dpidx[node][i] = dpidx[dpidx[node][i-1]][i-1];
dis[node][i] = dis[node][i-1] + dis[dpidx[node][i-1]][i-1];
}
pair<int,int> next;
for (int i =0; i < connect[node].size(); i++){
next = connect[node][i];
if (next.first == parent){
continue;
}
gdp(next.first,node,next.second,connect);
}
return;
}
long long int query(int a, int b){
if (a == b){
return dp[a][0];
}
// cout << a << " " << b << " lel ";
/* for (int i = 0; i < 1e8; i++){
continue;
}*/
for (int i = 0; i < 18; i++){
if ((dpidx[a][i] == -1) || (depth[dpidx[a][i]] < depth[b])){
// cout << i << '\n';
return min(dp[a][i-1], query(dpidx[a][i-1],b) + dis[a][i-1]);
}
}
cout << "UHOH STINKY\n";
return -1;
}
int main()
{
for (int i = 0; i < mxN; i++){
for (int j =0; j < 18; j++){
dp[i][j] = mxV;
dpidx[i][j] = -1;
dis[i][j] = mxV;
}
}
memset(shop,0,sizeof(shop));
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,s,q,e;
cin >> n >> s >> q >> e;
e-=1;
vector<vector<pair<int,int>>> connect(n);
vector<pair<pair<int,int>,int>> edges(n-1);
pair<int,int> temp;
for (int i = 0; i < n-1; i++){
cin >> edges[i].first.first >> edges[i].first.second >> edges[i].second;
edges[i].first.first -= 1;
edges[i].first.second -= 1;
temp.first = edges[i].first.second;
temp.second = edges[i].second;
connect[edges[i].first.first].push_back(temp);
temp.first = edges[i].first.first;
connect[edges[i].first.second].push_back(temp);
}
int a;
for (int i = 0; i < s; i++){
cin >> a;
a -= 1;
shop[a] = true;
}
setup(e,-1,0,0,connect);
gdp(e,-1,0,connect);
int I,R;
int cidx;
for (int i =0; i < 4*1e8; i++){
continue;
}
/* for (int i =0; i < n; i++){
cout << i << ": ";
for (int j = 0; j < 18; j++){
cout << dp[i][j] << " ";
}
cout<< '\n';
}
cout << '\n';
for (int i =0; i < n; i++){
cout << i << ": ";
for (int j = 0; j < 18; j++){
cout << dpidx[i][j] << " ";
}
cout<< '\n';
}
cout << '\n';
for (int i =0; i < n; i++){
cout << i << ": ";
for (int j = 0; j < 18; j++){
cout << dis[i][j] << " ";
}
cout<< '\n';
}
cout << '\n';*/
for (int i =0; i < q; i++){
cin >> I >> R;
I -= 1;
R -= 1;
if (depth[edges[I].first.first] > depth[edges[I].first.second]){
cidx = edges[I].first.first;
} else {
cidx = edges[I].first.second;
}
// cout << nidx[cidx] << " " << ssize[cidx] << " " << nidx[R] << '\n';
if ((nidx[cidx] > nidx[R]) || (nidx[cidx] + ssize[cidx] <= nidx[R])){
cout << "escaped\n";
continue;
}
long long int res = query(R,cidx);
if (res == mxV){
cout << "oo\n";
} else {
cout << res << "\n";
}
}
return 0;
}
Compilation message (stderr)
valley.cpp: In function 'void setup(int, int, int, int, std::vector<std::vector<std::pair<int, int> > >&)':
valley.cpp:20:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for (int i =0; i < connect[node].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~~~~~
valley.cpp: In function 'void gdp(int, int, int, std::vector<std::vector<std::pair<int, int> > >&)':
valley.cpp:55:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for (int i =0; i < connect[node].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |