#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
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 |
1 |
Correct |
15 ms |
35752 KB |
Output is correct |
2 |
Correct |
15 ms |
35720 KB |
Output is correct |
3 |
Correct |
17 ms |
35788 KB |
Output is correct |
4 |
Correct |
14 ms |
35796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
35752 KB |
Output is correct |
2 |
Correct |
15 ms |
35720 KB |
Output is correct |
3 |
Correct |
17 ms |
35788 KB |
Output is correct |
4 |
Correct |
14 ms |
35796 KB |
Output is correct |
5 |
Correct |
15 ms |
35736 KB |
Output is correct |
6 |
Correct |
14 ms |
35664 KB |
Output is correct |
7 |
Correct |
14 ms |
35796 KB |
Output is correct |
8 |
Correct |
14 ms |
35668 KB |
Output is correct |
9 |
Correct |
14 ms |
35668 KB |
Output is correct |
10 |
Correct |
15 ms |
35736 KB |
Output is correct |
11 |
Correct |
15 ms |
35740 KB |
Output is correct |
12 |
Correct |
14 ms |
35668 KB |
Output is correct |
13 |
Correct |
17 ms |
35788 KB |
Output is correct |
14 |
Correct |
15 ms |
35776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
108 ms |
48648 KB |
Output is correct |
2 |
Correct |
127 ms |
48328 KB |
Output is correct |
3 |
Correct |
132 ms |
48256 KB |
Output is correct |
4 |
Correct |
156 ms |
50416 KB |
Output is correct |
5 |
Correct |
135 ms |
50604 KB |
Output is correct |
6 |
Correct |
176 ms |
52864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
35752 KB |
Output is correct |
2 |
Correct |
15 ms |
35720 KB |
Output is correct |
3 |
Correct |
17 ms |
35788 KB |
Output is correct |
4 |
Correct |
14 ms |
35796 KB |
Output is correct |
5 |
Correct |
15 ms |
35736 KB |
Output is correct |
6 |
Correct |
14 ms |
35664 KB |
Output is correct |
7 |
Correct |
14 ms |
35796 KB |
Output is correct |
8 |
Correct |
14 ms |
35668 KB |
Output is correct |
9 |
Correct |
14 ms |
35668 KB |
Output is correct |
10 |
Correct |
15 ms |
35736 KB |
Output is correct |
11 |
Correct |
15 ms |
35740 KB |
Output is correct |
12 |
Correct |
14 ms |
35668 KB |
Output is correct |
13 |
Correct |
17 ms |
35788 KB |
Output is correct |
14 |
Correct |
15 ms |
35776 KB |
Output is correct |
15 |
Correct |
108 ms |
48648 KB |
Output is correct |
16 |
Correct |
127 ms |
48328 KB |
Output is correct |
17 |
Correct |
132 ms |
48256 KB |
Output is correct |
18 |
Correct |
156 ms |
50416 KB |
Output is correct |
19 |
Correct |
135 ms |
50604 KB |
Output is correct |
20 |
Correct |
176 ms |
52864 KB |
Output is correct |
21 |
Correct |
98 ms |
48204 KB |
Output is correct |
22 |
Correct |
111 ms |
47836 KB |
Output is correct |
23 |
Correct |
123 ms |
47848 KB |
Output is correct |
24 |
Correct |
149 ms |
50252 KB |
Output is correct |
25 |
Correct |
177 ms |
53596 KB |
Output is correct |
26 |
Correct |
99 ms |
48256 KB |
Output is correct |
27 |
Correct |
113 ms |
47812 KB |
Output is correct |
28 |
Correct |
123 ms |
47808 KB |
Output is correct |
29 |
Correct |
162 ms |
49484 KB |
Output is correct |
30 |
Correct |
165 ms |
51080 KB |
Output is correct |
31 |
Correct |
102 ms |
48204 KB |
Output is correct |
32 |
Correct |
117 ms |
48000 KB |
Output is correct |
33 |
Correct |
135 ms |
48012 KB |
Output is correct |
34 |
Correct |
159 ms |
50124 KB |
Output is correct |
35 |
Correct |
155 ms |
53392 KB |
Output is correct |
36 |
Correct |
144 ms |
50252 KB |
Output is correct |