#include <bits/stdc++.h>
using namespace std;
typedef int ll;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef map<ll, ll> mll;
#define N 200'005
#define L 1'000'005
#define MOD 1000000007
#define FOR(i, n) for (i = 0; i < n; i++)
#define FORR(i, a, b) for (i = a; i <= b; i++)
#define FASTIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define FREOPEN \
freopen("yinyang.in", "r", stdin); \
freopen("yinyang.out", "w", stdout);
#define gohome \
cout << "NO\n"; \
return;
#define arprt(x) \
for (auto it : x) \
cout << it << " "; \
cout << "\n";
void solve();
vpll edges[N];
vll torem;
ll sz[N], maxd, l, ans, cnt[L];
bool rem[N], calc;
const int BUF = 100'001;
ll find_sz(ll v, ll p)
{
sz[v] = 1;
for (auto [it, x] : edges[v])
if (it != p && !rem[it])
sz[v] += find_sz(it, v);
return sz[v];
}
ll get_centroid(ll v, ll p, ll trsz)
{
for (auto [it, x] : edges[v])
if (it != p && !rem[it] && sz[it] * 2 > trsz)
return get_centroid(it, v, trsz);
return v;
}
void process(ll v, ll p, ll s, ll d)
{
if (s > l)
return;
torem.push_back(s);
if (calc)
{
if (cnt[l - s] < INT_MAX)
ans = min(ans, d + cnt[l - s]);
}
else
cnt[s] = min(cnt[s], d);
for (auto [u, c] : edges[v])
{
if (u == p || rem[u])
continue;
process(u, v, s + c, d + 1);
}
}
void centroid_decomp(ll v)
{
ll centroid = get_centroid(v, -1, find_sz(v, -1));
rem[centroid] = true;
for (auto [u, c] : edges[centroid])
{
if (rem[u])
continue;
calc = true;
process(u, -1, c, 1);
calc = false;
process(u, -1, c, 1);
}
for (auto it : torem)
cnt[it] = INT_MAX;
torem.clear();
for (auto [it, x] : edges[centroid])
if (!rem[it])
centroid_decomp(it);
}
int best_path(int n, int K, int H[][2], int *cost)
{
l = K;
int u, v, c;
for (int i = 0; i < n - 1; i++)
{
u = H[i][0], v = H[i][1], c = cost[i];
edges[u].push_back({v, c});
edges[v].push_back({u, c});
}
for (int i = 1; i < L; i++)
cnt[i] = INT_MAX;
ans = INT_MAX;
centroid_decomp(0);
if (ans == INT_MAX)
ans = -1;
return ans;
}
// int main()
// {
// int n = 4, k = 3;
// int H[3][2] = {{0, 1}, {1, 2}, {1, 3}};
// int *cost = new int[3];
// cost[0] = 1;
// cost[1] = 2;
// cost[2] = 4;
// cout << best_path(n, k, H, cost);
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13656 KB |
Output is correct |
2 |
Correct |
3 ms |
13660 KB |
Output is correct |
3 |
Correct |
3 ms |
13660 KB |
Output is correct |
4 |
Correct |
3 ms |
13672 KB |
Output is correct |
5 |
Correct |
3 ms |
13660 KB |
Output is correct |
6 |
Correct |
3 ms |
13660 KB |
Output is correct |
7 |
Correct |
3 ms |
13660 KB |
Output is correct |
8 |
Correct |
3 ms |
13660 KB |
Output is correct |
9 |
Correct |
3 ms |
13660 KB |
Output is correct |
10 |
Correct |
3 ms |
13660 KB |
Output is correct |
11 |
Correct |
3 ms |
13656 KB |
Output is correct |
12 |
Correct |
3 ms |
13660 KB |
Output is correct |
13 |
Correct |
3 ms |
13652 KB |
Output is correct |
14 |
Correct |
3 ms |
13656 KB |
Output is correct |
15 |
Correct |
3 ms |
13656 KB |
Output is correct |
16 |
Correct |
4 ms |
13896 KB |
Output is correct |
17 |
Correct |
3 ms |
13660 KB |
Output is correct |
18 |
Correct |
3 ms |
13660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13656 KB |
Output is correct |
2 |
Correct |
3 ms |
13660 KB |
Output is correct |
3 |
Correct |
3 ms |
13660 KB |
Output is correct |
4 |
Correct |
3 ms |
13672 KB |
Output is correct |
5 |
Correct |
3 ms |
13660 KB |
Output is correct |
6 |
Correct |
3 ms |
13660 KB |
Output is correct |
7 |
Correct |
3 ms |
13660 KB |
Output is correct |
8 |
Correct |
3 ms |
13660 KB |
Output is correct |
9 |
Correct |
3 ms |
13660 KB |
Output is correct |
10 |
Correct |
3 ms |
13660 KB |
Output is correct |
11 |
Correct |
3 ms |
13656 KB |
Output is correct |
12 |
Correct |
3 ms |
13660 KB |
Output is correct |
13 |
Correct |
3 ms |
13652 KB |
Output is correct |
14 |
Correct |
3 ms |
13656 KB |
Output is correct |
15 |
Correct |
3 ms |
13656 KB |
Output is correct |
16 |
Correct |
4 ms |
13896 KB |
Output is correct |
17 |
Correct |
3 ms |
13660 KB |
Output is correct |
18 |
Correct |
3 ms |
13660 KB |
Output is correct |
19 |
Correct |
3 ms |
13656 KB |
Output is correct |
20 |
Correct |
3 ms |
13660 KB |
Output is correct |
21 |
Correct |
3 ms |
13660 KB |
Output is correct |
22 |
Correct |
3 ms |
13660 KB |
Output is correct |
23 |
Correct |
3 ms |
13724 KB |
Output is correct |
24 |
Correct |
3 ms |
13660 KB |
Output is correct |
25 |
Correct |
3 ms |
13660 KB |
Output is correct |
26 |
Correct |
3 ms |
13656 KB |
Output is correct |
27 |
Correct |
3 ms |
13656 KB |
Output is correct |
28 |
Correct |
3 ms |
13660 KB |
Output is correct |
29 |
Correct |
3 ms |
13656 KB |
Output is correct |
30 |
Correct |
3 ms |
13660 KB |
Output is correct |
31 |
Correct |
3 ms |
13656 KB |
Output is correct |
32 |
Correct |
3 ms |
13660 KB |
Output is correct |
33 |
Correct |
3 ms |
13660 KB |
Output is correct |
34 |
Correct |
3 ms |
13668 KB |
Output is correct |
35 |
Correct |
4 ms |
13660 KB |
Output is correct |
36 |
Correct |
3 ms |
13660 KB |
Output is correct |
37 |
Correct |
3 ms |
13660 KB |
Output is correct |
38 |
Correct |
3 ms |
13660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13656 KB |
Output is correct |
2 |
Correct |
3 ms |
13660 KB |
Output is correct |
3 |
Correct |
3 ms |
13660 KB |
Output is correct |
4 |
Correct |
3 ms |
13672 KB |
Output is correct |
5 |
Correct |
3 ms |
13660 KB |
Output is correct |
6 |
Correct |
3 ms |
13660 KB |
Output is correct |
7 |
Correct |
3 ms |
13660 KB |
Output is correct |
8 |
Correct |
3 ms |
13660 KB |
Output is correct |
9 |
Correct |
3 ms |
13660 KB |
Output is correct |
10 |
Correct |
3 ms |
13660 KB |
Output is correct |
11 |
Correct |
3 ms |
13656 KB |
Output is correct |
12 |
Correct |
3 ms |
13660 KB |
Output is correct |
13 |
Correct |
3 ms |
13652 KB |
Output is correct |
14 |
Correct |
3 ms |
13656 KB |
Output is correct |
15 |
Correct |
3 ms |
13656 KB |
Output is correct |
16 |
Correct |
4 ms |
13896 KB |
Output is correct |
17 |
Correct |
3 ms |
13660 KB |
Output is correct |
18 |
Correct |
3 ms |
13660 KB |
Output is correct |
19 |
Correct |
79 ms |
21364 KB |
Output is correct |
20 |
Correct |
78 ms |
21108 KB |
Output is correct |
21 |
Correct |
85 ms |
21332 KB |
Output is correct |
22 |
Correct |
73 ms |
21960 KB |
Output is correct |
23 |
Correct |
48 ms |
21220 KB |
Output is correct |
24 |
Correct |
38 ms |
20984 KB |
Output is correct |
25 |
Correct |
77 ms |
23632 KB |
Output is correct |
26 |
Correct |
69 ms |
27652 KB |
Output is correct |
27 |
Correct |
104 ms |
26452 KB |
Output is correct |
28 |
Correct |
152 ms |
37604 KB |
Output is correct |
29 |
Correct |
153 ms |
36604 KB |
Output is correct |
30 |
Correct |
113 ms |
26228 KB |
Output is correct |
31 |
Correct |
110 ms |
26412 KB |
Output is correct |
32 |
Correct |
117 ms |
26416 KB |
Output is correct |
33 |
Correct |
136 ms |
25336 KB |
Output is correct |
34 |
Correct |
110 ms |
25936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13656 KB |
Output is correct |
2 |
Correct |
3 ms |
13660 KB |
Output is correct |
3 |
Correct |
3 ms |
13660 KB |
Output is correct |
4 |
Correct |
3 ms |
13672 KB |
Output is correct |
5 |
Correct |
3 ms |
13660 KB |
Output is correct |
6 |
Correct |
3 ms |
13660 KB |
Output is correct |
7 |
Correct |
3 ms |
13660 KB |
Output is correct |
8 |
Correct |
3 ms |
13660 KB |
Output is correct |
9 |
Correct |
3 ms |
13660 KB |
Output is correct |
10 |
Correct |
3 ms |
13660 KB |
Output is correct |
11 |
Correct |
3 ms |
13656 KB |
Output is correct |
12 |
Correct |
3 ms |
13660 KB |
Output is correct |
13 |
Correct |
3 ms |
13652 KB |
Output is correct |
14 |
Correct |
3 ms |
13656 KB |
Output is correct |
15 |
Correct |
3 ms |
13656 KB |
Output is correct |
16 |
Correct |
4 ms |
13896 KB |
Output is correct |
17 |
Correct |
3 ms |
13660 KB |
Output is correct |
18 |
Correct |
3 ms |
13660 KB |
Output is correct |
19 |
Correct |
3 ms |
13656 KB |
Output is correct |
20 |
Correct |
3 ms |
13660 KB |
Output is correct |
21 |
Correct |
3 ms |
13660 KB |
Output is correct |
22 |
Correct |
3 ms |
13660 KB |
Output is correct |
23 |
Correct |
3 ms |
13724 KB |
Output is correct |
24 |
Correct |
3 ms |
13660 KB |
Output is correct |
25 |
Correct |
3 ms |
13660 KB |
Output is correct |
26 |
Correct |
3 ms |
13656 KB |
Output is correct |
27 |
Correct |
3 ms |
13656 KB |
Output is correct |
28 |
Correct |
3 ms |
13660 KB |
Output is correct |
29 |
Correct |
3 ms |
13656 KB |
Output is correct |
30 |
Correct |
3 ms |
13660 KB |
Output is correct |
31 |
Correct |
3 ms |
13656 KB |
Output is correct |
32 |
Correct |
3 ms |
13660 KB |
Output is correct |
33 |
Correct |
3 ms |
13660 KB |
Output is correct |
34 |
Correct |
3 ms |
13668 KB |
Output is correct |
35 |
Correct |
4 ms |
13660 KB |
Output is correct |
36 |
Correct |
3 ms |
13660 KB |
Output is correct |
37 |
Correct |
3 ms |
13660 KB |
Output is correct |
38 |
Correct |
3 ms |
13660 KB |
Output is correct |
39 |
Correct |
79 ms |
21364 KB |
Output is correct |
40 |
Correct |
78 ms |
21108 KB |
Output is correct |
41 |
Correct |
85 ms |
21332 KB |
Output is correct |
42 |
Correct |
73 ms |
21960 KB |
Output is correct |
43 |
Correct |
48 ms |
21220 KB |
Output is correct |
44 |
Correct |
38 ms |
20984 KB |
Output is correct |
45 |
Correct |
77 ms |
23632 KB |
Output is correct |
46 |
Correct |
69 ms |
27652 KB |
Output is correct |
47 |
Correct |
104 ms |
26452 KB |
Output is correct |
48 |
Correct |
152 ms |
37604 KB |
Output is correct |
49 |
Correct |
153 ms |
36604 KB |
Output is correct |
50 |
Correct |
113 ms |
26228 KB |
Output is correct |
51 |
Correct |
110 ms |
26412 KB |
Output is correct |
52 |
Correct |
117 ms |
26416 KB |
Output is correct |
53 |
Correct |
136 ms |
25336 KB |
Output is correct |
54 |
Correct |
110 ms |
25936 KB |
Output is correct |
55 |
Correct |
9 ms |
14168 KB |
Output is correct |
56 |
Correct |
9 ms |
14176 KB |
Output is correct |
57 |
Correct |
55 ms |
22044 KB |
Output is correct |
58 |
Correct |
30 ms |
22364 KB |
Output is correct |
59 |
Correct |
72 ms |
27580 KB |
Output is correct |
60 |
Correct |
246 ms |
39076 KB |
Output is correct |
61 |
Correct |
112 ms |
26968 KB |
Output is correct |
62 |
Correct |
127 ms |
29640 KB |
Output is correct |
63 |
Correct |
147 ms |
28616 KB |
Output is correct |
64 |
Correct |
283 ms |
28616 KB |
Output is correct |
65 |
Correct |
119 ms |
27224 KB |
Output is correct |
66 |
Correct |
211 ms |
34900 KB |
Output is correct |
67 |
Correct |
71 ms |
30040 KB |
Output is correct |
68 |
Correct |
160 ms |
28364 KB |
Output is correct |
69 |
Correct |
145 ms |
28368 KB |
Output is correct |
70 |
Correct |
137 ms |
27756 KB |
Output is correct |