#include <bits/stdc++.h>
#define Yang8on Nguyen_Dinh_Son
#define sonphamay Nguyen_Dinh_Son
#define aothtday "aog"
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define ll long long
#define endl '\n'
#define pii pair<int, ll>
#define pb push_back
#define all(v) v.begin(), v.end()
#define f first
#define s second
#define maxn 200005
#define maxm
#define maxx
#define mod 1000000007
#define base 311
#define ModHas 1000000003ll
#define gb(i, j) ((i >> j) & 1)
#define int long long
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r)
{
return uniform_int_distribution<ll> (l, r)(rng);
}
const ll oo = 1e18;
int n, k;
ll lim, sl;
ll dis[maxn], reach[maxn], place[maxn];
vector<pii> o[maxn];
/// reach[u] : khoảng cách xa nhất đến 1 đỉnh chưa được bao phủ (gọi đỉnh đấy là u, tức u đến 1 đỉnh
void dfs(int u, int par)
{
for(pii x : o[u])
{
int v = x.f;
ll w = x.s;
if(v == par) continue;
dfs(v, u);
if(reach[v] + min(w, dis[v]) <= lim) /// (**)
{
if(reach[v] + dis[v] > lim) reach[u] = max(reach[u], reach[v] + w);
dis[u] = min(dis[u], dis[v] + w);
}
else
{
place[v] = 1, sl ++;
dis[u] = min(dis[u], w);
}
}
}
bool calc(int mid)
{
fi(i, 1, n) dis[i] = oo, reach[i] = 0, place[i] = 0;
lim = mid, sl = 0;
dfs(1, -1);
if(dis[1] + reach[1] > lim) place[1] = 1, sl ++;
if(sl > k) return false;
return true;
}
/// (**): tại sao lại là reach[v] + min(w, dis[v]) chứ không phải reach[v] + dis[v]?
/// nếu reach[v] + min(w, dis[v]) <= lim, tức đến u, nếu reach[v] + dis[v] > lim
/// --> reach[v] sẽ ko được điểm đánh dấu nào bao phủ nữa, thay vào đó ta sẽ tìm đình xa
/// nhất có thể đặt để bao phủ v, khi đó điểm đặt của ta sẽ men theo đường từ v lên u
/// --> từ đấy mà có reach[v] + w <= lim --> vẫn có thể chờ để đặt đỉnh tối ưu hơn, tăng
/// reach[u] lên
void solve()
{
cin >> n >> k;
fi(i, 1, n - 1)
{
int x, y;
ll w;
cin >> x >> y >> w;
o[x].pb({y, w});
o[y].pb({x, w});
}
int l = 0, r = oo;
while(l <= r)
{
int mid = (l + r) >> 1;
if(calc(mid)) r = mid - 1;
else l = mid + 1;
}
bool check = calc(l);
sl = 0;
fi(i, 1, n) if(place[i]) sl ++;
cout << l << '\n';
fi(i, 1, n)
{
if(place[i]) cout << i << ' ';
else if(sl < k) sl ++, cout << i << ' ';
}
}
signed main()
{
if(fopen(aothtday".inp", "r"))
{
freopen(aothtday".inp","r",stdin);
freopen(aothtday".out","w",stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int nTest = 1;
// cin >> nTest;
while(nTest --)
{
solve();
}
return 0;
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:107:10: warning: unused variable 'check' [-Wunused-variable]
107 | bool check = calc(l);
| ^~~~~
Main.cpp: In function 'int main()':
Main.cpp:124:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
124 | freopen(aothtday".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:125:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
125 | freopen(aothtday".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8792 KB |
Output is correct |
6 |
Correct |
3 ms |
8840 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8812 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
347 ms |
35956 KB |
Output is correct |
2 |
Correct |
371 ms |
37972 KB |
Output is correct |
3 |
Correct |
379 ms |
24616 KB |
Output is correct |
4 |
Correct |
1201 ms |
23512 KB |
Output is correct |
5 |
Correct |
1069 ms |
23024 KB |
Output is correct |
6 |
Correct |
981 ms |
23124 KB |
Output is correct |
7 |
Correct |
1156 ms |
21896 KB |
Output is correct |
8 |
Correct |
1430 ms |
22568 KB |
Output is correct |
9 |
Correct |
1117 ms |
22908 KB |
Output is correct |
10 |
Correct |
1101 ms |
23264 KB |
Output is correct |
11 |
Correct |
840 ms |
24872 KB |
Output is correct |
12 |
Correct |
808 ms |
24764 KB |
Output is correct |
13 |
Correct |
851 ms |
26452 KB |
Output is correct |
14 |
Correct |
783 ms |
23296 KB |
Output is correct |
15 |
Correct |
785 ms |
22896 KB |
Output is correct |
16 |
Correct |
979 ms |
24348 KB |
Output is correct |
17 |
Correct |
705 ms |
23580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
381 ms |
38976 KB |
Output is correct |
2 |
Correct |
374 ms |
37876 KB |
Output is correct |
3 |
Correct |
381 ms |
36180 KB |
Output is correct |
4 |
Correct |
371 ms |
36472 KB |
Output is correct |
5 |
Correct |
336 ms |
38404 KB |
Output is correct |
6 |
Correct |
395 ms |
38200 KB |
Output is correct |
7 |
Correct |
398 ms |
39132 KB |
Output is correct |
8 |
Correct |
365 ms |
38152 KB |
Output is correct |
9 |
Correct |
358 ms |
37968 KB |
Output is correct |
10 |
Correct |
384 ms |
37380 KB |
Output is correct |
11 |
Correct |
331 ms |
36288 KB |
Output is correct |
12 |
Correct |
372 ms |
38748 KB |
Output is correct |
13 |
Correct |
424 ms |
39012 KB |
Output is correct |
14 |
Correct |
387 ms |
38232 KB |
Output is correct |
15 |
Correct |
345 ms |
36432 KB |
Output is correct |
16 |
Correct |
379 ms |
35556 KB |
Output is correct |
17 |
Correct |
398 ms |
35156 KB |
Output is correct |
18 |
Correct |
357 ms |
36852 KB |
Output is correct |
19 |
Correct |
382 ms |
36212 KB |
Output is correct |
20 |
Correct |
361 ms |
37004 KB |
Output is correct |
21 |
Correct |
340 ms |
36296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8792 KB |
Output is correct |
6 |
Correct |
3 ms |
8840 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8812 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
347 ms |
35956 KB |
Output is correct |
16 |
Correct |
371 ms |
37972 KB |
Output is correct |
17 |
Correct |
379 ms |
24616 KB |
Output is correct |
18 |
Correct |
1201 ms |
23512 KB |
Output is correct |
19 |
Correct |
1069 ms |
23024 KB |
Output is correct |
20 |
Correct |
981 ms |
23124 KB |
Output is correct |
21 |
Correct |
1156 ms |
21896 KB |
Output is correct |
22 |
Correct |
1430 ms |
22568 KB |
Output is correct |
23 |
Correct |
1117 ms |
22908 KB |
Output is correct |
24 |
Correct |
1101 ms |
23264 KB |
Output is correct |
25 |
Correct |
840 ms |
24872 KB |
Output is correct |
26 |
Correct |
808 ms |
24764 KB |
Output is correct |
27 |
Correct |
851 ms |
26452 KB |
Output is correct |
28 |
Correct |
783 ms |
23296 KB |
Output is correct |
29 |
Correct |
785 ms |
22896 KB |
Output is correct |
30 |
Correct |
979 ms |
24348 KB |
Output is correct |
31 |
Correct |
705 ms |
23580 KB |
Output is correct |
32 |
Correct |
381 ms |
38976 KB |
Output is correct |
33 |
Correct |
374 ms |
37876 KB |
Output is correct |
34 |
Correct |
381 ms |
36180 KB |
Output is correct |
35 |
Correct |
371 ms |
36472 KB |
Output is correct |
36 |
Correct |
336 ms |
38404 KB |
Output is correct |
37 |
Correct |
395 ms |
38200 KB |
Output is correct |
38 |
Correct |
398 ms |
39132 KB |
Output is correct |
39 |
Correct |
365 ms |
38152 KB |
Output is correct |
40 |
Correct |
358 ms |
37968 KB |
Output is correct |
41 |
Correct |
384 ms |
37380 KB |
Output is correct |
42 |
Correct |
331 ms |
36288 KB |
Output is correct |
43 |
Correct |
372 ms |
38748 KB |
Output is correct |
44 |
Correct |
424 ms |
39012 KB |
Output is correct |
45 |
Correct |
387 ms |
38232 KB |
Output is correct |
46 |
Correct |
345 ms |
36432 KB |
Output is correct |
47 |
Correct |
379 ms |
35556 KB |
Output is correct |
48 |
Correct |
398 ms |
35156 KB |
Output is correct |
49 |
Correct |
357 ms |
36852 KB |
Output is correct |
50 |
Correct |
382 ms |
36212 KB |
Output is correct |
51 |
Correct |
361 ms |
37004 KB |
Output is correct |
52 |
Correct |
340 ms |
36296 KB |
Output is correct |
53 |
Correct |
1057 ms |
23392 KB |
Output is correct |
54 |
Correct |
1060 ms |
23892 KB |
Output is correct |
55 |
Correct |
1257 ms |
24512 KB |
Output is correct |
56 |
Correct |
1496 ms |
24076 KB |
Output is correct |
57 |
Correct |
1425 ms |
24436 KB |
Output is correct |
58 |
Correct |
1369 ms |
23520 KB |
Output is correct |
59 |
Correct |
1571 ms |
25144 KB |
Output is correct |
60 |
Correct |
1638 ms |
23044 KB |
Output is correct |
61 |
Correct |
1108 ms |
21592 KB |
Output is correct |
62 |
Correct |
1396 ms |
22156 KB |
Output is correct |
63 |
Correct |
1503 ms |
23100 KB |
Output is correct |
64 |
Correct |
1338 ms |
23252 KB |
Output is correct |
65 |
Correct |
1570 ms |
23380 KB |
Output is correct |
66 |
Correct |
1378 ms |
23324 KB |
Output is correct |
67 |
Correct |
1161 ms |
22496 KB |
Output is correct |
68 |
Correct |
1414 ms |
24572 KB |
Output is correct |
69 |
Correct |
361 ms |
37972 KB |
Output is correct |
70 |
Correct |
366 ms |
35948 KB |
Output is correct |
71 |
Correct |
397 ms |
38740 KB |
Output is correct |
72 |
Correct |
270 ms |
23720 KB |
Output is correct |
73 |
Correct |
329 ms |
22860 KB |
Output is correct |
74 |
Correct |
290 ms |
23224 KB |
Output is correct |
75 |
Correct |
808 ms |
25188 KB |
Output is correct |
76 |
Correct |
892 ms |
24724 KB |
Output is correct |
77 |
Correct |
741 ms |
24400 KB |
Output is correct |
78 |
Correct |
853 ms |
23888 KB |
Output is correct |