This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <cstdio>
#include "race.h"
using namespace std;
#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif
void print(long long t) {cerr << t;}
void print(int t) {cerr << t;}
void print(string t) {cerr << t;}
void print(char t) {cerr << t;}
void print(double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}
template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T> void print(T v[],T n) {cerr << "["; for(int i = 0; i < n; i++) {cerr << v[i] << " ";} cerr << "]";}
template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";}
template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'
// lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5
void fastIO() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
}
void setIO(string str) {
fastIO();
freopen((str + ".in").c_str(), "r", stdin);
freopen((str + ".out").c_str(), "w", stdout);
}
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 2e5 + 10;
int n,k, answ = -1, compSize, sz[N];
vector< pair<int, int> > adj[N], mas;
map<int, int> mp;
bool used[N];
int dfs(int node, int parent = 0) {
int sz_ = 1; compSize++;
for(auto i: adj[node]) {
if(i.first ^ parent && !used[i.first]) {
sz_ += dfs(i.first, node);
}
}
sz[node] = sz_;
return sz_;
}
int get_centroid(int node, int parent = 0) {
int mxSz = 0, nd;
for(auto i: adj[node]) {
if(i.first ^ parent && !used[i.first]) {
if(sz[i.first] > mxSz) {
mxSz = sz[i.first];
nd = i.first;
}
}
}
if(mxSz * 2 <= compSize) {
return node;
}
return get_centroid(nd, node);
}
int centroid(int node) {
compSize = 0;
dfs(node);
int center = get_centroid(node);
dfs(center);
return center;
}
void solve(int node, int parent = 0, int l = 0, int d = 0) {
if(parent) {
mas.push_back({l, d});
}
for(auto i: adj[node]) {
if(i.first ^ parent && !used[i.first]) {
solve(i.first, node, l + i.second, d + 1);
if(!parent) {
for(auto j: mas) {
int L = j.first, D = j.second;
if(L > k) {
continue;
}
else if(L == k) {
if(answ == -1 || D < answ) {
answ = D;
}
} else {
if(mp.find(k - L) != mp.end()) {
int ans = mp[k - L] + D;
if(answ == -1 || ans < answ) {
answ = ans;
}
}
}
}
for(auto j: mas) {
int L = j.first, D = j.second;
if(L > k) {
continue;
}
if(mp.find(L) != mp.end()) {
mp[L] = min(D, mp[L]);
} else {
mp[L] = D;
}
}
mas.clear();
}
}
}
}
void centroid_decomposition () {
queue<int> q;
q.push(1);
while(!q.empty()) {
auto u = q.front();
q.pop();
int center = centroid(u);
// dbg(center)
mp.clear();
solve(center);
used[center] = 1;
for(auto i: adj[center]) {
if(!used[i.first] && sz[i.first] != 1) {
q.push(i.first);
}
}
}
}
int best_path(int N, int K, int H[][2], int L[]) {
k = K, n = N;
for(int i = 0; i < N - 1; i++) {
H[i][0]++, H[i][1]++;
adj[ H[i][0] ].push_back({H[i][1], L[i]});
adj[ H[i][1] ].push_back({H[i][0], L[i]});
}
centroid_decomposition();
return answ;
}
Compilation message (stderr)
race.cpp: In function 'void setIO(std::string)':
race.cpp:44:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | freopen((str + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
race.cpp:45:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | freopen((str + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
race.cpp: In function 'int get_centroid(int, int)':
race.cpp:66:19: warning: 'nd' may be used uninitialized in this function [-Wmaybe-uninitialized]
66 | int mxSz = 0, nd;
| ^~| # | 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... |