#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 |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
5144 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
5184 KB |
Output is correct |
7 |
Correct |
3 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4952 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 |
3 ms |
4956 KB |
Output is correct |
13 |
Correct |
3 ms |
4956 KB |
Output is correct |
14 |
Correct |
3 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
300 ms |
38272 KB |
Output is correct |
2 |
Correct |
352 ms |
40272 KB |
Output is correct |
3 |
Correct |
168 ms |
24516 KB |
Output is correct |
4 |
Correct |
831 ms |
22856 KB |
Output is correct |
5 |
Correct |
820 ms |
21280 KB |
Output is correct |
6 |
Correct |
686 ms |
21328 KB |
Output is correct |
7 |
Correct |
810 ms |
21328 KB |
Output is correct |
8 |
Correct |
945 ms |
22460 KB |
Output is correct |
9 |
Correct |
946 ms |
22576 KB |
Output is correct |
10 |
Correct |
1000 ms |
22920 KB |
Output is correct |
11 |
Correct |
506 ms |
24400 KB |
Output is correct |
12 |
Correct |
548 ms |
24148 KB |
Output is correct |
13 |
Correct |
575 ms |
26196 KB |
Output is correct |
14 |
Correct |
410 ms |
23124 KB |
Output is correct |
15 |
Correct |
449 ms |
22644 KB |
Output is correct |
16 |
Correct |
467 ms |
24144 KB |
Output is correct |
17 |
Correct |
458 ms |
23380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
334 ms |
41724 KB |
Output is correct |
2 |
Correct |
334 ms |
40020 KB |
Output is correct |
3 |
Correct |
312 ms |
38004 KB |
Output is correct |
4 |
Correct |
318 ms |
35664 KB |
Output is correct |
5 |
Correct |
374 ms |
43596 KB |
Output is correct |
6 |
Correct |
397 ms |
42800 KB |
Output is correct |
7 |
Correct |
373 ms |
44276 KB |
Output is correct |
8 |
Correct |
347 ms |
40528 KB |
Output is correct |
9 |
Correct |
330 ms |
40372 KB |
Output is correct |
10 |
Correct |
337 ms |
39508 KB |
Output is correct |
11 |
Correct |
298 ms |
38380 KB |
Output is correct |
12 |
Correct |
335 ms |
44488 KB |
Output is correct |
13 |
Correct |
404 ms |
44220 KB |
Output is correct |
14 |
Correct |
369 ms |
43092 KB |
Output is correct |
15 |
Correct |
324 ms |
39248 KB |
Output is correct |
16 |
Correct |
299 ms |
37692 KB |
Output is correct |
17 |
Correct |
317 ms |
37712 KB |
Output is correct |
18 |
Correct |
346 ms |
39404 KB |
Output is correct |
19 |
Correct |
314 ms |
41264 KB |
Output is correct |
20 |
Correct |
355 ms |
42476 KB |
Output is correct |
21 |
Correct |
308 ms |
41164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4952 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
5144 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
5184 KB |
Output is correct |
7 |
Correct |
3 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4952 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 |
3 ms |
4956 KB |
Output is correct |
13 |
Correct |
3 ms |
4956 KB |
Output is correct |
14 |
Correct |
3 ms |
4956 KB |
Output is correct |
15 |
Correct |
300 ms |
38272 KB |
Output is correct |
16 |
Correct |
352 ms |
40272 KB |
Output is correct |
17 |
Correct |
168 ms |
24516 KB |
Output is correct |
18 |
Correct |
831 ms |
22856 KB |
Output is correct |
19 |
Correct |
820 ms |
21280 KB |
Output is correct |
20 |
Correct |
686 ms |
21328 KB |
Output is correct |
21 |
Correct |
810 ms |
21328 KB |
Output is correct |
22 |
Correct |
945 ms |
22460 KB |
Output is correct |
23 |
Correct |
946 ms |
22576 KB |
Output is correct |
24 |
Correct |
1000 ms |
22920 KB |
Output is correct |
25 |
Correct |
506 ms |
24400 KB |
Output is correct |
26 |
Correct |
548 ms |
24148 KB |
Output is correct |
27 |
Correct |
575 ms |
26196 KB |
Output is correct |
28 |
Correct |
410 ms |
23124 KB |
Output is correct |
29 |
Correct |
449 ms |
22644 KB |
Output is correct |
30 |
Correct |
467 ms |
24144 KB |
Output is correct |
31 |
Correct |
458 ms |
23380 KB |
Output is correct |
32 |
Correct |
334 ms |
41724 KB |
Output is correct |
33 |
Correct |
334 ms |
40020 KB |
Output is correct |
34 |
Correct |
312 ms |
38004 KB |
Output is correct |
35 |
Correct |
318 ms |
35664 KB |
Output is correct |
36 |
Correct |
374 ms |
43596 KB |
Output is correct |
37 |
Correct |
397 ms |
42800 KB |
Output is correct |
38 |
Correct |
373 ms |
44276 KB |
Output is correct |
39 |
Correct |
347 ms |
40528 KB |
Output is correct |
40 |
Correct |
330 ms |
40372 KB |
Output is correct |
41 |
Correct |
337 ms |
39508 KB |
Output is correct |
42 |
Correct |
298 ms |
38380 KB |
Output is correct |
43 |
Correct |
335 ms |
44488 KB |
Output is correct |
44 |
Correct |
404 ms |
44220 KB |
Output is correct |
45 |
Correct |
369 ms |
43092 KB |
Output is correct |
46 |
Correct |
324 ms |
39248 KB |
Output is correct |
47 |
Correct |
299 ms |
37692 KB |
Output is correct |
48 |
Correct |
317 ms |
37712 KB |
Output is correct |
49 |
Correct |
346 ms |
39404 KB |
Output is correct |
50 |
Correct |
314 ms |
41264 KB |
Output is correct |
51 |
Correct |
355 ms |
42476 KB |
Output is correct |
52 |
Correct |
308 ms |
41164 KB |
Output is correct |
53 |
Correct |
822 ms |
22868 KB |
Output is correct |
54 |
Correct |
815 ms |
23380 KB |
Output is correct |
55 |
Correct |
845 ms |
24252 KB |
Output is correct |
56 |
Correct |
753 ms |
25252 KB |
Output is correct |
57 |
Correct |
844 ms |
26456 KB |
Output is correct |
58 |
Correct |
1039 ms |
23076 KB |
Output is correct |
59 |
Correct |
1493 ms |
27844 KB |
Output is correct |
60 |
Correct |
1176 ms |
22924 KB |
Output is correct |
61 |
Correct |
913 ms |
21316 KB |
Output is correct |
62 |
Correct |
1091 ms |
24184 KB |
Output is correct |
63 |
Correct |
1196 ms |
22992 KB |
Output is correct |
64 |
Correct |
1028 ms |
25912 KB |
Output is correct |
65 |
Correct |
1039 ms |
23064 KB |
Output is correct |
66 |
Correct |
1188 ms |
25144 KB |
Output is correct |
67 |
Correct |
879 ms |
22184 KB |
Output is correct |
68 |
Correct |
1080 ms |
27424 KB |
Output is correct |
69 |
Correct |
318 ms |
40368 KB |
Output is correct |
70 |
Correct |
292 ms |
38064 KB |
Output is correct |
71 |
Correct |
373 ms |
43676 KB |
Output is correct |
72 |
Correct |
155 ms |
23484 KB |
Output is correct |
73 |
Correct |
153 ms |
23872 KB |
Output is correct |
74 |
Correct |
161 ms |
27072 KB |
Output is correct |
75 |
Correct |
537 ms |
25172 KB |
Output is correct |
76 |
Correct |
540 ms |
24720 KB |
Output is correct |
77 |
Correct |
426 ms |
24416 KB |
Output is correct |
78 |
Correct |
450 ms |
26392 KB |
Output is correct |