#include <bits/stdc++.h>
#define taskname "cargroup"
using namespace std;
#define int long long
typedef long long ll;
typedef pair<int,int> pli;
typedef unsigned long long ull;
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
const ll maxN=2e5+10;
// a la thg chon gan nhat
// b la thg ko thoa man xa nhat
ll a[maxN],b[maxN];
const ll inf=1e15;
vector<pli>g[maxN];
#define fi first
#define se second
ll cnt=0;
vector<ll>trace;
bool in[maxN];
void dfs(ll u,ll p,ll mid)
{
a[u]=inf;
b[u]=0;
for(auto vv:g[u])
{
int v=vv.fi;
int w=vv.se;
if(v==p) continue;
dfs(v,u,mid);
}
for(auto vv:g[u])
{
int v=vv.fi;
int w=vv.se;
if(v==p) continue;
if(b[v]!=-1&&b[v]+w>mid)
{
trace.pb(v);
in[v]=true;
cnt++;
a[v]=0;
a[u]=min(a[u],w);
b[v]=-1;
}
else
{
if(b[v]!=-1) b[u]=max(b[u],b[v]+w);
a[u]=min(a[u],a[v]+w);
}
}
if(a[u]+b[u]<=mid)
{
b[u]=-1;
}
if(u==1&&b[1]!=-1)
{
cnt++;
trace.pb(1);
in[1]=true;
}
}
ll k,n;
bool check(ll mid)
{
trace.clear();
for(int i=1;i<=n;i++) in[i]=false;
cnt=0;
dfs(1,0,mid);
return cnt<=k;
}
//ll n;
void solve()
{
cin >> n >> k;
for(int i=1;i<n;i++)
{
ll u,v,w;
cin >> u >> v >> w;
g[u].pb({v,w});
g[v].pb({u,w});
}
ll low=0,high=3e14;
while(low<=high)
{
ll mid=low+high>>1;
if(check(mid)) high=mid-1;
else low=mid+1;
}
cout << low<<'\n';
check(low);
for(int i=1;i<=n;i++)
{
if(in[i]==false&&trace.size()<k) trace.pb(i),in[i]=true;
}
for(auto zz:trace) cout << zz<<' ';
}
signed main()
{
faster
// freopen(taskname".INP","r",stdin);
// freopen(taskname".OUT","w",stdout);
solve();
return 0;
}
Compilation message
Main.cpp: In function 'void dfs(ll, ll, ll)':
Main.cpp:31:13: warning: unused variable 'w' [-Wunused-variable]
31 | int w=vv.se;
| ^
Main.cpp: In function 'void solve()':
Main.cpp:89:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
89 | ll mid=low+high>>1;
| ~~~^~~~~
Main.cpp:97:38: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
97 | if(in[i]==false&&trace.size()<k) trace.pb(i),in[i]=true;
| ~~~~~~~~~~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4952 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
5032 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
354 ms |
34540 KB |
Output is correct |
2 |
Correct |
365 ms |
35344 KB |
Output is correct |
3 |
Correct |
324 ms |
19212 KB |
Output is correct |
4 |
Correct |
1034 ms |
17732 KB |
Output is correct |
5 |
Correct |
1260 ms |
17324 KB |
Output is correct |
6 |
Correct |
1373 ms |
17344 KB |
Output is correct |
7 |
Correct |
957 ms |
17440 KB |
Output is correct |
8 |
Correct |
1244 ms |
18104 KB |
Output is correct |
9 |
Correct |
1173 ms |
17756 KB |
Output is correct |
10 |
Correct |
1082 ms |
18068 KB |
Output is correct |
11 |
Correct |
706 ms |
19348 KB |
Output is correct |
12 |
Correct |
635 ms |
19436 KB |
Output is correct |
13 |
Correct |
815 ms |
20772 KB |
Output is correct |
14 |
Correct |
606 ms |
19256 KB |
Output is correct |
15 |
Correct |
673 ms |
18776 KB |
Output is correct |
16 |
Correct |
737 ms |
19548 KB |
Output is correct |
17 |
Correct |
797 ms |
18768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
371 ms |
35892 KB |
Output is correct |
2 |
Correct |
345 ms |
35032 KB |
Output is correct |
3 |
Correct |
350 ms |
33500 KB |
Output is correct |
4 |
Correct |
323 ms |
33428 KB |
Output is correct |
5 |
Correct |
387 ms |
37244 KB |
Output is correct |
6 |
Correct |
427 ms |
36280 KB |
Output is correct |
7 |
Correct |
405 ms |
37876 KB |
Output is correct |
8 |
Correct |
406 ms |
36076 KB |
Output is correct |
9 |
Correct |
343 ms |
35816 KB |
Output is correct |
10 |
Correct |
352 ms |
35192 KB |
Output is correct |
11 |
Correct |
300 ms |
34072 KB |
Output is correct |
12 |
Correct |
362 ms |
38360 KB |
Output is correct |
13 |
Correct |
387 ms |
38372 KB |
Output is correct |
14 |
Correct |
426 ms |
37076 KB |
Output is correct |
15 |
Correct |
316 ms |
35168 KB |
Output is correct |
16 |
Correct |
294 ms |
33820 KB |
Output is correct |
17 |
Correct |
292 ms |
33808 KB |
Output is correct |
18 |
Correct |
328 ms |
35396 KB |
Output is correct |
19 |
Correct |
369 ms |
36308 KB |
Output is correct |
20 |
Correct |
319 ms |
37028 KB |
Output is correct |
21 |
Correct |
325 ms |
36140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4952 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
5032 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
354 ms |
34540 KB |
Output is correct |
16 |
Correct |
365 ms |
35344 KB |
Output is correct |
17 |
Correct |
324 ms |
19212 KB |
Output is correct |
18 |
Correct |
1034 ms |
17732 KB |
Output is correct |
19 |
Correct |
1260 ms |
17324 KB |
Output is correct |
20 |
Correct |
1373 ms |
17344 KB |
Output is correct |
21 |
Correct |
957 ms |
17440 KB |
Output is correct |
22 |
Correct |
1244 ms |
18104 KB |
Output is correct |
23 |
Correct |
1173 ms |
17756 KB |
Output is correct |
24 |
Correct |
1082 ms |
18068 KB |
Output is correct |
25 |
Correct |
706 ms |
19348 KB |
Output is correct |
26 |
Correct |
635 ms |
19436 KB |
Output is correct |
27 |
Correct |
815 ms |
20772 KB |
Output is correct |
28 |
Correct |
606 ms |
19256 KB |
Output is correct |
29 |
Correct |
673 ms |
18776 KB |
Output is correct |
30 |
Correct |
737 ms |
19548 KB |
Output is correct |
31 |
Correct |
797 ms |
18768 KB |
Output is correct |
32 |
Correct |
371 ms |
35892 KB |
Output is correct |
33 |
Correct |
345 ms |
35032 KB |
Output is correct |
34 |
Correct |
350 ms |
33500 KB |
Output is correct |
35 |
Correct |
323 ms |
33428 KB |
Output is correct |
36 |
Correct |
387 ms |
37244 KB |
Output is correct |
37 |
Correct |
427 ms |
36280 KB |
Output is correct |
38 |
Correct |
405 ms |
37876 KB |
Output is correct |
39 |
Correct |
406 ms |
36076 KB |
Output is correct |
40 |
Correct |
343 ms |
35816 KB |
Output is correct |
41 |
Correct |
352 ms |
35192 KB |
Output is correct |
42 |
Correct |
300 ms |
34072 KB |
Output is correct |
43 |
Correct |
362 ms |
38360 KB |
Output is correct |
44 |
Correct |
387 ms |
38372 KB |
Output is correct |
45 |
Correct |
426 ms |
37076 KB |
Output is correct |
46 |
Correct |
316 ms |
35168 KB |
Output is correct |
47 |
Correct |
294 ms |
33820 KB |
Output is correct |
48 |
Correct |
292 ms |
33808 KB |
Output is correct |
49 |
Correct |
328 ms |
35396 KB |
Output is correct |
50 |
Correct |
369 ms |
36308 KB |
Output is correct |
51 |
Correct |
319 ms |
37028 KB |
Output is correct |
52 |
Correct |
325 ms |
36140 KB |
Output is correct |
53 |
Correct |
1040 ms |
17620 KB |
Output is correct |
54 |
Correct |
1061 ms |
18004 KB |
Output is correct |
55 |
Correct |
1129 ms |
18564 KB |
Output is correct |
56 |
Correct |
1165 ms |
18440 KB |
Output is correct |
57 |
Correct |
1138 ms |
19560 KB |
Output is correct |
58 |
Correct |
1010 ms |
17752 KB |
Output is correct |
59 |
Correct |
1138 ms |
21172 KB |
Output is correct |
60 |
Correct |
1129 ms |
18448 KB |
Output is correct |
61 |
Correct |
1004 ms |
17376 KB |
Output is correct |
62 |
Correct |
982 ms |
19000 KB |
Output is correct |
63 |
Correct |
1124 ms |
18576 KB |
Output is correct |
64 |
Correct |
1123 ms |
20580 KB |
Output is correct |
65 |
Correct |
1046 ms |
18176 KB |
Output is correct |
66 |
Correct |
986 ms |
19112 KB |
Output is correct |
67 |
Correct |
895 ms |
17404 KB |
Output is correct |
68 |
Correct |
1124 ms |
21252 KB |
Output is correct |
69 |
Correct |
340 ms |
35360 KB |
Output is correct |
70 |
Correct |
290 ms |
33672 KB |
Output is correct |
71 |
Correct |
377 ms |
37544 KB |
Output is correct |
72 |
Correct |
287 ms |
18448 KB |
Output is correct |
73 |
Correct |
312 ms |
18184 KB |
Output is correct |
74 |
Correct |
288 ms |
20148 KB |
Output is correct |
75 |
Correct |
647 ms |
19788 KB |
Output is correct |
76 |
Correct |
708 ms |
20348 KB |
Output is correct |
77 |
Correct |
627 ms |
19740 KB |
Output is correct |
78 |
Correct |
722 ms |
20952 KB |
Output is correct |