#include "race.h"
#include <bits/stdc++.h>
using namespace std;
const int maxk = 1e6 + 5;
const int maxn = 2e5 + 5;
const int inf = 1e9;
int dis[maxn], depth[maxn];
int n, k;
int vis[maxn];
int ans;
vector<vector<pair<int, int> > > edge(maxn);
int cur, tag[maxk], pt[maxk];
inline void reset(int dist)
{
if (tag[dist] != cur)
{
tag[dist] = cur;
pt[dist] = dist ? inf : 0;
}
return;
}
int dfn[maxn], last[maxn], rnk[maxn], cnt;
void init(int u, int f)
{
dfn[u] = ++cnt;
rnk[cnt] = u;
for (int i = 0; i < edge[u].size(); i++)
{
int v = edge[u][i].first;
int dist = edge[u][i].second;
if (f == v || vis[v])
continue;
dis[v] = dis[u] + dist;
depth[v] = depth[u] + 1;
init(v, u);
}
last[u] = cnt;
return;
}
int size[maxn], son[maxn], total;
void find_rt(int u, int f, int &root)
{
size[u] = 1, son[u] = 0;
for (int i = 0; i < edge[u].size(); i++)
{
int v = edge[u][i].first;
if (f == v || vis[v])
continue;
find_rt(v, u, root);
son[u] = max(son[u], size[v]);
size[u] += size[v];
}
son[u] = max(son[u], total - size[u]);
// cout << u << " " << son[u] << endl;
if (root == -1 || son[u] < son[root])
root = u;
return;
}
void dfs1(int u)
{
vis[u] = 1;
cur++;
// memset(pt, 0x3f, sizeof(pt));
pt[0] = 0;
for (int i = 0; i < edge[u].size(); i++)
{
int v = edge[u][i].first;
if (vis[v])
continue;
for (int j = dfn[v]; j <= last[v]; j++)
{
int dist = dis[rnk[j]];
int dep = depth[rnk[j]];
if (dist > k)
continue;
reset(k - dist);
ans = min(ans, dep + pt[k - dist]);
}
for (int j = dfn[v]; j <= last[v]; j++)
{
int dist = dis[rnk[j]];
int dep = depth[rnk[j]];
if (dist > k)
continue;
reset(dist);
pt[dist] = min(pt[dist], dep);
}
}
for (int i = 0; i < edge[u].size(); i++)
{
int v = edge[u][i].first;
if (vis[v])
continue;
int rt = -1;
total = size[v];
find_rt(v, -1, rt);
// cerr << rt << endl;
depth[rt] = 0;
dis[rt] = 0;
cnt = 0;
init(rt, -1);
dfs1(rt);
}
return;
}
int best_path(int N, int K, int H[][2], int L[])
{
for (int i = 0; i < N - 1; i++)
{
edge[H[i][0]].push_back(make_pair(H[i][1], L[i]));
edge[H[i][1]].push_back(make_pair(H[i][0], L[i]));
}
k = K;
n = N;
ans = N + 1;
int rt = -1;
total = n;
find_rt(0, -1, rt);
// cerr << rt << endl;
// cerr << son[rt] << endl;
depth[rt] = 0;
dis[rt] = 0;
init(rt, -1);
dfs1(rt);
if (ans == N + 1)
return -1;
return ans;
}
Compilation message
race.cpp: In function 'void init(int, int)':
race.cpp:27:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < edge[u].size(); i++)
~~^~~~~~~~~~~~~~~~
race.cpp: In function 'void find_rt(int, int, int&)':
race.cpp:44:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < edge[u].size(); i++)
~~^~~~~~~~~~~~~~~~
race.cpp: In function 'void dfs1(int)':
race.cpp:65:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < edge[u].size(); i++)
~~^~~~~~~~~~~~~~~~
race.cpp:90:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < edge[u].size(); i++)
~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
3 ms |
5120 KB |
Output is correct |
4 |
Correct |
3 ms |
5120 KB |
Output is correct |
5 |
Correct |
3 ms |
5120 KB |
Output is correct |
6 |
Correct |
3 ms |
5120 KB |
Output is correct |
7 |
Correct |
3 ms |
5120 KB |
Output is correct |
8 |
Correct |
3 ms |
5120 KB |
Output is correct |
9 |
Correct |
3 ms |
5120 KB |
Output is correct |
10 |
Correct |
3 ms |
5120 KB |
Output is correct |
11 |
Correct |
3 ms |
5120 KB |
Output is correct |
12 |
Correct |
3 ms |
5120 KB |
Output is correct |
13 |
Correct |
3 ms |
5120 KB |
Output is correct |
14 |
Correct |
3 ms |
5120 KB |
Output is correct |
15 |
Correct |
3 ms |
5120 KB |
Output is correct |
16 |
Correct |
3 ms |
5120 KB |
Output is correct |
17 |
Correct |
3 ms |
5120 KB |
Output is correct |
18 |
Correct |
3 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
3 ms |
5120 KB |
Output is correct |
4 |
Correct |
3 ms |
5120 KB |
Output is correct |
5 |
Correct |
3 ms |
5120 KB |
Output is correct |
6 |
Correct |
3 ms |
5120 KB |
Output is correct |
7 |
Correct |
3 ms |
5120 KB |
Output is correct |
8 |
Correct |
3 ms |
5120 KB |
Output is correct |
9 |
Correct |
3 ms |
5120 KB |
Output is correct |
10 |
Correct |
3 ms |
5120 KB |
Output is correct |
11 |
Correct |
3 ms |
5120 KB |
Output is correct |
12 |
Correct |
3 ms |
5120 KB |
Output is correct |
13 |
Correct |
3 ms |
5120 KB |
Output is correct |
14 |
Correct |
3 ms |
5120 KB |
Output is correct |
15 |
Correct |
3 ms |
5120 KB |
Output is correct |
16 |
Correct |
3 ms |
5120 KB |
Output is correct |
17 |
Correct |
3 ms |
5120 KB |
Output is correct |
18 |
Correct |
3 ms |
5120 KB |
Output is correct |
19 |
Correct |
3 ms |
5120 KB |
Output is correct |
20 |
Correct |
3 ms |
5120 KB |
Output is correct |
21 |
Correct |
4 ms |
5120 KB |
Output is correct |
22 |
Correct |
8 ms |
11776 KB |
Output is correct |
23 |
Correct |
8 ms |
10624 KB |
Output is correct |
24 |
Correct |
8 ms |
11776 KB |
Output is correct |
25 |
Correct |
8 ms |
11648 KB |
Output is correct |
26 |
Correct |
5 ms |
7808 KB |
Output is correct |
27 |
Correct |
3 ms |
5120 KB |
Output is correct |
28 |
Correct |
5 ms |
6656 KB |
Output is correct |
29 |
Correct |
5 ms |
7680 KB |
Output is correct |
30 |
Correct |
5 ms |
8064 KB |
Output is correct |
31 |
Correct |
7 ms |
10240 KB |
Output is correct |
32 |
Correct |
7 ms |
10752 KB |
Output is correct |
33 |
Correct |
7 ms |
11264 KB |
Output is correct |
34 |
Correct |
6 ms |
9728 KB |
Output is correct |
35 |
Correct |
7 ms |
11520 KB |
Output is correct |
36 |
Correct |
8 ms |
12416 KB |
Output is correct |
37 |
Correct |
7 ms |
11392 KB |
Output is correct |
38 |
Correct |
7 ms |
9088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
3 ms |
5120 KB |
Output is correct |
4 |
Correct |
3 ms |
5120 KB |
Output is correct |
5 |
Correct |
3 ms |
5120 KB |
Output is correct |
6 |
Correct |
3 ms |
5120 KB |
Output is correct |
7 |
Correct |
3 ms |
5120 KB |
Output is correct |
8 |
Correct |
3 ms |
5120 KB |
Output is correct |
9 |
Correct |
3 ms |
5120 KB |
Output is correct |
10 |
Correct |
3 ms |
5120 KB |
Output is correct |
11 |
Correct |
3 ms |
5120 KB |
Output is correct |
12 |
Correct |
3 ms |
5120 KB |
Output is correct |
13 |
Correct |
3 ms |
5120 KB |
Output is correct |
14 |
Correct |
3 ms |
5120 KB |
Output is correct |
15 |
Correct |
3 ms |
5120 KB |
Output is correct |
16 |
Correct |
3 ms |
5120 KB |
Output is correct |
17 |
Correct |
3 ms |
5120 KB |
Output is correct |
18 |
Correct |
3 ms |
5120 KB |
Output is correct |
19 |
Correct |
202 ms |
13048 KB |
Output is correct |
20 |
Correct |
192 ms |
14328 KB |
Output is correct |
21 |
Correct |
200 ms |
14584 KB |
Output is correct |
22 |
Correct |
166 ms |
14456 KB |
Output is correct |
23 |
Correct |
144 ms |
14712 KB |
Output is correct |
24 |
Correct |
96 ms |
14584 KB |
Output is correct |
25 |
Correct |
175 ms |
18040 KB |
Output is correct |
26 |
Correct |
103 ms |
21624 KB |
Output is correct |
27 |
Correct |
235 ms |
24060 KB |
Output is correct |
28 |
Correct |
503 ms |
38520 KB |
Output is correct |
29 |
Correct |
508 ms |
37624 KB |
Output is correct |
30 |
Correct |
243 ms |
24184 KB |
Output is correct |
31 |
Correct |
242 ms |
24056 KB |
Output is correct |
32 |
Correct |
368 ms |
24184 KB |
Output is correct |
33 |
Correct |
461 ms |
23288 KB |
Output is correct |
34 |
Correct |
408 ms |
23928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
3 ms |
5120 KB |
Output is correct |
4 |
Correct |
3 ms |
5120 KB |
Output is correct |
5 |
Correct |
3 ms |
5120 KB |
Output is correct |
6 |
Correct |
3 ms |
5120 KB |
Output is correct |
7 |
Correct |
3 ms |
5120 KB |
Output is correct |
8 |
Correct |
3 ms |
5120 KB |
Output is correct |
9 |
Correct |
3 ms |
5120 KB |
Output is correct |
10 |
Correct |
3 ms |
5120 KB |
Output is correct |
11 |
Correct |
3 ms |
5120 KB |
Output is correct |
12 |
Correct |
3 ms |
5120 KB |
Output is correct |
13 |
Correct |
3 ms |
5120 KB |
Output is correct |
14 |
Correct |
3 ms |
5120 KB |
Output is correct |
15 |
Correct |
3 ms |
5120 KB |
Output is correct |
16 |
Correct |
3 ms |
5120 KB |
Output is correct |
17 |
Correct |
3 ms |
5120 KB |
Output is correct |
18 |
Correct |
3 ms |
5120 KB |
Output is correct |
19 |
Correct |
3 ms |
5120 KB |
Output is correct |
20 |
Correct |
3 ms |
5120 KB |
Output is correct |
21 |
Correct |
4 ms |
5120 KB |
Output is correct |
22 |
Correct |
8 ms |
11776 KB |
Output is correct |
23 |
Correct |
8 ms |
10624 KB |
Output is correct |
24 |
Correct |
8 ms |
11776 KB |
Output is correct |
25 |
Correct |
8 ms |
11648 KB |
Output is correct |
26 |
Correct |
5 ms |
7808 KB |
Output is correct |
27 |
Correct |
3 ms |
5120 KB |
Output is correct |
28 |
Correct |
5 ms |
6656 KB |
Output is correct |
29 |
Correct |
5 ms |
7680 KB |
Output is correct |
30 |
Correct |
5 ms |
8064 KB |
Output is correct |
31 |
Correct |
7 ms |
10240 KB |
Output is correct |
32 |
Correct |
7 ms |
10752 KB |
Output is correct |
33 |
Correct |
7 ms |
11264 KB |
Output is correct |
34 |
Correct |
6 ms |
9728 KB |
Output is correct |
35 |
Correct |
7 ms |
11520 KB |
Output is correct |
36 |
Correct |
8 ms |
12416 KB |
Output is correct |
37 |
Correct |
7 ms |
11392 KB |
Output is correct |
38 |
Correct |
7 ms |
9088 KB |
Output is correct |
39 |
Correct |
202 ms |
13048 KB |
Output is correct |
40 |
Correct |
192 ms |
14328 KB |
Output is correct |
41 |
Correct |
200 ms |
14584 KB |
Output is correct |
42 |
Correct |
166 ms |
14456 KB |
Output is correct |
43 |
Correct |
144 ms |
14712 KB |
Output is correct |
44 |
Correct |
96 ms |
14584 KB |
Output is correct |
45 |
Correct |
175 ms |
18040 KB |
Output is correct |
46 |
Correct |
103 ms |
21624 KB |
Output is correct |
47 |
Correct |
235 ms |
24060 KB |
Output is correct |
48 |
Correct |
503 ms |
38520 KB |
Output is correct |
49 |
Correct |
508 ms |
37624 KB |
Output is correct |
50 |
Correct |
243 ms |
24184 KB |
Output is correct |
51 |
Correct |
242 ms |
24056 KB |
Output is correct |
52 |
Correct |
368 ms |
24184 KB |
Output is correct |
53 |
Correct |
461 ms |
23288 KB |
Output is correct |
54 |
Correct |
408 ms |
23928 KB |
Output is correct |
55 |
Correct |
14 ms |
6016 KB |
Output is correct |
56 |
Correct |
14 ms |
6016 KB |
Output is correct |
57 |
Correct |
104 ms |
14584 KB |
Output is correct |
58 |
Correct |
46 ms |
14320 KB |
Output is correct |
59 |
Correct |
121 ms |
23076 KB |
Output is correct |
60 |
Correct |
599 ms |
45560 KB |
Output is correct |
61 |
Correct |
224 ms |
24312 KB |
Output is correct |
62 |
Correct |
228 ms |
24440 KB |
Output is correct |
63 |
Correct |
341 ms |
24440 KB |
Output is correct |
64 |
Correct |
679 ms |
28536 KB |
Output is correct |
65 |
Correct |
456 ms |
24952 KB |
Output is correct |
66 |
Correct |
584 ms |
42872 KB |
Output is correct |
67 |
Correct |
158 ms |
24936 KB |
Output is correct |
68 |
Correct |
353 ms |
30152 KB |
Output is correct |
69 |
Correct |
365 ms |
30596 KB |
Output is correct |
70 |
Correct |
367 ms |
29304 KB |
Output is correct |