#include <bits/stdc++.h>
using namespace std;
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key
#define ll long long
#define vi vector<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vvi vector<vi>
#define vll vector<ll>
#define vvll vector<vector<ll>>
#define vpii vector<pair<int,int>>
#define vpll vector<pair<ll,ll>>
int Set(int N, int pos) {return N = N | (1<<pos);}
int Reset(int N, int pos) {return N = N & ~(1<<pos);}
bool Cheek(int N, int pos) {return (bool)(N & (1<<pos));}
#define least_one_pos(x) __builtin_ffs(x)
#define leading_zeros(x) __builtin_clz(x)
#define tailing_zeros(x) __builtin_ctz(x)
#define num_of_one(x) __builtin_popcount(x)
///............x...........///
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define UNIQUE(X) (X).erase(unique(all(X)), (X).end())
#define ft cout << "for test"<<endl;
#define print(v) for (auto it : v)cout << it<<" ";cout << endl;
#define cinv(v) for(auto &it : v)cin>>it;
#define PI acos(-1.0)
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define t_c int test, cs = 1;cin>>test;while (test--)
///................function.....................///
//#define mod 1000000007
//........constant........///
const ll N = 1e6+5;
void file(){
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
}
class CentroidDecomposition{
public:
vector<vector<pii>>adj;
vector<bool>removed;
vector<int>subtree,parent;
map<int,int>Count;
int n,root,k;
ll ans;
CentroidDecomposition(int _n, int _k){
n = _n;
k = _k;
root=1;
adj.resize(n+1);
removed.resize(n+1);
subtree.resize(n+1);
parent.resize(n+1);
ans=INT_MAX;
}
void readTree(int H[][2], int L[]){
for(int i=0; i<n-1; i++){
int a=H[i][0],b=H[i][1];
adj[a].push_back({b,L[i]});
adj[b].push_back({a,L[i]});
}
centroid_decomposition(0);
// cout<<ans<<endl;
}
int get_subtree_size(int node, int par=-1){
subtree[node]=1;
for(auto x:adj[node])
if(!removed[x.first] and par!=x.first)
subtree[node] += get_subtree_size(x.first,node);
return subtree[node];
}
int get_centroid(int node, int desired, int par=-1){
for(auto x:adj[node])
if(!removed[x.first] and x.first!=par and desired<=subtree[x.first])
return get_centroid(x.first,desired,node);
return node;
}
void get_cnt(int nd, int par, int d, int l, bool tp){
if(d>k)return;
// cout<<nd<<" "<<par<<" "<<d<<" "<<l<<" "<<tp<<endl;
if(!tp){
if(Count.find(d)!=Count.end())
Count[d] = min(Count[d],l);
else
Count[d]=l;
// cout<<"add "<<d<<" "<<Count[d]<<endl;
}
else{
if(Count.find(k-d)!=Count.end()){
ans = min(ans,(ll)l+Count[k-d]);
// cout<<k-d<<" update "<<l<<" + "<<Count[k-d]<<endl;
}
}
for(auto x:adj[nd])
if(!removed[x.first] and x.first!=par)
get_cnt(x.first,nd,d+x.second,l+1,tp);
}
void centroid_decomposition(int node){
int centroid = get_centroid(node,get_subtree_size(node)>>1);
removed[centroid]=true;
// cout<<"centroid "<<centroid<<endl;
Count.clear();
Count[0]=0;
for(auto x:adj[centroid]){
if(!removed[x.first]){
get_cnt(x.first,centroid,x.second,1,1);
get_cnt(x.first,centroid,x.second,1,0);
}
}
for(auto x:adj[centroid]){
if(!removed[x.first])
centroid_decomposition(x.first);
}
}
};
int best_path(int N, int K, int H[][2], int L[]){
CentroidDecomposition cd(N,K);
cd.readTree(H,L);
cd.centroid_decomposition(1);
if(cd.ans==INT_MAX)cd.ans=-1;
return cd.ans;
}
Compilation message
race.cpp: In function 'void file()':
race.cpp:41:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | freopen("input.txt","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
race.cpp:42:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | freopen("output.txt","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2648 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2648 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
2648 KB |
Output is correct |
22 |
Correct |
1 ms |
2568 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2500 KB |
Output is correct |
25 |
Correct |
2 ms |
2396 KB |
Output is correct |
26 |
Correct |
2 ms |
2396 KB |
Output is correct |
27 |
Correct |
2 ms |
2396 KB |
Output is correct |
28 |
Correct |
2 ms |
2396 KB |
Output is correct |
29 |
Correct |
2 ms |
2464 KB |
Output is correct |
30 |
Correct |
2 ms |
2396 KB |
Output is correct |
31 |
Correct |
2 ms |
2396 KB |
Output is correct |
32 |
Correct |
2 ms |
2396 KB |
Output is correct |
33 |
Correct |
2 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
2 ms |
2396 KB |
Output is correct |
38 |
Correct |
2 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2648 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
147 ms |
11600 KB |
Output is correct |
20 |
Correct |
143 ms |
12568 KB |
Output is correct |
21 |
Correct |
159 ms |
12260 KB |
Output is correct |
22 |
Correct |
156 ms |
12960 KB |
Output is correct |
23 |
Correct |
57 ms |
13136 KB |
Output is correct |
24 |
Correct |
63 ms |
12960 KB |
Output is correct |
25 |
Correct |
97 ms |
14284 KB |
Output is correct |
26 |
Correct |
90 ms |
18000 KB |
Output is correct |
27 |
Correct |
135 ms |
20052 KB |
Output is correct |
28 |
Correct |
162 ms |
26920 KB |
Output is correct |
29 |
Correct |
170 ms |
26660 KB |
Output is correct |
30 |
Correct |
145 ms |
20564 KB |
Output is correct |
31 |
Correct |
139 ms |
20560 KB |
Output is correct |
32 |
Correct |
142 ms |
20556 KB |
Output is correct |
33 |
Correct |
204 ms |
19552 KB |
Output is correct |
34 |
Correct |
117 ms |
19764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2648 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
2648 KB |
Output is correct |
22 |
Correct |
1 ms |
2568 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2500 KB |
Output is correct |
25 |
Correct |
2 ms |
2396 KB |
Output is correct |
26 |
Correct |
2 ms |
2396 KB |
Output is correct |
27 |
Correct |
2 ms |
2396 KB |
Output is correct |
28 |
Correct |
2 ms |
2396 KB |
Output is correct |
29 |
Correct |
2 ms |
2464 KB |
Output is correct |
30 |
Correct |
2 ms |
2396 KB |
Output is correct |
31 |
Correct |
2 ms |
2396 KB |
Output is correct |
32 |
Correct |
2 ms |
2396 KB |
Output is correct |
33 |
Correct |
2 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
2 ms |
2396 KB |
Output is correct |
38 |
Correct |
2 ms |
2396 KB |
Output is correct |
39 |
Correct |
147 ms |
11600 KB |
Output is correct |
40 |
Correct |
143 ms |
12568 KB |
Output is correct |
41 |
Correct |
159 ms |
12260 KB |
Output is correct |
42 |
Correct |
156 ms |
12960 KB |
Output is correct |
43 |
Correct |
57 ms |
13136 KB |
Output is correct |
44 |
Correct |
63 ms |
12960 KB |
Output is correct |
45 |
Correct |
97 ms |
14284 KB |
Output is correct |
46 |
Correct |
90 ms |
18000 KB |
Output is correct |
47 |
Correct |
135 ms |
20052 KB |
Output is correct |
48 |
Correct |
162 ms |
26920 KB |
Output is correct |
49 |
Correct |
170 ms |
26660 KB |
Output is correct |
50 |
Correct |
145 ms |
20564 KB |
Output is correct |
51 |
Correct |
139 ms |
20560 KB |
Output is correct |
52 |
Correct |
142 ms |
20556 KB |
Output is correct |
53 |
Correct |
204 ms |
19552 KB |
Output is correct |
54 |
Correct |
117 ms |
19764 KB |
Output is correct |
55 |
Correct |
15 ms |
3420 KB |
Output is correct |
56 |
Correct |
11 ms |
3164 KB |
Output is correct |
57 |
Correct |
100 ms |
11956 KB |
Output is correct |
58 |
Correct |
40 ms |
12744 KB |
Output is correct |
59 |
Correct |
236 ms |
21580 KB |
Output is correct |
60 |
Correct |
689 ms |
40240 KB |
Output is correct |
61 |
Correct |
199 ms |
20056 KB |
Output is correct |
62 |
Correct |
230 ms |
20564 KB |
Output is correct |
63 |
Correct |
245 ms |
20628 KB |
Output is correct |
64 |
Correct |
672 ms |
23852 KB |
Output is correct |
65 |
Correct |
115 ms |
21476 KB |
Output is correct |
66 |
Correct |
474 ms |
23620 KB |
Output is correct |
67 |
Correct |
92 ms |
22464 KB |
Output is correct |
68 |
Correct |
284 ms |
24544 KB |
Output is correct |
69 |
Correct |
298 ms |
22352 KB |
Output is correct |
70 |
Correct |
287 ms |
24144 KB |
Output is correct |