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;
#include "race.h"
#define int long long
// Do everything in global distance because it simplifies work
const int MX = 2e5 + 10;
int ans = 1e9, n, k, timer = 0, d[MX], tin[MX], tout[MX], st[MX], big[MX], siz[MX], edges[MX];
vector<pair<int,int>> adj[MX];
map<int,int> hold;
void dfs2(int i,int p,bool keep){
for(auto [e, w] : adj[i]){
if(e != p && e != big[i]) dfs2(e, i,false);
}
if(big[i] != -1)
dfs2(big[i], i, true);
hold[d[i]] = edges[i];
int total = k + 2*d[i];
if(hold.find(total - d[i]) != hold.end()){
ans = min(ans, hold[total - d[i]] - edges[i]);
}
for(auto [e, w] : adj[i]){
if(e != p && e != big[i]){
for(int tim = tin[e];tim<=tout[e];tim++){
int id = st[tim];
if(hold.find(total - d[id]) != hold.end()){
ans = min(ans, hold[total - d[id]] + edges[id] - 2*edges[i]);
}
}
for(int tim = tin[e];tim<=tout[e];tim++){
int id = st[tim];
hold[d[id]] = min(hold[d[id]], edges[id]);
}
}
}
if(!keep){
hold[d[i]] = 1e8;
for(auto [e,w] : adj[i]){
if(e != p){
for(int tim = tin[e];tim<=tout[e];tim++){
int id = st[tim];
hold[d[id]] = 1e8;
}
}
}
}
}
void dfs1(int i,int p){
int tt = -1;
big[i] = -1;
siz[i] = 1;
st[++timer] = i;
tin[i] = timer;
big[i] = -1;
for(auto [e, w] : adj[i]){
if(e == p)continue;
d[e] = d[i] + w;
edges[e] = edges[i] + 1;
hold[d[e]] = 1e8;
dfs1(e, i);
siz[i] += siz[e];
if(siz[e] > tt){
big[i] = e;
tt = siz[e];
}
}
tout[i] = timer;
}
#undef int long long
int best_path(int N, int K, int H[][2], int L[]){
n = N;
k = K;
for(int i = 0;i<n - 1;i++){
int a = H[i][0],b = H[i][1], w = L[i];
adj[a].push_back({b,w});
adj[b].push_back({a,w});
}
d[0] = 0;
dfs1(0, -1);
dfs2(0,-1,true);
if(ans > N)
ans = -1;
return ans;
}
Compilation message (stderr)
race.cpp:68:12: warning: extra tokens at end of #undef directive
68 | #undef int long long
| ^~~~
race.cpp: In function 'void dfs2(long long int, long long int, bool)':
race.cpp:11:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
11 | for(auto [e, w] : adj[i]){
| ^
race.cpp:21:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
21 | for(auto [e, w] : adj[i]){
| ^
race.cpp:37:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
37 | for(auto [e,w] : adj[i]){
| ^
race.cpp: In function 'void dfs1(long long int, long long int)':
race.cpp:54:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
54 | for(auto [e, w] : adj[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... |