#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPD(i, n) for (int i = (n) - 1; i >= 0; --i)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*
Phu Trong from Nguyen Tat Thanh High School for gifted student
*/
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
#define MAX 200005
struct Edge{
int u, v, w;
Edge(){}
Edge(int _u, int _v, int _w): u(_u), v(_v), w(_w){}
int other(int x){return (x ^ u ^ v);}
} edge[MAX];
int numNode, k;
vector<int> G[MAX];
void loadGraph(void){
cin >> numNode >> k;
for (int i = 1; i < numNode; ++i){
cin >> edge[i].u >> edge[i].v >> edge[i].w;
G[edge[i].u].emplace_back(i);
G[edge[i].v].emplace_back(i);
}
}
int minDist[MAX], maxReach[MAX];
vector<int> candidate;
void dfs(int u, int p, int d){
for (int &i : G[u]){
int v = edge[i].other(u);
if(v ^ p){
dfs(v, u, d);
if (maxReach[v] + min(edge[i].w, minDist[v]) <= d){
if (maxReach[v] + minDist[v] > d)
maximize(maxReach[u], maxReach[v] + edge[i].w);
minimize(minDist[u], minDist[v] + edge[i].w);
}
else{
candidate.emplace_back(v);
minimize(minDist[u], edge[i].w);
}
}
}
}
bool check(int d){
candidate.clear();
memset(maxReach, 0, (numNode + 1) * sizeof(int));
memset(minDist, 0x3f, (numNode + 1) * sizeof(int));
dfs(1, -1, d);
if (maxReach[1] + minDist[1] > d) candidate.push_back(1);
return (int)candidate.size() <= k;
}
int chosen[MAX];
void process(void){
int l = 0, r = 1e15;
while(r - l > 1){
int m = (l + r) >> 1;
if(check(m)) r = m;
else l = m;
}
cout << r << '\n';
check(r);
for (int& v : candidate) chosen[v] = 1;
for (int i = 1; i <= numNode; ++i) {
if(!chosen[i] && (int)candidate.size() < k)
candidate.push_back(i);
}
for (int &v : candidate) cout << v << " ";
}
signed main(){
#define name "Whisper"
cin.tie(nullptr) -> sync_with_stdio(false);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
loadGraph();
process();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
5188 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4952 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
5184 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
273 ms |
38748 KB |
Output is correct |
2 |
Correct |
300 ms |
41108 KB |
Output is correct |
3 |
Correct |
162 ms |
25416 KB |
Output is correct |
4 |
Correct |
725 ms |
23620 KB |
Output is correct |
5 |
Correct |
654 ms |
23012 KB |
Output is correct |
6 |
Correct |
670 ms |
23128 KB |
Output is correct |
7 |
Correct |
685 ms |
22096 KB |
Output is correct |
8 |
Correct |
664 ms |
22608 KB |
Output is correct |
9 |
Correct |
757 ms |
22868 KB |
Output is correct |
10 |
Correct |
681 ms |
23384 KB |
Output is correct |
11 |
Correct |
437 ms |
25020 KB |
Output is correct |
12 |
Correct |
345 ms |
24916 KB |
Output is correct |
13 |
Correct |
384 ms |
26892 KB |
Output is correct |
14 |
Correct |
357 ms |
23540 KB |
Output is correct |
15 |
Correct |
332 ms |
22864 KB |
Output is correct |
16 |
Correct |
349 ms |
24628 KB |
Output is correct |
17 |
Correct |
400 ms |
23640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
291 ms |
41812 KB |
Output is correct |
2 |
Correct |
244 ms |
40776 KB |
Output is correct |
3 |
Correct |
297 ms |
38744 KB |
Output is correct |
4 |
Correct |
233 ms |
35676 KB |
Output is correct |
5 |
Correct |
333 ms |
44180 KB |
Output is correct |
6 |
Correct |
319 ms |
43400 KB |
Output is correct |
7 |
Correct |
339 ms |
44996 KB |
Output is correct |
8 |
Correct |
293 ms |
41296 KB |
Output is correct |
9 |
Correct |
303 ms |
40992 KB |
Output is correct |
10 |
Correct |
298 ms |
40268 KB |
Output is correct |
11 |
Correct |
267 ms |
38736 KB |
Output is correct |
12 |
Correct |
362 ms |
44996 KB |
Output is correct |
13 |
Correct |
347 ms |
44668 KB |
Output is correct |
14 |
Correct |
365 ms |
43468 KB |
Output is correct |
15 |
Correct |
266 ms |
39504 KB |
Output is correct |
16 |
Correct |
249 ms |
38224 KB |
Output is correct |
17 |
Correct |
282 ms |
37952 KB |
Output is correct |
18 |
Correct |
324 ms |
39764 KB |
Output is correct |
19 |
Correct |
286 ms |
41880 KB |
Output is correct |
20 |
Correct |
343 ms |
42692 KB |
Output is correct |
21 |
Correct |
308 ms |
41680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
5188 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
3 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4952 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
5184 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
273 ms |
38748 KB |
Output is correct |
16 |
Correct |
300 ms |
41108 KB |
Output is correct |
17 |
Correct |
162 ms |
25416 KB |
Output is correct |
18 |
Correct |
725 ms |
23620 KB |
Output is correct |
19 |
Correct |
654 ms |
23012 KB |
Output is correct |
20 |
Correct |
670 ms |
23128 KB |
Output is correct |
21 |
Correct |
685 ms |
22096 KB |
Output is correct |
22 |
Correct |
664 ms |
22608 KB |
Output is correct |
23 |
Correct |
757 ms |
22868 KB |
Output is correct |
24 |
Correct |
681 ms |
23384 KB |
Output is correct |
25 |
Correct |
437 ms |
25020 KB |
Output is correct |
26 |
Correct |
345 ms |
24916 KB |
Output is correct |
27 |
Correct |
384 ms |
26892 KB |
Output is correct |
28 |
Correct |
357 ms |
23540 KB |
Output is correct |
29 |
Correct |
332 ms |
22864 KB |
Output is correct |
30 |
Correct |
349 ms |
24628 KB |
Output is correct |
31 |
Correct |
400 ms |
23640 KB |
Output is correct |
32 |
Correct |
291 ms |
41812 KB |
Output is correct |
33 |
Correct |
244 ms |
40776 KB |
Output is correct |
34 |
Correct |
297 ms |
38744 KB |
Output is correct |
35 |
Correct |
233 ms |
35676 KB |
Output is correct |
36 |
Correct |
333 ms |
44180 KB |
Output is correct |
37 |
Correct |
319 ms |
43400 KB |
Output is correct |
38 |
Correct |
339 ms |
44996 KB |
Output is correct |
39 |
Correct |
293 ms |
41296 KB |
Output is correct |
40 |
Correct |
303 ms |
40992 KB |
Output is correct |
41 |
Correct |
298 ms |
40268 KB |
Output is correct |
42 |
Correct |
267 ms |
38736 KB |
Output is correct |
43 |
Correct |
362 ms |
44996 KB |
Output is correct |
44 |
Correct |
347 ms |
44668 KB |
Output is correct |
45 |
Correct |
365 ms |
43468 KB |
Output is correct |
46 |
Correct |
266 ms |
39504 KB |
Output is correct |
47 |
Correct |
249 ms |
38224 KB |
Output is correct |
48 |
Correct |
282 ms |
37952 KB |
Output is correct |
49 |
Correct |
324 ms |
39764 KB |
Output is correct |
50 |
Correct |
286 ms |
41880 KB |
Output is correct |
51 |
Correct |
343 ms |
42692 KB |
Output is correct |
52 |
Correct |
308 ms |
41680 KB |
Output is correct |
53 |
Correct |
751 ms |
23540 KB |
Output is correct |
54 |
Correct |
778 ms |
23888 KB |
Output is correct |
55 |
Correct |
849 ms |
24860 KB |
Output is correct |
56 |
Correct |
880 ms |
25992 KB |
Output is correct |
57 |
Correct |
903 ms |
27024 KB |
Output is correct |
58 |
Correct |
715 ms |
23628 KB |
Output is correct |
59 |
Correct |
987 ms |
28596 KB |
Output is correct |
60 |
Correct |
939 ms |
23376 KB |
Output is correct |
61 |
Correct |
639 ms |
21588 KB |
Output is correct |
62 |
Correct |
837 ms |
24600 KB |
Output is correct |
63 |
Correct |
1056 ms |
23232 KB |
Output is correct |
64 |
Correct |
886 ms |
26496 KB |
Output is correct |
65 |
Correct |
745 ms |
23500 KB |
Output is correct |
66 |
Correct |
901 ms |
25604 KB |
Output is correct |
67 |
Correct |
748 ms |
22552 KB |
Output is correct |
68 |
Correct |
798 ms |
28248 KB |
Output is correct |
69 |
Correct |
332 ms |
41004 KB |
Output is correct |
70 |
Correct |
252 ms |
38736 KB |
Output is correct |
71 |
Correct |
354 ms |
44232 KB |
Output is correct |
72 |
Correct |
138 ms |
24296 KB |
Output is correct |
73 |
Correct |
179 ms |
24456 KB |
Output is correct |
74 |
Correct |
155 ms |
27580 KB |
Output is correct |
75 |
Correct |
417 ms |
25428 KB |
Output is correct |
76 |
Correct |
417 ms |
25144 KB |
Output is correct |
77 |
Correct |
411 ms |
24912 KB |
Output is correct |
78 |
Correct |
387 ms |
26564 KB |
Output is correct |