#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
typedef long long ll;
typedef long double ld;
const int mod = 1e9+7;
const int maxn = 2e5+5;
const ll inf = 1e18+5;
ll dist = inf;
int pos;
int n, k;
ll dp[maxn];
ll E[maxn];
vector<pair<int, int>> g[maxn];
void dfs(int x, int msk, int p = 0, ll d = 0){
//cout << x << " " << msk << "\n";
if(msk&(1<<(x-1)))dist = min(dist, d);
for(auto v: g[x]){
if(v.f == p)continue;
dfs(v.f, msk, x, d+v.s);
}
}
void dfs1(int x, int p){
for(auto v: g[x]){
if(v.f == p)continue;
dfs1(v.f, x);
dp[x] = max(dp[x], dp[v.f] + v.s);
}
}
void solve(int x, int p, ll d){
//cout << x << " " << d << "\n";
if(max(d, dp[x]) < dist){
dist = max(d, dp[x]);
pos = x;
}
vector<pair<int, int>> cc;
for(auto v: g[x])if(v.f != p)cc.push_back(v);
if(cc.empty())return;
vector<ll> pfx(cc.size()), sfx(cc.size());
pfx[0] = dp[cc[0].f]+cc[0].s;
sfx[cc.size()-1] = dp[cc[cc.size()-1].f]+cc[cc.size()-1].s;
for(int i = 1; i < cc.size(); i++)pfx[i] = max(pfx[i-1], dp[cc[i].f] + cc[i].s);
for(int i = cc.size()-2; i >= 0; i--)sfx[i] = max(sfx[i+1], dp[cc[i].f]+cc[i].s);
for(int i = 0; i < cc.size(); i++){
ll tmp = d;
if(i)tmp = max(tmp, pfx[i-1]);
if(i+1 < cc.size())tmp = max(tmp, sfx[i+1]);
solve(cc[i].f, x, tmp+cc[i].s);
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for(int i = 0; i < n-1; i++){
int a, b, w;
cin >> a >> b >> w;
E[i] = w;
g[a].push_back({b, w});
g[b].push_back({a, w});
}
ll ans = inf;
if(n <= 20){
for(int i = 0; i < (1<<n) ; i++){
if(__builtin_popcount(i) != k)continue;
ll mx = 0;
for(int j = 1; j <= n; j++){
dist = inf;
dfs(j, i);
mx = max(mx, dist);
}
if(ans > mx){
ans = mx;
pos = i;
}
}
cout << ans << "\n";
for(int i = 0; i < n; i++)if(pos&(1<<i))cout << i+1 << " ";
}
else if(k == 1){
dfs1(1, 0);
solve(1, 0, 0);
cout << dist << "\n" << pos;
}
}
Compilation message
Main.cpp: In function 'void solve(int, int, ll)':
Main.cpp:52:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int i = 1; i < cc.size(); i++)pfx[i] = max(pfx[i-1], dp[cc[i].f] + cc[i].s);
| ~~^~~~~~~~~~~
Main.cpp:54:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for(int i = 0; i < cc.size(); i++){
| ~~^~~~~~~~~~~
Main.cpp:57:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | if(i+1 < cc.size())tmp = max(tmp, sfx[i+1]);
| ~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
4948 KB |
Output is correct |
2 |
Correct |
7 ms |
4948 KB |
Output is correct |
3 |
Correct |
9 ms |
5024 KB |
Output is correct |
4 |
Correct |
7 ms |
4948 KB |
Output is correct |
5 |
Correct |
7 ms |
5028 KB |
Output is correct |
6 |
Correct |
44 ms |
4948 KB |
Output is correct |
7 |
Correct |
46 ms |
5008 KB |
Output is correct |
8 |
Correct |
18 ms |
4948 KB |
Output is correct |
9 |
Correct |
212 ms |
4948 KB |
Output is correct |
10 |
Correct |
20 ms |
5076 KB |
Output is correct |
11 |
Correct |
106 ms |
5008 KB |
Output is correct |
12 |
Correct |
211 ms |
5004 KB |
Output is correct |
13 |
Correct |
10 ms |
5028 KB |
Output is correct |
14 |
Correct |
104 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
66560 KB |
Output is correct |
2 |
Correct |
114 ms |
68268 KB |
Output is correct |
3 |
Correct |
81 ms |
19124 KB |
Output is correct |
4 |
Correct |
137 ms |
14836 KB |
Output is correct |
5 |
Correct |
152 ms |
14568 KB |
Output is correct |
6 |
Correct |
127 ms |
14584 KB |
Output is correct |
7 |
Correct |
134 ms |
14720 KB |
Output is correct |
8 |
Correct |
134 ms |
15180 KB |
Output is correct |
9 |
Correct |
151 ms |
14896 KB |
Output is correct |
10 |
Correct |
135 ms |
15304 KB |
Output is correct |
11 |
Correct |
119 ms |
19716 KB |
Output is correct |
12 |
Correct |
126 ms |
21168 KB |
Output is correct |
13 |
Correct |
117 ms |
22592 KB |
Output is correct |
14 |
Correct |
101 ms |
20664 KB |
Output is correct |
15 |
Correct |
117 ms |
19180 KB |
Output is correct |
16 |
Correct |
105 ms |
21028 KB |
Output is correct |
17 |
Correct |
101 ms |
19052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
12672 KB |
Unexpected end of file - int64 expected |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
4948 KB |
Output is correct |
2 |
Correct |
7 ms |
4948 KB |
Output is correct |
3 |
Correct |
9 ms |
5024 KB |
Output is correct |
4 |
Correct |
7 ms |
4948 KB |
Output is correct |
5 |
Correct |
7 ms |
5028 KB |
Output is correct |
6 |
Correct |
44 ms |
4948 KB |
Output is correct |
7 |
Correct |
46 ms |
5008 KB |
Output is correct |
8 |
Correct |
18 ms |
4948 KB |
Output is correct |
9 |
Correct |
212 ms |
4948 KB |
Output is correct |
10 |
Correct |
20 ms |
5076 KB |
Output is correct |
11 |
Correct |
106 ms |
5008 KB |
Output is correct |
12 |
Correct |
211 ms |
5004 KB |
Output is correct |
13 |
Correct |
10 ms |
5028 KB |
Output is correct |
14 |
Correct |
104 ms |
4948 KB |
Output is correct |
15 |
Correct |
102 ms |
66560 KB |
Output is correct |
16 |
Correct |
114 ms |
68268 KB |
Output is correct |
17 |
Correct |
81 ms |
19124 KB |
Output is correct |
18 |
Correct |
137 ms |
14836 KB |
Output is correct |
19 |
Correct |
152 ms |
14568 KB |
Output is correct |
20 |
Correct |
127 ms |
14584 KB |
Output is correct |
21 |
Correct |
134 ms |
14720 KB |
Output is correct |
22 |
Correct |
134 ms |
15180 KB |
Output is correct |
23 |
Correct |
151 ms |
14896 KB |
Output is correct |
24 |
Correct |
135 ms |
15304 KB |
Output is correct |
25 |
Correct |
119 ms |
19716 KB |
Output is correct |
26 |
Correct |
126 ms |
21168 KB |
Output is correct |
27 |
Correct |
117 ms |
22592 KB |
Output is correct |
28 |
Correct |
101 ms |
20664 KB |
Output is correct |
29 |
Correct |
117 ms |
19180 KB |
Output is correct |
30 |
Correct |
105 ms |
21028 KB |
Output is correct |
31 |
Correct |
101 ms |
19052 KB |
Output is correct |
32 |
Incorrect |
66 ms |
12672 KB |
Unexpected end of file - int64 expected |
33 |
Halted |
0 ms |
0 KB |
- |