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
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);
}
// file in/out
void setIO(string str = "") {
fastIO();
if(str != "") {
freopen((str + ".in").c_str(), "r", stdin);
freopen((str + ".out").c_str(), "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, answ = -1, sz[N], compSize;
vector<pair<int, int>> adj[N], mas;
bool used[N];
map<int, int> mp;
int dfs(int node = 1,int parent = 0) {
sz[node] = 1, compSize++;
for(auto i: adj[node]) {
if(i.first != parent && !used[i.first]) {
sz[node] += dfs(i.first, node);
}
}
return sz[node];
}
int find_center(int node, int parent = 0) {
for(auto i: adj[node]) {
if(i.first != parent && !used[i.first]) {
if(sz[i.first] * 2 > compSize) {
return find_center(i.first, node);
}
}
}
return node;
}
int centroid(int a) {
compSize = 0;
dfs(a);
int center = find_center(a);
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;
if(L == k) {
if(answ == -1 || answ > D) {
answ = D;
}
} else {
if(mp.find(k - L) != mp.end()) {
int ans = mp[k - L] + D;
if(answ == -1 || answ > ans) {
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(mp[L], D);
} else {
mp[L] = D;
}
}
mas.clear();
}
}
}
}
void centroid_decomposition() {
queue <int> q;
q.push(1);
while(!q.empty()) {
auto a = q.front();
q.pop();
int center = centroid(a);
solve(center);
mp.clear();
used[center] = 1;
for(auto i: adj[center]) {
if(!used[i.first] && sz[i.first] != 1) {
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;
}
//
//int main () {
// cin >> n >> k;
//
// for(int i = 0; i < n - 1; i++) {
// int a, b, c; cin >> a >> b >> c, a++, b++;
// adj[a].push_back({b, c});
// adj[b].push_back({a, c});
// }
//
// centroid_decomposition();
//
// cout << answ << endl;
// return 0;
//}
Compilation message (stderr)
race.cpp: In function 'void setIO(std::string)':
race.cpp:46:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | freopen((str + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
race.cpp:47:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | freopen((str + ".out").c_str(), "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... |