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 "temp.cpp"
#include <cstdio>
using namespace std;
#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif
#define sz(x) (int((x).size()))
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(x) (x).clear()
#define uniq(x) x.resize(unique(all(x)) - x.begin());
#define pb push_back
#define popf pop_front
#define popb pop_back
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(long double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}
template <class T> void print(set <T> v);
template <class T> void print(vector <T> v);
template <class T> void print(multiset <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T, class V> void print(pair <T, V> p);
template <class T, class V> void print(T v[],V n) {cerr << "["; for(int i = 0; i < n; i++) {print(v[i]); cerr << " "; } 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(deque<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'
// for grid problems
int dx[8] = {-1,0,1,0,1,-1,1,-1};
int dy[8] = {0,1,0,-1,1,1,-1,-1};
// lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6
void fastIO() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
}
// file in/out
void setIO(string str = "") {
fastIO();
if (str != "" && str != "input") {
freopen((str + ".in").c_str(), "r", stdin);
freopen((str + ".out").c_str(), "w", stdout);
}
if(str == "input") {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
}
// Indexed Set
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, sz[N], compSize;
long long answ = -1;
vector<pair<int, long long>> adj[N], p;
bool used[N];
map<long long, long long> mp;
int dfs(int node, int parent) {
sz[node] = 1; compSize++;
for(auto i: adj[node]) {
if(used[i.first] || i.first == parent) continue;
sz[node] += dfs(i.first, node);
}
return sz[node];
}
int find_center(int node, int parent) {
for(auto i: adj[node]) {
if (used[i.first] || i.first == parent) continue;
if (2 * sz[i.first] > compSize) {
return find_center(i.first, node);
}
}
return node;
}
int centroid(int u) {
compSize = 0;
dfs(u, 0);
u = find_center(u, 0);
return u;
}
void solve(int node, int parent, long long ln, int kn) {
if(parent) {
p.pb({kn, ln});
}
for(auto i: adj[node]) {
if(i.first == parent || used[i.first]) continue;
solve(i.first, node, ln + i.second, kn + 1);
if(parent) continue;
// exacly k -> Ln
for(auto j: p) {
long long Ln = j.second, Kn = j.first;
if(Ln > k) continue;
if(Ln == k) {
if(answ == -1 || answ > Kn) {
answ = Kn;
}
} else {
auto it = mp.find(k - Ln);
if(it != mp.end()) {
if(answ == -1 || answ > Kn + it->second) {
answ = Kn + it->second;
}
}
}
}
for(auto j: p) {
long long Ln = j.second, Kn = j.first;
if(Ln > k) continue;
auto it = mp.find(Ln);
if(it == mp.end()) {
mp[Ln] = Kn;
} else {
mp[Ln] = min(mp[Ln], Kn);
}
}
p.clear();
}
}
void centroid_decomposition() {
queue<int> q;
q.push(1);
while(!q.empty()) {
auto u = q.front();
q.pop();
int center = centroid(u);
solve(center, 0, 0, 0);
mp.clear();
used[center] = true;
for(auto i: adj[center]) {
if(used[i.first]) continue;
q.push(i.first);
}
}
}
int best_path(int M, int K, int H[][2], int L[]) {
n = M, k = K;
for(int i = 0; i < n - 1; i++) {
int a = H[i][0], b = H[i][1], c = L[i]; a++, b++;
adj[a].push_back({b, c});
adj[b].push_back({a, c});
}
centroid_decomposition();
return answ;
}
Compilation message (stderr)
race.cpp: In function 'void setIO(std::string)':
race.cpp:63:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | freopen((str + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
race.cpp:64:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | freopen((str + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
race.cpp:68:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
race.cpp:69:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |