//TrungNotChung
#include <bits/stdc++.h>
#define pii pair<int , int>
#define fi first
#define se second
#define BIT(x,i) (1&((x)>>(i)))
#define MASK(x) (1LL<<(x))
#define CNT(x) __builtin_popcountll(x)
#define task "tnc"
using namespace std;
const int N = (int)2e5+228;
const long long INF = (long long)1e15;
int n, k, dp[N], mark[N], res[N];
vector<vector<pii> > adj;
long long dist[N], f[N];
void dfs(int u, int par, long long mid)
{
mark[u] = 0, dp[u] = 0, f[u] = -INF, dist[u] = 0;
for(int i=0; i<(int)adj[u].size(); ++i)
{
int v = adj[u][i].fi;
int w = adj[u][i].se;
if(v == par) continue;
dfs(v, u, mid);
f[u] = max(f[u], f[v] - w);
dp[u] += dp[v];
if(dist[v] == 0 && f[v] >= 0)
{
mark[v] = 2;
continue;
}
if(dist[v] + w > mid)
{
mark[v] = 1;
f[u] = max(f[u], mid - w);
dp[u]++;
// cout << u << " " << v << '\n';
}
}
// cout << u << " " << dp[u] << '\n';
for(int i=0; i<(int)adj[u].size(); ++i)
{
int v = adj[u][i].fi, w = adj[u][i].se;
if(mark[v] || v == par) continue;
if(f[u] - w >= dist[v])
mark[v] = 2;
}
for(int i=0; i<(int)adj[u].size(); ++i)
{
int v = adj[u][i].fi, w = adj[u][i].se;
if(mark[v] || v == par) continue;
dist[u] = max(dist[u], dist[v] + w);
}
}
bool check(long long mid)
{
dfs(1, -1, mid);
dp[1]++;
if(dist[1] == 0 && f[1] >= 0)
--dp[1];
return (dp[1] <= k);
}
void solve()
{
cin >> n >> k;
adj.assign(n+7, vector<pii>());
for(int i=1; i<n; ++i)
{
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back(make_pair(v, w));
adj[v].push_back(make_pair(u, w));
}
// check(29);
// for(int i=1; i<=n; ++i)
// {
// cout << dist[i] << " " << f[i] << " " << dp[i] << '\n';
// }
// return;
long long l = 0, r = INF, best;
while(l <= r)
{
long long mid = (l+r)/2;
if(check(mid))
{
best = mid;
for(int i=1; i<=n; ++i)
if(mark[i] == 1) res[i] = 1;
else res[i] = 0;
r = mid - 1;
}
else l = mid + 1;
}
int cnt = 0;
for(int i=1; i<=n; ++i)
cnt += res[i];
cnt = k - cnt;
for(int i=1; i<=n; ++i)
if(res[i] == 0 && cnt > 0)
res[i] = 1, --cnt;
cout << best << '\n';
for(int i=1; i<=n; ++i)
if(res[i]) cout << i << " ";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
//cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
483 ms |
33232 KB |
Output is correct |
2 |
Correct |
633 ms |
34376 KB |
Output is correct |
3 |
Correct |
637 ms |
18440 KB |
Output is correct |
4 |
Correct |
1768 ms |
17016 KB |
Output is correct |
5 |
Correct |
1602 ms |
16480 KB |
Output is correct |
6 |
Correct |
1640 ms |
16508 KB |
Output is correct |
7 |
Correct |
1626 ms |
16596 KB |
Output is correct |
8 |
Correct |
1709 ms |
17484 KB |
Output is correct |
9 |
Correct |
1745 ms |
16964 KB |
Output is correct |
10 |
Correct |
1751 ms |
17428 KB |
Output is correct |
11 |
Correct |
1035 ms |
18476 KB |
Output is correct |
12 |
Correct |
1028 ms |
18488 KB |
Output is correct |
13 |
Correct |
1132 ms |
20176 KB |
Output is correct |
14 |
Correct |
1025 ms |
18320 KB |
Output is correct |
15 |
Correct |
962 ms |
17764 KB |
Output is correct |
16 |
Correct |
1044 ms |
18708 KB |
Output is correct |
17 |
Correct |
1027 ms |
17760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
517 ms |
34824 KB |
Output is correct |
2 |
Correct |
500 ms |
34124 KB |
Output is correct |
3 |
Correct |
477 ms |
32368 KB |
Output is correct |
4 |
Correct |
484 ms |
32324 KB |
Output is correct |
5 |
Correct |
530 ms |
34688 KB |
Output is correct |
6 |
Correct |
568 ms |
34452 KB |
Output is correct |
7 |
Correct |
565 ms |
35624 KB |
Output is correct |
8 |
Correct |
525 ms |
35256 KB |
Output is correct |
9 |
Correct |
531 ms |
34968 KB |
Output is correct |
10 |
Correct |
505 ms |
34252 KB |
Output is correct |
11 |
Correct |
478 ms |
32976 KB |
Output is correct |
12 |
Correct |
571 ms |
35888 KB |
Output is correct |
13 |
Correct |
570 ms |
36108 KB |
Output is correct |
14 |
Correct |
557 ms |
35304 KB |
Output is correct |
15 |
Correct |
497 ms |
34188 KB |
Output is correct |
16 |
Correct |
481 ms |
32916 KB |
Output is correct |
17 |
Correct |
485 ms |
32800 KB |
Output is correct |
18 |
Correct |
522 ms |
34632 KB |
Output is correct |
19 |
Correct |
509 ms |
33680 KB |
Output is correct |
20 |
Correct |
527 ms |
34488 KB |
Output is correct |
21 |
Correct |
510 ms |
33848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
483 ms |
33232 KB |
Output is correct |
16 |
Correct |
633 ms |
34376 KB |
Output is correct |
17 |
Correct |
637 ms |
18440 KB |
Output is correct |
18 |
Correct |
1768 ms |
17016 KB |
Output is correct |
19 |
Correct |
1602 ms |
16480 KB |
Output is correct |
20 |
Correct |
1640 ms |
16508 KB |
Output is correct |
21 |
Correct |
1626 ms |
16596 KB |
Output is correct |
22 |
Correct |
1709 ms |
17484 KB |
Output is correct |
23 |
Correct |
1745 ms |
16964 KB |
Output is correct |
24 |
Correct |
1751 ms |
17428 KB |
Output is correct |
25 |
Correct |
1035 ms |
18476 KB |
Output is correct |
26 |
Correct |
1028 ms |
18488 KB |
Output is correct |
27 |
Correct |
1132 ms |
20176 KB |
Output is correct |
28 |
Correct |
1025 ms |
18320 KB |
Output is correct |
29 |
Correct |
962 ms |
17764 KB |
Output is correct |
30 |
Correct |
1044 ms |
18708 KB |
Output is correct |
31 |
Correct |
1027 ms |
17760 KB |
Output is correct |
32 |
Correct |
517 ms |
34824 KB |
Output is correct |
33 |
Correct |
500 ms |
34124 KB |
Output is correct |
34 |
Correct |
477 ms |
32368 KB |
Output is correct |
35 |
Correct |
484 ms |
32324 KB |
Output is correct |
36 |
Correct |
530 ms |
34688 KB |
Output is correct |
37 |
Correct |
568 ms |
34452 KB |
Output is correct |
38 |
Correct |
565 ms |
35624 KB |
Output is correct |
39 |
Correct |
525 ms |
35256 KB |
Output is correct |
40 |
Correct |
531 ms |
34968 KB |
Output is correct |
41 |
Correct |
505 ms |
34252 KB |
Output is correct |
42 |
Correct |
478 ms |
32976 KB |
Output is correct |
43 |
Correct |
571 ms |
35888 KB |
Output is correct |
44 |
Correct |
570 ms |
36108 KB |
Output is correct |
45 |
Correct |
557 ms |
35304 KB |
Output is correct |
46 |
Correct |
497 ms |
34188 KB |
Output is correct |
47 |
Correct |
481 ms |
32916 KB |
Output is correct |
48 |
Correct |
485 ms |
32800 KB |
Output is correct |
49 |
Correct |
522 ms |
34632 KB |
Output is correct |
50 |
Correct |
509 ms |
33680 KB |
Output is correct |
51 |
Correct |
527 ms |
34488 KB |
Output is correct |
52 |
Correct |
510 ms |
33848 KB |
Output is correct |
53 |
Correct |
1635 ms |
16760 KB |
Output is correct |
54 |
Correct |
1742 ms |
17264 KB |
Output is correct |
55 |
Correct |
1765 ms |
17996 KB |
Output is correct |
56 |
Correct |
1837 ms |
17608 KB |
Output is correct |
57 |
Correct |
1812 ms |
17996 KB |
Output is correct |
58 |
Correct |
1704 ms |
16972 KB |
Output is correct |
59 |
Correct |
1857 ms |
18792 KB |
Output is correct |
60 |
Correct |
1839 ms |
17908 KB |
Output is correct |
61 |
Correct |
1584 ms |
16416 KB |
Output is correct |
62 |
Correct |
1649 ms |
16880 KB |
Output is correct |
63 |
Correct |
1865 ms |
18296 KB |
Output is correct |
64 |
Correct |
1648 ms |
18132 KB |
Output is correct |
65 |
Correct |
1726 ms |
17476 KB |
Output is correct |
66 |
Correct |
1743 ms |
17432 KB |
Output is correct |
67 |
Correct |
1670 ms |
16488 KB |
Output is correct |
68 |
Correct |
1846 ms |
18920 KB |
Output is correct |
69 |
Correct |
522 ms |
34284 KB |
Output is correct |
70 |
Correct |
482 ms |
32544 KB |
Output is correct |
71 |
Correct |
560 ms |
35792 KB |
Output is correct |
72 |
Correct |
485 ms |
17180 KB |
Output is correct |
73 |
Correct |
624 ms |
18052 KB |
Output is correct |
74 |
Correct |
566 ms |
18780 KB |
Output is correct |
75 |
Correct |
1042 ms |
18892 KB |
Output is correct |
76 |
Correct |
1071 ms |
19752 KB |
Output is correct |
77 |
Correct |
1077 ms |
18892 KB |
Output is correct |
78 |
Correct |
1213 ms |
19052 KB |
Output is correct |