#include<bits/stdc++.h>
#include<race.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pli pair<ll, int>
#define f first
#define s second
int sol = 1e9;
void dfs(vector<vector<pii>> &graph, vector<int> &sub, int i, int p)
{
sub[i]=1;
for (pii ele:graph[i])
{
if (ele.f == p) continue;
dfs(graph, sub, ele.f, i);
sub[i] += sub[ele.f];
}
}
int centroid(vector<vector<pii>> &graph, vector<int> &sub, int i, int p, int n)
{
for (pii ele:graph[i])
{
if (ele.f == p) continue;
if (sub[ele.f]*2 > n) return centroid (graph, sub, ele.f, i, n);
}
return i;
}
void finde(vector<vector<pii>> &graph, vector<pli> &dist, vector<int> &nodes, int i, int p, ll d, int di)
{
di++;
dist.push_back({d, di});
nodes.push_back(i);
for (pii ele:graph[i])
{
if (ele.f == p) continue;
finde(graph, dist, nodes, ele.f, i, d+ele.s, di);
}
}
void cd(vector<vector<pii>> &graph, int K)
{
int n = graph.size();
vector<int> sub(n, 0);
dfs(graph, sub, 0, -1);
int c = centroid(graph, sub, 0, -1, n);
map<ll, int> paths;
paths[0] = 1;
vector<int> nind(n, 0);
for (pii ele:graph[c])
{
vector<pli> dist;
vector<int> nodes;
finde(graph, dist, nodes, ele.f, c, ele.s, 1);
for (pli d:dist)
{
if (d.f > K) continue;
if (paths[K-d.f]==0) continue;
sol = min(paths[K-d.f]+d.s, sol);
}
for (pli d:dist)
{
if (paths[d.f]==0) paths[d.f] = d.s;
else paths[d.f] = min(paths[d.f], d.s);
}
for (int i = 0; i < nodes.size(); i++) nind[nodes[i]] = i;
vector<vector<pii>> graph2(nodes.size());
for (int node:nodes)
{
for (pii conn:graph[node])
{
if (conn.f==c) continue;
graph2[nind[node]].push_back({nind[conn.f], conn.s});
}
}
cd(graph2, K);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
vector<vector<pii>> graph(N);
for (int i =0; i < N-1; i++)
{
graph[H[i][0]].push_back({H[i][1], L[i]});
graph[H[i][1]].push_back({H[i][0], L[i]});
}
cd(graph, K);
if (sol == 1e9) sol = 1;
return sol-2;
}
/*
int32_t main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int n, k;
cin >> n >> k;
int h[n-1][2];
int l[n-1];
for (int i = 0; i < n-1; i++)
{
cin >> h[i][0] >> h[i][1] >> l[i];
}
cout << best_path(n, k, h, l);
return 0;
}
*/
Compilation message
race.cpp: In function 'void cd(std::vector<std::vector<std::pair<int, int> > >&, int)':
race.cpp:76:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for (int i = 0; i < nodes.size(); i++) nind[nodes[i]] = i;
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 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 |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 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 |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2648 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 |
2392 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 |
2 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 |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 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 |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2648 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 |
2392 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 |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2472 KB |
Output is correct |
21 |
Correct |
3 ms |
2652 KB |
Output is correct |
22 |
Correct |
3 ms |
2652 KB |
Output is correct |
23 |
Correct |
3 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
3 ms |
2816 KB |
Output is correct |
26 |
Correct |
3 ms |
2652 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
3 ms |
2652 KB |
Output is correct |
29 |
Correct |
3 ms |
2652 KB |
Output is correct |
30 |
Correct |
3 ms |
2820 KB |
Output is correct |
31 |
Correct |
3 ms |
2652 KB |
Output is correct |
32 |
Correct |
3 ms |
2652 KB |
Output is correct |
33 |
Correct |
3 ms |
2652 KB |
Output is correct |
34 |
Correct |
3 ms |
2652 KB |
Output is correct |
35 |
Correct |
3 ms |
2752 KB |
Output is correct |
36 |
Correct |
3 ms |
2652 KB |
Output is correct |
37 |
Correct |
3 ms |
2652 KB |
Output is correct |
38 |
Correct |
3 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 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 |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 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 |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2648 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 |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
274 ms |
18776 KB |
Output is correct |
20 |
Correct |
276 ms |
20396 KB |
Output is correct |
21 |
Correct |
267 ms |
20716 KB |
Output is correct |
22 |
Correct |
249 ms |
20168 KB |
Output is correct |
23 |
Correct |
365 ms |
19636 KB |
Output is correct |
24 |
Correct |
162 ms |
15368 KB |
Output is correct |
25 |
Correct |
274 ms |
27892 KB |
Output is correct |
26 |
Correct |
203 ms |
28392 KB |
Output is correct |
27 |
Correct |
348 ms |
20820 KB |
Output is correct |
28 |
Correct |
888 ms |
77028 KB |
Output is correct |
29 |
Correct |
895 ms |
75916 KB |
Output is correct |
30 |
Correct |
353 ms |
21220 KB |
Output is correct |
31 |
Correct |
342 ms |
21340 KB |
Output is correct |
32 |
Correct |
356 ms |
21076 KB |
Output is correct |
33 |
Correct |
625 ms |
35456 KB |
Output is correct |
34 |
Correct |
752 ms |
47608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 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 |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 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 |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2648 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 |
2392 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 |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2472 KB |
Output is correct |
21 |
Correct |
3 ms |
2652 KB |
Output is correct |
22 |
Correct |
3 ms |
2652 KB |
Output is correct |
23 |
Correct |
3 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
3 ms |
2816 KB |
Output is correct |
26 |
Correct |
3 ms |
2652 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
3 ms |
2652 KB |
Output is correct |
29 |
Correct |
3 ms |
2652 KB |
Output is correct |
30 |
Correct |
3 ms |
2820 KB |
Output is correct |
31 |
Correct |
3 ms |
2652 KB |
Output is correct |
32 |
Correct |
3 ms |
2652 KB |
Output is correct |
33 |
Correct |
3 ms |
2652 KB |
Output is correct |
34 |
Correct |
3 ms |
2652 KB |
Output is correct |
35 |
Correct |
3 ms |
2752 KB |
Output is correct |
36 |
Correct |
3 ms |
2652 KB |
Output is correct |
37 |
Correct |
3 ms |
2652 KB |
Output is correct |
38 |
Correct |
3 ms |
2652 KB |
Output is correct |
39 |
Correct |
274 ms |
18776 KB |
Output is correct |
40 |
Correct |
276 ms |
20396 KB |
Output is correct |
41 |
Correct |
267 ms |
20716 KB |
Output is correct |
42 |
Correct |
249 ms |
20168 KB |
Output is correct |
43 |
Correct |
365 ms |
19636 KB |
Output is correct |
44 |
Correct |
162 ms |
15368 KB |
Output is correct |
45 |
Correct |
274 ms |
27892 KB |
Output is correct |
46 |
Correct |
203 ms |
28392 KB |
Output is correct |
47 |
Correct |
348 ms |
20820 KB |
Output is correct |
48 |
Correct |
888 ms |
77028 KB |
Output is correct |
49 |
Correct |
895 ms |
75916 KB |
Output is correct |
50 |
Correct |
353 ms |
21220 KB |
Output is correct |
51 |
Correct |
342 ms |
21340 KB |
Output is correct |
52 |
Correct |
356 ms |
21076 KB |
Output is correct |
53 |
Correct |
625 ms |
35456 KB |
Output is correct |
54 |
Correct |
752 ms |
47608 KB |
Output is correct |
55 |
Correct |
29 ms |
4696 KB |
Output is correct |
56 |
Correct |
21 ms |
3860 KB |
Output is correct |
57 |
Correct |
259 ms |
18104 KB |
Output is correct |
58 |
Correct |
55 ms |
13000 KB |
Output is correct |
59 |
Correct |
532 ms |
46612 KB |
Output is correct |
60 |
Correct |
1235 ms |
96444 KB |
Output is correct |
61 |
Correct |
421 ms |
22084 KB |
Output is correct |
62 |
Correct |
393 ms |
21592 KB |
Output is correct |
63 |
Correct |
425 ms |
21332 KB |
Output is correct |
64 |
Correct |
1083 ms |
53416 KB |
Output is correct |
65 |
Correct |
696 ms |
49776 KB |
Output is correct |
66 |
Correct |
1116 ms |
74236 KB |
Output is correct |
67 |
Correct |
110 ms |
22208 KB |
Output is correct |
68 |
Correct |
508 ms |
44488 KB |
Output is correct |
69 |
Correct |
520 ms |
43812 KB |
Output is correct |
70 |
Correct |
493 ms |
43668 KB |
Output is correct |