#include "race.h"
#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
const int N=2e5+5;
vector< pair<int,int> > adj[N];
int n,k, ans=(1<<30);
int sz[N], del[N], nn;
void dfs0(int node, int pa=-1)
{
sz[node]=1;
nn++;
//for(auto it:adj[node])
for(int i=0;i<adj[node].size();i++)
{
auto it=adj[node][i];
if(del[it.F])
{
swap(adj[node][i],adj[node].back());
adj[node].pop_back();
i--; continue;
}
if(del[it.F] || it.F==pa) continue;
dfs0(it.F, node);
sz[node]+=sz[it.F];
}
}
int find_cent(int node,int pa=-1)
{
for(auto it:adj[node])
{
if(del[it.F]||it.F==pa) continue;
if(sz[it.F]>nn/2) return find_cent(it.F, node);
}
return node;
}
map<long long,multiset<int>> co;
void dfs1(int node, int pa, long long len, int h, int add)
{
if(add)
co[len].insert(h);
else
co[len].erase(co[len].find(h));
for(auto it:adj[node])
{
if(del[it.F]||it.F==pa) continue;
dfs1(it.F, node, len+it.S, h+1, add);
}
}
void dfs2(int node, int pa, long long len, int h)
{
if(co.count(k-len))
{
if(co[k-len].size())
ans=min(ans,h+(*co[k-len].begin()));
}
for(auto it:adj[node])
{
if(del[it.F]||it.F==pa) continue;
dfs2(it.F, node, len+it.S, h+1);
}
}
void dfs_cent(int root)
{
nn=0; co.clear();
dfs0(root);
int cent=find_cent(root);
dfs1(cent, -1, 0, 0, 1);
for(auto it:adj[cent])
{
if(del[it.F]) continue;
dfs1(it.F, cent, it.S, 1, 0);
dfs2(it.F, cent, it.S, 1);
}
del[cent]=1;
for(auto it:adj[cent])
{
if(!del[it.F])
dfs_cent(it.F);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n=N; k=K;
for(int i=0;i<n-1;i++)
{
adj[H[i][0]].push_back({H[i][1], L[i]});
adj[H[i][1]].push_back({H[i][0], L[i]});
}
dfs_cent(0);
return (ans==(1<<30)?-1:ans);
}
Compilation message
race.cpp: In function 'void dfs0(int, int)':
race.cpp:19:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | for(int i=0;i<adj[node].size();i++)
| ~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10332 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10336 KB |
Output is correct |
4 |
Correct |
3 ms |
10332 KB |
Output is correct |
5 |
Correct |
2 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10332 KB |
Output is correct |
7 |
Correct |
2 ms |
10332 KB |
Output is correct |
8 |
Correct |
2 ms |
10332 KB |
Output is correct |
9 |
Correct |
2 ms |
10332 KB |
Output is correct |
10 |
Correct |
3 ms |
10332 KB |
Output is correct |
11 |
Correct |
2 ms |
10332 KB |
Output is correct |
12 |
Correct |
2 ms |
10332 KB |
Output is correct |
13 |
Correct |
2 ms |
10332 KB |
Output is correct |
14 |
Correct |
2 ms |
10332 KB |
Output is correct |
15 |
Correct |
2 ms |
10332 KB |
Output is correct |
16 |
Correct |
2 ms |
10332 KB |
Output is correct |
17 |
Correct |
2 ms |
10332 KB |
Output is correct |
18 |
Correct |
2 ms |
10332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10332 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10336 KB |
Output is correct |
4 |
Correct |
3 ms |
10332 KB |
Output is correct |
5 |
Correct |
2 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10332 KB |
Output is correct |
7 |
Correct |
2 ms |
10332 KB |
Output is correct |
8 |
Correct |
2 ms |
10332 KB |
Output is correct |
9 |
Correct |
2 ms |
10332 KB |
Output is correct |
10 |
Correct |
3 ms |
10332 KB |
Output is correct |
11 |
Correct |
2 ms |
10332 KB |
Output is correct |
12 |
Correct |
2 ms |
10332 KB |
Output is correct |
13 |
Correct |
2 ms |
10332 KB |
Output is correct |
14 |
Correct |
2 ms |
10332 KB |
Output is correct |
15 |
Correct |
2 ms |
10332 KB |
Output is correct |
16 |
Correct |
2 ms |
10332 KB |
Output is correct |
17 |
Correct |
2 ms |
10332 KB |
Output is correct |
18 |
Correct |
2 ms |
10332 KB |
Output is correct |
19 |
Correct |
2 ms |
10332 KB |
Output is correct |
20 |
Correct |
2 ms |
10332 KB |
Output is correct |
21 |
Correct |
3 ms |
10332 KB |
Output is correct |
22 |
Correct |
4 ms |
10332 KB |
Output is correct |
23 |
Correct |
4 ms |
10332 KB |
Output is correct |
24 |
Correct |
4 ms |
10332 KB |
Output is correct |
25 |
Correct |
4 ms |
10332 KB |
Output is correct |
26 |
Correct |
4 ms |
10332 KB |
Output is correct |
27 |
Correct |
4 ms |
10332 KB |
Output is correct |
28 |
Correct |
4 ms |
10332 KB |
Output is correct |
29 |
Correct |
3 ms |
10332 KB |
Output is correct |
30 |
Correct |
4 ms |
10332 KB |
Output is correct |
31 |
Correct |
4 ms |
10328 KB |
Output is correct |
32 |
Correct |
4 ms |
10332 KB |
Output is correct |
33 |
Correct |
4 ms |
10332 KB |
Output is correct |
34 |
Correct |
4 ms |
10328 KB |
Output is correct |
35 |
Correct |
5 ms |
10332 KB |
Output is correct |
36 |
Correct |
4 ms |
10332 KB |
Output is correct |
37 |
Correct |
4 ms |
10332 KB |
Output is correct |
38 |
Correct |
4 ms |
10332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10332 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10336 KB |
Output is correct |
4 |
Correct |
3 ms |
10332 KB |
Output is correct |
5 |
Correct |
2 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10332 KB |
Output is correct |
7 |
Correct |
2 ms |
10332 KB |
Output is correct |
8 |
Correct |
2 ms |
10332 KB |
Output is correct |
9 |
Correct |
2 ms |
10332 KB |
Output is correct |
10 |
Correct |
3 ms |
10332 KB |
Output is correct |
11 |
Correct |
2 ms |
10332 KB |
Output is correct |
12 |
Correct |
2 ms |
10332 KB |
Output is correct |
13 |
Correct |
2 ms |
10332 KB |
Output is correct |
14 |
Correct |
2 ms |
10332 KB |
Output is correct |
15 |
Correct |
2 ms |
10332 KB |
Output is correct |
16 |
Correct |
2 ms |
10332 KB |
Output is correct |
17 |
Correct |
2 ms |
10332 KB |
Output is correct |
18 |
Correct |
2 ms |
10332 KB |
Output is correct |
19 |
Correct |
342 ms |
20820 KB |
Output is correct |
20 |
Correct |
335 ms |
20908 KB |
Output is correct |
21 |
Correct |
337 ms |
20736 KB |
Output is correct |
22 |
Correct |
302 ms |
20820 KB |
Output is correct |
23 |
Correct |
440 ms |
21024 KB |
Output is correct |
24 |
Correct |
186 ms |
21088 KB |
Output is correct |
25 |
Correct |
302 ms |
26360 KB |
Output is correct |
26 |
Correct |
206 ms |
29780 KB |
Output is correct |
27 |
Correct |
404 ms |
32336 KB |
Output is correct |
28 |
Correct |
1170 ms |
65200 KB |
Output is correct |
29 |
Correct |
1152 ms |
64284 KB |
Output is correct |
30 |
Correct |
364 ms |
32336 KB |
Output is correct |
31 |
Correct |
371 ms |
32596 KB |
Output is correct |
32 |
Correct |
417 ms |
32336 KB |
Output is correct |
33 |
Correct |
656 ms |
31124 KB |
Output is correct |
34 |
Correct |
1121 ms |
50004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10332 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10336 KB |
Output is correct |
4 |
Correct |
3 ms |
10332 KB |
Output is correct |
5 |
Correct |
2 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10332 KB |
Output is correct |
7 |
Correct |
2 ms |
10332 KB |
Output is correct |
8 |
Correct |
2 ms |
10332 KB |
Output is correct |
9 |
Correct |
2 ms |
10332 KB |
Output is correct |
10 |
Correct |
3 ms |
10332 KB |
Output is correct |
11 |
Correct |
2 ms |
10332 KB |
Output is correct |
12 |
Correct |
2 ms |
10332 KB |
Output is correct |
13 |
Correct |
2 ms |
10332 KB |
Output is correct |
14 |
Correct |
2 ms |
10332 KB |
Output is correct |
15 |
Correct |
2 ms |
10332 KB |
Output is correct |
16 |
Correct |
2 ms |
10332 KB |
Output is correct |
17 |
Correct |
2 ms |
10332 KB |
Output is correct |
18 |
Correct |
2 ms |
10332 KB |
Output is correct |
19 |
Correct |
2 ms |
10332 KB |
Output is correct |
20 |
Correct |
2 ms |
10332 KB |
Output is correct |
21 |
Correct |
3 ms |
10332 KB |
Output is correct |
22 |
Correct |
4 ms |
10332 KB |
Output is correct |
23 |
Correct |
4 ms |
10332 KB |
Output is correct |
24 |
Correct |
4 ms |
10332 KB |
Output is correct |
25 |
Correct |
4 ms |
10332 KB |
Output is correct |
26 |
Correct |
4 ms |
10332 KB |
Output is correct |
27 |
Correct |
4 ms |
10332 KB |
Output is correct |
28 |
Correct |
4 ms |
10332 KB |
Output is correct |
29 |
Correct |
3 ms |
10332 KB |
Output is correct |
30 |
Correct |
4 ms |
10332 KB |
Output is correct |
31 |
Correct |
4 ms |
10328 KB |
Output is correct |
32 |
Correct |
4 ms |
10332 KB |
Output is correct |
33 |
Correct |
4 ms |
10332 KB |
Output is correct |
34 |
Correct |
4 ms |
10328 KB |
Output is correct |
35 |
Correct |
5 ms |
10332 KB |
Output is correct |
36 |
Correct |
4 ms |
10332 KB |
Output is correct |
37 |
Correct |
4 ms |
10332 KB |
Output is correct |
38 |
Correct |
4 ms |
10332 KB |
Output is correct |
39 |
Correct |
342 ms |
20820 KB |
Output is correct |
40 |
Correct |
335 ms |
20908 KB |
Output is correct |
41 |
Correct |
337 ms |
20736 KB |
Output is correct |
42 |
Correct |
302 ms |
20820 KB |
Output is correct |
43 |
Correct |
440 ms |
21024 KB |
Output is correct |
44 |
Correct |
186 ms |
21088 KB |
Output is correct |
45 |
Correct |
302 ms |
26360 KB |
Output is correct |
46 |
Correct |
206 ms |
29780 KB |
Output is correct |
47 |
Correct |
404 ms |
32336 KB |
Output is correct |
48 |
Correct |
1170 ms |
65200 KB |
Output is correct |
49 |
Correct |
1152 ms |
64284 KB |
Output is correct |
50 |
Correct |
364 ms |
32336 KB |
Output is correct |
51 |
Correct |
371 ms |
32596 KB |
Output is correct |
52 |
Correct |
417 ms |
32336 KB |
Output is correct |
53 |
Correct |
656 ms |
31124 KB |
Output is correct |
54 |
Correct |
1121 ms |
50004 KB |
Output is correct |
55 |
Correct |
27 ms |
11868 KB |
Output is correct |
56 |
Correct |
22 ms |
11124 KB |
Output is correct |
57 |
Correct |
304 ms |
22268 KB |
Output is correct |
58 |
Correct |
67 ms |
22204 KB |
Output is correct |
59 |
Correct |
394 ms |
36764 KB |
Output is correct |
60 |
Correct |
1199 ms |
63316 KB |
Output is correct |
61 |
Correct |
424 ms |
35432 KB |
Output is correct |
62 |
Correct |
399 ms |
32644 KB |
Output is correct |
63 |
Correct |
451 ms |
32856 KB |
Output is correct |
64 |
Correct |
1328 ms |
41856 KB |
Output is correct |
65 |
Correct |
1142 ms |
51812 KB |
Output is correct |
66 |
Correct |
1209 ms |
61964 KB |
Output is correct |
67 |
Correct |
170 ms |
33092 KB |
Output is correct |
68 |
Correct |
649 ms |
49004 KB |
Output is correct |
69 |
Correct |
647 ms |
49492 KB |
Output is correct |
70 |
Correct |
593 ms |
47432 KB |
Output is correct |