#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
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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4960 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
2 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
5028 KB |
Output is correct |
17 |
Correct |
5 ms |
4976 KB |
Output is correct |
18 |
Correct |
3 ms |
5020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4960 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
2 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
5028 KB |
Output is correct |
17 |
Correct |
5 ms |
4976 KB |
Output is correct |
18 |
Correct |
3 ms |
5020 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
2 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
5080 KB |
Output is correct |
22 |
Correct |
3 ms |
5068 KB |
Output is correct |
23 |
Correct |
4 ms |
5068 KB |
Output is correct |
24 |
Correct |
4 ms |
5068 KB |
Output is correct |
25 |
Correct |
4 ms |
5068 KB |
Output is correct |
26 |
Correct |
3 ms |
5068 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
4 ms |
5068 KB |
Output is correct |
29 |
Correct |
4 ms |
5068 KB |
Output is correct |
30 |
Correct |
4 ms |
5068 KB |
Output is correct |
31 |
Correct |
6 ms |
5068 KB |
Output is correct |
32 |
Correct |
4 ms |
5068 KB |
Output is correct |
33 |
Correct |
5 ms |
5068 KB |
Output is correct |
34 |
Correct |
5 ms |
5068 KB |
Output is correct |
35 |
Correct |
4 ms |
5068 KB |
Output is correct |
36 |
Correct |
3 ms |
5068 KB |
Output is correct |
37 |
Correct |
4 ms |
5068 KB |
Output is correct |
38 |
Correct |
4 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4960 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
2 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
5028 KB |
Output is correct |
17 |
Correct |
5 ms |
4976 KB |
Output is correct |
18 |
Correct |
3 ms |
5020 KB |
Output is correct |
19 |
Correct |
255 ms |
10852 KB |
Output is correct |
20 |
Correct |
253 ms |
10780 KB |
Output is correct |
21 |
Correct |
240 ms |
10844 KB |
Output is correct |
22 |
Correct |
247 ms |
10892 KB |
Output is correct |
23 |
Correct |
140 ms |
11012 KB |
Output is correct |
24 |
Correct |
89 ms |
10640 KB |
Output is correct |
25 |
Correct |
189 ms |
17272 KB |
Output is correct |
26 |
Correct |
115 ms |
18700 KB |
Output is correct |
27 |
Correct |
246 ms |
18828 KB |
Output is correct |
28 |
Correct |
537 ms |
32888 KB |
Output is correct |
29 |
Correct |
544 ms |
32712 KB |
Output is correct |
30 |
Correct |
319 ms |
18832 KB |
Output is correct |
31 |
Correct |
287 ms |
18844 KB |
Output is correct |
32 |
Correct |
381 ms |
18936 KB |
Output is correct |
33 |
Correct |
464 ms |
18640 KB |
Output is correct |
34 |
Correct |
364 ms |
19516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4960 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
2 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
5028 KB |
Output is correct |
17 |
Correct |
5 ms |
4976 KB |
Output is correct |
18 |
Correct |
3 ms |
5020 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
2 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
5080 KB |
Output is correct |
22 |
Correct |
3 ms |
5068 KB |
Output is correct |
23 |
Correct |
4 ms |
5068 KB |
Output is correct |
24 |
Correct |
4 ms |
5068 KB |
Output is correct |
25 |
Correct |
4 ms |
5068 KB |
Output is correct |
26 |
Correct |
3 ms |
5068 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
4 ms |
5068 KB |
Output is correct |
29 |
Correct |
4 ms |
5068 KB |
Output is correct |
30 |
Correct |
4 ms |
5068 KB |
Output is correct |
31 |
Correct |
6 ms |
5068 KB |
Output is correct |
32 |
Correct |
4 ms |
5068 KB |
Output is correct |
33 |
Correct |
5 ms |
5068 KB |
Output is correct |
34 |
Correct |
5 ms |
5068 KB |
Output is correct |
35 |
Correct |
4 ms |
5068 KB |
Output is correct |
36 |
Correct |
3 ms |
5068 KB |
Output is correct |
37 |
Correct |
4 ms |
5068 KB |
Output is correct |
38 |
Correct |
4 ms |
5068 KB |
Output is correct |
39 |
Correct |
255 ms |
10852 KB |
Output is correct |
40 |
Correct |
253 ms |
10780 KB |
Output is correct |
41 |
Correct |
240 ms |
10844 KB |
Output is correct |
42 |
Correct |
247 ms |
10892 KB |
Output is correct |
43 |
Correct |
140 ms |
11012 KB |
Output is correct |
44 |
Correct |
89 ms |
10640 KB |
Output is correct |
45 |
Correct |
189 ms |
17272 KB |
Output is correct |
46 |
Correct |
115 ms |
18700 KB |
Output is correct |
47 |
Correct |
246 ms |
18828 KB |
Output is correct |
48 |
Correct |
537 ms |
32888 KB |
Output is correct |
49 |
Correct |
544 ms |
32712 KB |
Output is correct |
50 |
Correct |
319 ms |
18832 KB |
Output is correct |
51 |
Correct |
287 ms |
18844 KB |
Output is correct |
52 |
Correct |
381 ms |
18936 KB |
Output is correct |
53 |
Correct |
464 ms |
18640 KB |
Output is correct |
54 |
Correct |
364 ms |
19516 KB |
Output is correct |
55 |
Correct |
20 ms |
5836 KB |
Output is correct |
56 |
Correct |
15 ms |
5656 KB |
Output is correct |
57 |
Correct |
131 ms |
12356 KB |
Output is correct |
58 |
Correct |
44 ms |
11492 KB |
Output is correct |
59 |
Correct |
301 ms |
22200 KB |
Output is correct |
60 |
Correct |
937 ms |
40976 KB |
Output is correct |
61 |
Correct |
296 ms |
19224 KB |
Output is correct |
62 |
Correct |
298 ms |
18972 KB |
Output is correct |
63 |
Correct |
487 ms |
19012 KB |
Output is correct |
64 |
Correct |
1063 ms |
24356 KB |
Output is correct |
65 |
Correct |
465 ms |
20640 KB |
Output is correct |
66 |
Correct |
755 ms |
32912 KB |
Output is correct |
67 |
Correct |
112 ms |
19540 KB |
Output is correct |
68 |
Correct |
481 ms |
23928 KB |
Output is correct |
69 |
Correct |
465 ms |
24008 KB |
Output is correct |
70 |
Correct |
458 ms |
23180 KB |
Output is correct |