#include<bits/stdc++.h>
#include "race.h"
#define maxn 200005
//#define MAX_N 500000
using namespace std;
int n,k;
vector<pair<int,int> > a[maxn]; //u a[u] su grane susedne sa u
set<pair<int,int> > s[maxn];
int o1[maxn];
int o2[maxn];
int pok[maxn];
int sol;
void dfs(int u,int pr) {
pok[u] = u;
s[u].insert({0,0});
o1[u] = 0;
o2[u] = 0;
int ch = 0;
for(auto edge:a[u]) {
int v=edge.first;
int w=edge.second;
if(v!=pr) {
ch++;
dfs(v,u);
if(s[pok[u]].size()<=s[pok[v]].size()) pok[u]=pok[v];
}
}
if(ch==0) {
return;
}
for(auto edge:a[u]) {
int v=edge.first;
int l=edge.second;
if(v!=pr) {
if(pok[u]==pok[v]) {
o1[pok[u]]+=l;
o2[pok[u]]+=1;
}
}
}
s[pok[u]].insert({-o1[pok[u]],-o2[pok[u]]});
set<pair<int,int> >::iterator it = s[pok[u]].lower_bound({k-o1[pok[u]],-1e9});
if(it!=s[pok[u]].end() && (*it).first==k-o1[pok[u]]) sol=min(sol,(*it).second+o2[pok[u]]);
for(auto edge:a[u]) {
int v=edge.first;
int l=edge.second;
if(v!=pr && pok[v]!=pok[u]) {
for(auto val:s[pok[v]]) {
int d = val.first + o1[pok[v]];
int i = val.second + o2[pok[v]];
set<pair<int,int> >::iterator it = s[pok[u]].lower_bound({k-d-l-o1[pok[u]],-1e9});
if(it!=s[pok[u]].end() && (*it).first==k-d-l-o1[pok[u]]) sol=min(sol,(*it).second+o2[pok[u]]+i+1);
}
for(auto val:s[pok[v]]) {
int d = val.first + o1[pok[v]];
int i = val.second + o2[pok[v]];
s[pok[u]].insert({d+l-o1[pok[u]],i+1-o2[pok[u]]});
//set<pair<int,int> >::iterator it = s[pok[u]].lower_bound({k-d-l-o1[pok[u]],-1e9});
//if(it!=s[pok[u]].end() && (*it).first==k-d-l-o1[pok[u]]) sol=min(sol,(*it).second+o2[pok[u]]+i+1);
}
}
}
}
int best_path(int N, int K, int H[][2], int L[]) {
n=N;
k=K;
for(int i=0;i<n-1;i++) {
a[H[i][0]].push_back({H[i][1],L[i]});
a[H[i][1]].push_back({H[i][0],L[i]});
}
sol=1e9;
dfs(0,-1);
if(sol==1e9) return -1;
else return sol;
}
/*static int N, K;
static int H[MAX_N][2];
static int L[MAX_N];
void read_input()
{
int i;
scanf("%d %d",&N,&K);
for(i=0; i<N-1; i++)
scanf("%d %d %d",&H[i][0],&H[i][1],&L[i]);
}
int main()
{
int ans;
read_input();
ans = best_path(N,K,H,L);
printf("%d",ans);
return 0;
}*/
Compilation message
race.cpp: In function 'void dfs(int, int)':
race.cpp:23:13: warning: unused variable 'w' [-Wunused-variable]
23 | int w=edge.second;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
9 ms |
14348 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14428 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14420 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14420 KB |
Output is correct |
13 |
Correct |
7 ms |
14372 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
9 ms |
14356 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
8 ms |
14412 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
9 ms |
14348 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14428 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14420 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14420 KB |
Output is correct |
13 |
Correct |
7 ms |
14372 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
9 ms |
14356 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
8 ms |
14412 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
8 ms |
14420 KB |
Output is correct |
20 |
Correct |
9 ms |
14420 KB |
Output is correct |
21 |
Correct |
10 ms |
14568 KB |
Output is correct |
22 |
Correct |
9 ms |
14676 KB |
Output is correct |
23 |
Correct |
10 ms |
14668 KB |
Output is correct |
24 |
Correct |
9 ms |
14548 KB |
Output is correct |
25 |
Correct |
9 ms |
14548 KB |
Output is correct |
26 |
Correct |
10 ms |
14580 KB |
Output is correct |
27 |
Correct |
11 ms |
14548 KB |
Output is correct |
28 |
Correct |
8 ms |
14668 KB |
Output is correct |
29 |
Correct |
9 ms |
14672 KB |
Output is correct |
30 |
Correct |
10 ms |
14548 KB |
Output is correct |
31 |
Correct |
8 ms |
14548 KB |
Output is correct |
32 |
Correct |
9 ms |
14664 KB |
Output is correct |
33 |
Correct |
9 ms |
14676 KB |
Output is correct |
34 |
Correct |
8 ms |
14604 KB |
Output is correct |
35 |
Correct |
8 ms |
14548 KB |
Output is correct |
36 |
Correct |
8 ms |
14536 KB |
Output is correct |
37 |
Correct |
8 ms |
14548 KB |
Output is correct |
38 |
Correct |
9 ms |
14536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
9 ms |
14348 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14428 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14420 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14420 KB |
Output is correct |
13 |
Correct |
7 ms |
14372 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
9 ms |
14356 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
8 ms |
14412 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
130 ms |
36348 KB |
Output is correct |
20 |
Correct |
134 ms |
36140 KB |
Output is correct |
21 |
Correct |
141 ms |
35844 KB |
Output is correct |
22 |
Correct |
141 ms |
34992 KB |
Output is correct |
23 |
Correct |
151 ms |
46360 KB |
Output is correct |
24 |
Correct |
118 ms |
36364 KB |
Output is correct |
25 |
Correct |
122 ms |
41052 KB |
Output is correct |
26 |
Correct |
79 ms |
46280 KB |
Output is correct |
27 |
Correct |
237 ms |
46176 KB |
Output is correct |
28 |
Correct |
255 ms |
78368 KB |
Output is correct |
29 |
Correct |
254 ms |
76768 KB |
Output is correct |
30 |
Correct |
189 ms |
46292 KB |
Output is correct |
31 |
Correct |
195 ms |
46184 KB |
Output is correct |
32 |
Correct |
268 ms |
46360 KB |
Output is correct |
33 |
Correct |
204 ms |
47248 KB |
Output is correct |
34 |
Correct |
309 ms |
72208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
9 ms |
14348 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14428 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14420 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14420 KB |
Output is correct |
13 |
Correct |
7 ms |
14372 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
9 ms |
14356 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
8 ms |
14412 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
8 ms |
14420 KB |
Output is correct |
20 |
Correct |
9 ms |
14420 KB |
Output is correct |
21 |
Correct |
10 ms |
14568 KB |
Output is correct |
22 |
Correct |
9 ms |
14676 KB |
Output is correct |
23 |
Correct |
10 ms |
14668 KB |
Output is correct |
24 |
Correct |
9 ms |
14548 KB |
Output is correct |
25 |
Correct |
9 ms |
14548 KB |
Output is correct |
26 |
Correct |
10 ms |
14580 KB |
Output is correct |
27 |
Correct |
11 ms |
14548 KB |
Output is correct |
28 |
Correct |
8 ms |
14668 KB |
Output is correct |
29 |
Correct |
9 ms |
14672 KB |
Output is correct |
30 |
Correct |
10 ms |
14548 KB |
Output is correct |
31 |
Correct |
8 ms |
14548 KB |
Output is correct |
32 |
Correct |
9 ms |
14664 KB |
Output is correct |
33 |
Correct |
9 ms |
14676 KB |
Output is correct |
34 |
Correct |
8 ms |
14604 KB |
Output is correct |
35 |
Correct |
8 ms |
14548 KB |
Output is correct |
36 |
Correct |
8 ms |
14536 KB |
Output is correct |
37 |
Correct |
8 ms |
14548 KB |
Output is correct |
38 |
Correct |
9 ms |
14536 KB |
Output is correct |
39 |
Correct |
130 ms |
36348 KB |
Output is correct |
40 |
Correct |
134 ms |
36140 KB |
Output is correct |
41 |
Correct |
141 ms |
35844 KB |
Output is correct |
42 |
Correct |
141 ms |
34992 KB |
Output is correct |
43 |
Correct |
151 ms |
46360 KB |
Output is correct |
44 |
Correct |
118 ms |
36364 KB |
Output is correct |
45 |
Correct |
122 ms |
41052 KB |
Output is correct |
46 |
Correct |
79 ms |
46280 KB |
Output is correct |
47 |
Correct |
237 ms |
46176 KB |
Output is correct |
48 |
Correct |
255 ms |
78368 KB |
Output is correct |
49 |
Correct |
254 ms |
76768 KB |
Output is correct |
50 |
Correct |
189 ms |
46292 KB |
Output is correct |
51 |
Correct |
195 ms |
46184 KB |
Output is correct |
52 |
Correct |
268 ms |
46360 KB |
Output is correct |
53 |
Correct |
204 ms |
47248 KB |
Output is correct |
54 |
Correct |
309 ms |
72208 KB |
Output is correct |
55 |
Correct |
19 ms |
17100 KB |
Output is correct |
56 |
Correct |
15 ms |
16204 KB |
Output is correct |
57 |
Correct |
74 ms |
32744 KB |
Output is correct |
58 |
Correct |
48 ms |
26340 KB |
Output is correct |
59 |
Correct |
73 ms |
46212 KB |
Output is correct |
60 |
Correct |
246 ms |
77264 KB |
Output is correct |
61 |
Correct |
218 ms |
50508 KB |
Output is correct |
62 |
Correct |
184 ms |
46272 KB |
Output is correct |
63 |
Correct |
241 ms |
46308 KB |
Output is correct |
64 |
Correct |
388 ms |
87536 KB |
Output is correct |
65 |
Correct |
379 ms |
82636 KB |
Output is correct |
66 |
Correct |
266 ms |
73284 KB |
Output is correct |
67 |
Correct |
173 ms |
39864 KB |
Output is correct |
68 |
Correct |
310 ms |
57932 KB |
Output is correct |
69 |
Correct |
310 ms |
58296 KB |
Output is correct |
70 |
Correct |
289 ms |
55844 KB |
Output is correct |