/**
* Author: Anil Byar
**/
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
// template<class T>
// using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)(x).size()
#define ft first
#define sd second
#define pb push_back
// Source: https://codeforces.com/blog/entry/68809
// { start
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
// } end
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<pll> vll;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef vector<vl> vvl;
#define dbg if(0)
const ll MOD = 1e9+7;
const ll MOD9 = 998244353;
const ll INFLL = 1e18+5;
const int INF = 1e9;
const int maxN = 2e5+5;
const int maxK = 1e6+5;
void printbit(int x, int len) {string s="\n";while(len--){s=((x%2)?'1':'0')+s;x/=2;} cout<<s;}
template<class T>
istream& operator>>(istream&in, vector<T>&v){
for (T& x:v) in>>x;
return in;
}
template<class T>
ostream& operator<<(ostream&out, vector<T>&v){
for (T& x:v) out<<x<<' ';
cout<<'\n';
return out;
}
int mnhighways[maxK];
int ans, k;
struct Edge{
int v, w;
bool operator==(Edge e){
return v==e.v;
}
};
struct Centroid{
std::vector<std::vector<Edge>> adj;
std::vector<int> parent, stree;
const long long INF = 1e9;
Centroid(std::vector<std::vector<Edge>>& _adj):adj(_adj){
parent.resize((int)_adj.size());
stree.resize((int)_adj.size());
}
void build(int node = 1, int par = -1){
int n = dfssize(node, par);
int centroid = dfscentroid(node, par, n);
parent[centroid] = par;
auto tmp = adj[centroid];
adj[centroid].clear();
for (Edge y:tmp){
work(y.v, centroid, y.w, 1);
upd(y.v, centroid, y.w, 1);
}
for (Edge y:tmp){
del(y.v, centroid, y.w, 1);
}
for (Edge y:tmp) {
int x = y.v, w = y.w;
Edge tmp = {centroid, -1};
adj[x].erase(find(all(adj[x]), tmp));
build(x, centroid);
}
}
int dfssize(int node, int par){
stree[node] = 1;
for (Edge y:adj[node]){
int x = y.v;
if (x==par) continue;
dfssize(x, node);
stree[node] += stree[x];
}
return stree[node];
}
int dfscentroid(int node, int par, int n){
for (Edge y:adj[node]){
int x = y.v;
if (x==par) continue;
if (stree[x]>n/2) return dfscentroid(x, node, n);
}
return node;
}
void work(int node, int par, int h, int dep){
if (h>k) return;
ans = min(ans, dep + mnhighways[k-h]);
for (Edge x:adj[node]){
if (x.v==par) continue;
work(x.v, node, h+x.w, dep+1);
}
}
void upd(int node, int par, int h, int dep){
if (h>k) return;
mnhighways[h] = min(mnhighways[h], dep);
for (Edge x:adj[node]){
if (x.v==par) continue;
upd(x.v, node, h+x.w, dep+1);
}
}
void del(int node, int par, int h, int dep){
if (h>k) return;
for (Edge x:adj[node]){
if (x.v==par) continue;
del(x.v, node, h+x.w, dep+1);
}
mnhighways[h] = INF;
}
};
int best_path(int N,int K, int H[][2],int L[]){
vector<vector<Edge>> tadj(N);
for (int i = 0;i<N-1;i++){
int u = H[i][0], v = H[i][1], w = L[i];
tadj[u].pb({v, w});
tadj[v].pb({u, w});
}
k = K;
ans = INF;
memset(mnhighways, 127, sizeof(mnhighways));
mnhighways[0] = 0;
Centroid ctr(tadj);
ctr.build(0, -1);
if (ans==INF) return -1;
return ans;
}
Compilation message
race.cpp: In member function 'void Centroid::build(int, int)':
race.cpp:127:26: warning: unused variable 'w' [-Wunused-variable]
127 | int x = y.v, w = y.w;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
3 |
Correct |
3 ms |
4144 KB |
Output is correct |
4 |
Correct |
2 ms |
4180 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Correct |
2 ms |
4152 KB |
Output is correct |
7 |
Correct |
3 ms |
4180 KB |
Output is correct |
8 |
Correct |
2 ms |
4180 KB |
Output is correct |
9 |
Correct |
2 ms |
4180 KB |
Output is correct |
10 |
Correct |
2 ms |
4180 KB |
Output is correct |
11 |
Correct |
2 ms |
4180 KB |
Output is correct |
12 |
Correct |
2 ms |
4180 KB |
Output is correct |
13 |
Correct |
2 ms |
4180 KB |
Output is correct |
14 |
Correct |
2 ms |
4180 KB |
Output is correct |
15 |
Correct |
2 ms |
4152 KB |
Output is correct |
16 |
Correct |
2 ms |
4180 KB |
Output is correct |
17 |
Correct |
2 ms |
4148 KB |
Output is correct |
18 |
Correct |
2 ms |
4152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
3 |
Correct |
3 ms |
4144 KB |
Output is correct |
4 |
Correct |
2 ms |
4180 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Correct |
2 ms |
4152 KB |
Output is correct |
7 |
Correct |
3 ms |
4180 KB |
Output is correct |
8 |
Correct |
2 ms |
4180 KB |
Output is correct |
9 |
Correct |
2 ms |
4180 KB |
Output is correct |
10 |
Correct |
2 ms |
4180 KB |
Output is correct |
11 |
Correct |
2 ms |
4180 KB |
Output is correct |
12 |
Correct |
2 ms |
4180 KB |
Output is correct |
13 |
Correct |
2 ms |
4180 KB |
Output is correct |
14 |
Correct |
2 ms |
4180 KB |
Output is correct |
15 |
Correct |
2 ms |
4152 KB |
Output is correct |
16 |
Correct |
2 ms |
4180 KB |
Output is correct |
17 |
Correct |
2 ms |
4148 KB |
Output is correct |
18 |
Correct |
2 ms |
4152 KB |
Output is correct |
19 |
Correct |
2 ms |
4180 KB |
Output is correct |
20 |
Correct |
2 ms |
4180 KB |
Output is correct |
21 |
Correct |
3 ms |
4284 KB |
Output is correct |
22 |
Correct |
3 ms |
4308 KB |
Output is correct |
23 |
Correct |
3 ms |
4308 KB |
Output is correct |
24 |
Correct |
2 ms |
4308 KB |
Output is correct |
25 |
Correct |
3 ms |
4296 KB |
Output is correct |
26 |
Correct |
3 ms |
4308 KB |
Output is correct |
27 |
Correct |
3 ms |
4308 KB |
Output is correct |
28 |
Correct |
3 ms |
4316 KB |
Output is correct |
29 |
Correct |
4 ms |
4292 KB |
Output is correct |
30 |
Correct |
3 ms |
4368 KB |
Output is correct |
31 |
Correct |
3 ms |
4308 KB |
Output is correct |
32 |
Correct |
3 ms |
4308 KB |
Output is correct |
33 |
Correct |
4 ms |
4292 KB |
Output is correct |
34 |
Correct |
3 ms |
4308 KB |
Output is correct |
35 |
Correct |
3 ms |
4308 KB |
Output is correct |
36 |
Correct |
4 ms |
4288 KB |
Output is correct |
37 |
Correct |
3 ms |
4308 KB |
Output is correct |
38 |
Correct |
3 ms |
4288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
3 |
Correct |
3 ms |
4144 KB |
Output is correct |
4 |
Correct |
2 ms |
4180 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Correct |
2 ms |
4152 KB |
Output is correct |
7 |
Correct |
3 ms |
4180 KB |
Output is correct |
8 |
Correct |
2 ms |
4180 KB |
Output is correct |
9 |
Correct |
2 ms |
4180 KB |
Output is correct |
10 |
Correct |
2 ms |
4180 KB |
Output is correct |
11 |
Correct |
2 ms |
4180 KB |
Output is correct |
12 |
Correct |
2 ms |
4180 KB |
Output is correct |
13 |
Correct |
2 ms |
4180 KB |
Output is correct |
14 |
Correct |
2 ms |
4180 KB |
Output is correct |
15 |
Correct |
2 ms |
4152 KB |
Output is correct |
16 |
Correct |
2 ms |
4180 KB |
Output is correct |
17 |
Correct |
2 ms |
4148 KB |
Output is correct |
18 |
Correct |
2 ms |
4152 KB |
Output is correct |
19 |
Correct |
134 ms |
18132 KB |
Output is correct |
20 |
Correct |
146 ms |
18064 KB |
Output is correct |
21 |
Correct |
179 ms |
18212 KB |
Output is correct |
22 |
Correct |
141 ms |
18176 KB |
Output is correct |
23 |
Correct |
103 ms |
18220 KB |
Output is correct |
24 |
Correct |
70 ms |
18752 KB |
Output is correct |
25 |
Correct |
190 ms |
19660 KB |
Output is correct |
26 |
Correct |
107 ms |
21348 KB |
Output is correct |
27 |
Correct |
178 ms |
32212 KB |
Output is correct |
28 |
Correct |
466 ms |
38308 KB |
Output is correct |
29 |
Correct |
484 ms |
37656 KB |
Output is correct |
30 |
Correct |
184 ms |
32324 KB |
Output is correct |
31 |
Correct |
167 ms |
32204 KB |
Output is correct |
32 |
Correct |
215 ms |
32192 KB |
Output is correct |
33 |
Correct |
402 ms |
30336 KB |
Output is correct |
34 |
Correct |
341 ms |
30244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
3 |
Correct |
3 ms |
4144 KB |
Output is correct |
4 |
Correct |
2 ms |
4180 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Correct |
2 ms |
4152 KB |
Output is correct |
7 |
Correct |
3 ms |
4180 KB |
Output is correct |
8 |
Correct |
2 ms |
4180 KB |
Output is correct |
9 |
Correct |
2 ms |
4180 KB |
Output is correct |
10 |
Correct |
2 ms |
4180 KB |
Output is correct |
11 |
Correct |
2 ms |
4180 KB |
Output is correct |
12 |
Correct |
2 ms |
4180 KB |
Output is correct |
13 |
Correct |
2 ms |
4180 KB |
Output is correct |
14 |
Correct |
2 ms |
4180 KB |
Output is correct |
15 |
Correct |
2 ms |
4152 KB |
Output is correct |
16 |
Correct |
2 ms |
4180 KB |
Output is correct |
17 |
Correct |
2 ms |
4148 KB |
Output is correct |
18 |
Correct |
2 ms |
4152 KB |
Output is correct |
19 |
Correct |
2 ms |
4180 KB |
Output is correct |
20 |
Correct |
2 ms |
4180 KB |
Output is correct |
21 |
Correct |
3 ms |
4284 KB |
Output is correct |
22 |
Correct |
3 ms |
4308 KB |
Output is correct |
23 |
Correct |
3 ms |
4308 KB |
Output is correct |
24 |
Correct |
2 ms |
4308 KB |
Output is correct |
25 |
Correct |
3 ms |
4296 KB |
Output is correct |
26 |
Correct |
3 ms |
4308 KB |
Output is correct |
27 |
Correct |
3 ms |
4308 KB |
Output is correct |
28 |
Correct |
3 ms |
4316 KB |
Output is correct |
29 |
Correct |
4 ms |
4292 KB |
Output is correct |
30 |
Correct |
3 ms |
4368 KB |
Output is correct |
31 |
Correct |
3 ms |
4308 KB |
Output is correct |
32 |
Correct |
3 ms |
4308 KB |
Output is correct |
33 |
Correct |
4 ms |
4292 KB |
Output is correct |
34 |
Correct |
3 ms |
4308 KB |
Output is correct |
35 |
Correct |
3 ms |
4308 KB |
Output is correct |
36 |
Correct |
4 ms |
4288 KB |
Output is correct |
37 |
Correct |
3 ms |
4308 KB |
Output is correct |
38 |
Correct |
3 ms |
4288 KB |
Output is correct |
39 |
Correct |
134 ms |
18132 KB |
Output is correct |
40 |
Correct |
146 ms |
18064 KB |
Output is correct |
41 |
Correct |
179 ms |
18212 KB |
Output is correct |
42 |
Correct |
141 ms |
18176 KB |
Output is correct |
43 |
Correct |
103 ms |
18220 KB |
Output is correct |
44 |
Correct |
70 ms |
18752 KB |
Output is correct |
45 |
Correct |
190 ms |
19660 KB |
Output is correct |
46 |
Correct |
107 ms |
21348 KB |
Output is correct |
47 |
Correct |
178 ms |
32212 KB |
Output is correct |
48 |
Correct |
466 ms |
38308 KB |
Output is correct |
49 |
Correct |
484 ms |
37656 KB |
Output is correct |
50 |
Correct |
184 ms |
32324 KB |
Output is correct |
51 |
Correct |
167 ms |
32204 KB |
Output is correct |
52 |
Correct |
215 ms |
32192 KB |
Output is correct |
53 |
Correct |
402 ms |
30336 KB |
Output is correct |
54 |
Correct |
341 ms |
30244 KB |
Output is correct |
55 |
Correct |
11 ms |
5716 KB |
Output is correct |
56 |
Correct |
12 ms |
5716 KB |
Output is correct |
57 |
Correct |
94 ms |
19260 KB |
Output is correct |
58 |
Correct |
48 ms |
20428 KB |
Output is correct |
59 |
Correct |
107 ms |
22488 KB |
Output is correct |
60 |
Correct |
862 ms |
40640 KB |
Output is correct |
61 |
Correct |
195 ms |
35076 KB |
Output is correct |
62 |
Correct |
211 ms |
34892 KB |
Output is correct |
63 |
Correct |
289 ms |
34936 KB |
Output is correct |
64 |
Correct |
515 ms |
35008 KB |
Output is correct |
65 |
Correct |
240 ms |
35664 KB |
Output is correct |
66 |
Correct |
680 ms |
39384 KB |
Output is correct |
67 |
Correct |
179 ms |
38164 KB |
Output is correct |
68 |
Correct |
274 ms |
35684 KB |
Output is correct |
69 |
Correct |
281 ms |
36124 KB |
Output is correct |
70 |
Correct |
245 ms |
34312 KB |
Output is correct |