#include "race.h"
#include <vector>
#include <iostream>
#include <set>
#include <cstdlib>
using namespace std;
const int maxN = 200'000;
int N;
long long K;
vector<int> edge[1+maxN+1];
vector<long long> dist[1+maxN+1];
int ans = maxN;
vector<int> parent(1+maxN+1, maxN);
vector<int> subtree(1+maxN+1, 1);
int curr_size = 0;
vector<int> inaccessible(1+maxN+1, 0);
void dfs(int u, int p, int b)
{
// cerr << "normal dfs " << u << ' ' << p << ' ' << b << '\n';
curr_size++;
parent[u] = p;
subtree[u] = 1;
for(int v: edge[u])
{
if(inaccessible[v]) continue;
if(v == p || v == b) continue;
dfs(v, u, b);
subtree[u] += subtree[v];
}
}
int getSize(int u, int v)
{
if(parent[v] == u) return subtree[v];
else return curr_size - subtree[u];
}
vector< pair<long long, int> > dist_list[1+maxN+1];
int error_ct = 0;
void dfs2(int u, int p, int r, int b, long long d, int e) //parent, root, banned
{
// error_ct++;
// if(error_ct == 100) exit(0);
// cerr << "dfs2 " << u << ' ' << p << ' ' << r << ' ' << b << ' ' << d << ' ' << e << '\n';
dist_list[r].push_back(make_pair(d, e));
for(int z = 0; z < int(edge[u].size()); z++)
{
int v = edge[u][z];
long long w = dist[u][z];
if(v == p || v == b) continue;
if(inaccessible[v]) continue;
dfs2(v, u, r, b, d+w, e+1);
}
}
multiset< pair<long long, int> > X;
void solve(int u, int p)
{
// cerr << "\n\n\n\n\n";
// cerr << "solve " << u << ' ' << p << '\n';
curr_size = 0;
dfs(u, p, p);
int c = u;
while(1)
{
bool flag = 0;
for(int v: edge[c])
{
if(inaccessible[v]) continue;
if(v == p) continue;
if(2*getSize(c, v) > curr_size)
{
c = v;
flag = 1;
break;
}
}
if(!flag) break;
}
// cerr << "c = " << c << '\n';
inaccessible[c]++;
// cerr << "don't access " << c << '\n';
X.clear();
for(int e = 0; e < int(edge[c].size()); e++)
{
int v = edge[c][e];
long long w = dist[c][e];
if(v == p) continue;
if(inaccessible[v]) continue;
dist_list[v].clear();
dfs2(v, c, v, p, w, 1);
for(auto x: dist_list[v]) X.insert(x);
// cerr << c << " -> " << v << ": ";
// for(auto x: dist_list[v]) cerr << x.first <<","<<x.second << " | ";
// cerr << '\n';
}
auto y1 = X.lower_bound(make_pair(K, -1));
if(y1 != X.end() && y1->first == K) ans = min(ans, y1->second);
for(int e = 0; e < int(edge[c].size()); e++)
{
int v = edge[c][e];
long long w = dist[c][e];
if(v == p) continue;
if(inaccessible[v]) continue;
for(auto x: dist_list[v]) X.erase(X.find(x));
for(auto x: dist_list[v])
{
auto y = X.lower_bound(make_pair(K - x.first, -1));
if(y == X.end()) continue;
if(y->first + x.first != K) continue;
ans = min(ans, y->second + x.second);
}
for(auto x: dist_list[v]) X.insert(x);
}
for(int v: edge[c])
{
if(v == p) continue;
if(inaccessible[v]) continue;
solve(v, c);
}
inaccessible[c]--;
// cerr << "do access " << c << '\n';
}
int best_path(int N_, int K_, int H_[][2], int L_[])
{
N = N_;
K = K_;
for(int e = 0; e < N-1; e++)
{
edge[H_[e][0]].push_back(H_[e][1]);
dist[H_[e][0]].push_back(L_[e]);
edge[H_[e][1]].push_back(H_[e][0]);
dist[H_[e][1]].push_back(L_[e]);
}
solve(0, maxN); //solve for paths belonging to centroid subtree of u
if(ans == maxN) ans = -1;
return ans;
}
Compilation message
race.cpp: In function 'void solve(int, int)':
race.cpp:129:19: warning: unused variable 'w' [-Wunused-variable]
129 | long long w = dist[c][e];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16716 KB |
Output is correct |
2 |
Correct |
9 ms |
16716 KB |
Output is correct |
3 |
Correct |
10 ms |
16716 KB |
Output is correct |
4 |
Correct |
9 ms |
16684 KB |
Output is correct |
5 |
Correct |
9 ms |
16716 KB |
Output is correct |
6 |
Correct |
9 ms |
16716 KB |
Output is correct |
7 |
Correct |
9 ms |
16748 KB |
Output is correct |
8 |
Correct |
9 ms |
16760 KB |
Output is correct |
9 |
Correct |
10 ms |
16768 KB |
Output is correct |
10 |
Correct |
11 ms |
16744 KB |
Output is correct |
11 |
Correct |
9 ms |
16716 KB |
Output is correct |
12 |
Correct |
9 ms |
16800 KB |
Output is correct |
13 |
Correct |
9 ms |
16760 KB |
Output is correct |
14 |
Correct |
11 ms |
16764 KB |
Output is correct |
15 |
Correct |
11 ms |
16760 KB |
Output is correct |
16 |
Correct |
10 ms |
16716 KB |
Output is correct |
17 |
Correct |
11 ms |
16704 KB |
Output is correct |
18 |
Correct |
9 ms |
16772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16716 KB |
Output is correct |
2 |
Correct |
9 ms |
16716 KB |
Output is correct |
3 |
Correct |
10 ms |
16716 KB |
Output is correct |
4 |
Correct |
9 ms |
16684 KB |
Output is correct |
5 |
Correct |
9 ms |
16716 KB |
Output is correct |
6 |
Correct |
9 ms |
16716 KB |
Output is correct |
7 |
Correct |
9 ms |
16748 KB |
Output is correct |
8 |
Correct |
9 ms |
16760 KB |
Output is correct |
9 |
Correct |
10 ms |
16768 KB |
Output is correct |
10 |
Correct |
11 ms |
16744 KB |
Output is correct |
11 |
Correct |
9 ms |
16716 KB |
Output is correct |
12 |
Correct |
9 ms |
16800 KB |
Output is correct |
13 |
Correct |
9 ms |
16760 KB |
Output is correct |
14 |
Correct |
11 ms |
16764 KB |
Output is correct |
15 |
Correct |
11 ms |
16760 KB |
Output is correct |
16 |
Correct |
10 ms |
16716 KB |
Output is correct |
17 |
Correct |
11 ms |
16704 KB |
Output is correct |
18 |
Correct |
9 ms |
16772 KB |
Output is correct |
19 |
Correct |
8 ms |
16716 KB |
Output is correct |
20 |
Correct |
8 ms |
16716 KB |
Output is correct |
21 |
Correct |
11 ms |
16844 KB |
Output is correct |
22 |
Correct |
10 ms |
16956 KB |
Output is correct |
23 |
Correct |
11 ms |
16880 KB |
Output is correct |
24 |
Correct |
14 ms |
16892 KB |
Output is correct |
25 |
Correct |
11 ms |
16844 KB |
Output is correct |
26 |
Correct |
11 ms |
16904 KB |
Output is correct |
27 |
Correct |
11 ms |
16844 KB |
Output is correct |
28 |
Correct |
11 ms |
16972 KB |
Output is correct |
29 |
Correct |
11 ms |
16844 KB |
Output is correct |
30 |
Correct |
11 ms |
16844 KB |
Output is correct |
31 |
Correct |
11 ms |
16876 KB |
Output is correct |
32 |
Correct |
11 ms |
16904 KB |
Output is correct |
33 |
Correct |
14 ms |
16948 KB |
Output is correct |
34 |
Correct |
12 ms |
16972 KB |
Output is correct |
35 |
Correct |
12 ms |
16972 KB |
Output is correct |
36 |
Correct |
11 ms |
16896 KB |
Output is correct |
37 |
Correct |
12 ms |
16892 KB |
Output is correct |
38 |
Correct |
14 ms |
16972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16716 KB |
Output is correct |
2 |
Correct |
9 ms |
16716 KB |
Output is correct |
3 |
Correct |
10 ms |
16716 KB |
Output is correct |
4 |
Correct |
9 ms |
16684 KB |
Output is correct |
5 |
Correct |
9 ms |
16716 KB |
Output is correct |
6 |
Correct |
9 ms |
16716 KB |
Output is correct |
7 |
Correct |
9 ms |
16748 KB |
Output is correct |
8 |
Correct |
9 ms |
16760 KB |
Output is correct |
9 |
Correct |
10 ms |
16768 KB |
Output is correct |
10 |
Correct |
11 ms |
16744 KB |
Output is correct |
11 |
Correct |
9 ms |
16716 KB |
Output is correct |
12 |
Correct |
9 ms |
16800 KB |
Output is correct |
13 |
Correct |
9 ms |
16760 KB |
Output is correct |
14 |
Correct |
11 ms |
16764 KB |
Output is correct |
15 |
Correct |
11 ms |
16760 KB |
Output is correct |
16 |
Correct |
10 ms |
16716 KB |
Output is correct |
17 |
Correct |
11 ms |
16704 KB |
Output is correct |
18 |
Correct |
9 ms |
16772 KB |
Output is correct |
19 |
Correct |
656 ms |
45676 KB |
Output is correct |
20 |
Correct |
654 ms |
45288 KB |
Output is correct |
21 |
Correct |
635 ms |
44984 KB |
Output is correct |
22 |
Correct |
571 ms |
41464 KB |
Output is correct |
23 |
Correct |
771 ms |
50708 KB |
Output is correct |
24 |
Correct |
394 ms |
38212 KB |
Output is correct |
25 |
Correct |
661 ms |
60548 KB |
Output is correct |
26 |
Correct |
463 ms |
62596 KB |
Output is correct |
27 |
Correct |
704 ms |
59892 KB |
Output is correct |
28 |
Correct |
1604 ms |
112592 KB |
Output is correct |
29 |
Correct |
1595 ms |
112024 KB |
Output is correct |
30 |
Correct |
680 ms |
60000 KB |
Output is correct |
31 |
Correct |
733 ms |
59924 KB |
Output is correct |
32 |
Correct |
839 ms |
59892 KB |
Output is correct |
33 |
Correct |
1232 ms |
87108 KB |
Output is correct |
34 |
Correct |
1381 ms |
86824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16716 KB |
Output is correct |
2 |
Correct |
9 ms |
16716 KB |
Output is correct |
3 |
Correct |
10 ms |
16716 KB |
Output is correct |
4 |
Correct |
9 ms |
16684 KB |
Output is correct |
5 |
Correct |
9 ms |
16716 KB |
Output is correct |
6 |
Correct |
9 ms |
16716 KB |
Output is correct |
7 |
Correct |
9 ms |
16748 KB |
Output is correct |
8 |
Correct |
9 ms |
16760 KB |
Output is correct |
9 |
Correct |
10 ms |
16768 KB |
Output is correct |
10 |
Correct |
11 ms |
16744 KB |
Output is correct |
11 |
Correct |
9 ms |
16716 KB |
Output is correct |
12 |
Correct |
9 ms |
16800 KB |
Output is correct |
13 |
Correct |
9 ms |
16760 KB |
Output is correct |
14 |
Correct |
11 ms |
16764 KB |
Output is correct |
15 |
Correct |
11 ms |
16760 KB |
Output is correct |
16 |
Correct |
10 ms |
16716 KB |
Output is correct |
17 |
Correct |
11 ms |
16704 KB |
Output is correct |
18 |
Correct |
9 ms |
16772 KB |
Output is correct |
19 |
Correct |
8 ms |
16716 KB |
Output is correct |
20 |
Correct |
8 ms |
16716 KB |
Output is correct |
21 |
Correct |
11 ms |
16844 KB |
Output is correct |
22 |
Correct |
10 ms |
16956 KB |
Output is correct |
23 |
Correct |
11 ms |
16880 KB |
Output is correct |
24 |
Correct |
14 ms |
16892 KB |
Output is correct |
25 |
Correct |
11 ms |
16844 KB |
Output is correct |
26 |
Correct |
11 ms |
16904 KB |
Output is correct |
27 |
Correct |
11 ms |
16844 KB |
Output is correct |
28 |
Correct |
11 ms |
16972 KB |
Output is correct |
29 |
Correct |
11 ms |
16844 KB |
Output is correct |
30 |
Correct |
11 ms |
16844 KB |
Output is correct |
31 |
Correct |
11 ms |
16876 KB |
Output is correct |
32 |
Correct |
11 ms |
16904 KB |
Output is correct |
33 |
Correct |
14 ms |
16948 KB |
Output is correct |
34 |
Correct |
12 ms |
16972 KB |
Output is correct |
35 |
Correct |
12 ms |
16972 KB |
Output is correct |
36 |
Correct |
11 ms |
16896 KB |
Output is correct |
37 |
Correct |
12 ms |
16892 KB |
Output is correct |
38 |
Correct |
14 ms |
16972 KB |
Output is correct |
39 |
Correct |
656 ms |
45676 KB |
Output is correct |
40 |
Correct |
654 ms |
45288 KB |
Output is correct |
41 |
Correct |
635 ms |
44984 KB |
Output is correct |
42 |
Correct |
571 ms |
41464 KB |
Output is correct |
43 |
Correct |
771 ms |
50708 KB |
Output is correct |
44 |
Correct |
394 ms |
38212 KB |
Output is correct |
45 |
Correct |
661 ms |
60548 KB |
Output is correct |
46 |
Correct |
463 ms |
62596 KB |
Output is correct |
47 |
Correct |
704 ms |
59892 KB |
Output is correct |
48 |
Correct |
1604 ms |
112592 KB |
Output is correct |
49 |
Correct |
1595 ms |
112024 KB |
Output is correct |
50 |
Correct |
680 ms |
60000 KB |
Output is correct |
51 |
Correct |
733 ms |
59924 KB |
Output is correct |
52 |
Correct |
839 ms |
59892 KB |
Output is correct |
53 |
Correct |
1232 ms |
87108 KB |
Output is correct |
54 |
Correct |
1381 ms |
86824 KB |
Output is correct |
55 |
Correct |
47 ms |
18880 KB |
Output is correct |
56 |
Correct |
47 ms |
19112 KB |
Output is correct |
57 |
Correct |
616 ms |
50548 KB |
Output is correct |
58 |
Correct |
163 ms |
35916 KB |
Output is correct |
59 |
Correct |
531 ms |
62776 KB |
Output is correct |
60 |
Correct |
1614 ms |
112136 KB |
Output is correct |
61 |
Correct |
699 ms |
60100 KB |
Output is correct |
62 |
Correct |
658 ms |
59816 KB |
Output is correct |
63 |
Correct |
818 ms |
59884 KB |
Output is correct |
64 |
Correct |
1547 ms |
77840 KB |
Output is correct |
65 |
Correct |
1499 ms |
68500 KB |
Output is correct |
66 |
Correct |
1715 ms |
108928 KB |
Output is correct |
67 |
Correct |
511 ms |
55976 KB |
Output is correct |
68 |
Correct |
850 ms |
58548 KB |
Output is correct |
69 |
Correct |
837 ms |
58548 KB |
Output is correct |
70 |
Correct |
851 ms |
56216 KB |
Output is correct |