#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,k;
int Max[100005],d[100005];
vector<pair<int,int>> Gaux[100005];
vector<int> G[100005];
int l[100005];
multiset<int,greater<int>> s;
void debug(vector<int> c)
{
for(auto it : c)
{
cerr<<it<<' ';
}
cerr<<'\n';
}
void Add(int val)
{
s.insert(val);
}
int kmax()
{
int sum = 0;
int cnt = 0;
for(auto it=s.begin();it!=s.end() && cnt<k;it++)
{
sum += *it;
++cnt;
}
return sum;
}
void preset(int nod, int dad = 0)
{
for(auto it : Gaux[nod])
{
if(it.first==dad)
{
continue;
}
preset(it.first,nod);
d[it.first] = it.second;
}
}
void dfs(int nod, int dad = 0)
{
for(auto it : G[nod])
{
if(it==dad)
{
continue;
}
dfs(it,nod);
if(l[it] + d[it] > l[Max[nod]] + d[Max[nod]])
{
Max[nod] = it;
}
}
l[nod] = l[Max[nod]] + d[Max[nod]];
for(auto it : G[nod])
{
if(it==dad || it==Max[nod])
{
continue;
}
Add(l[it] + d[it]);
}
}
signed main()
{
cin>>n>>k;
for(int i=1; i<n; i++)
{
int x,y,c;
cin>>x>>y>>c;
Gaux[x].push_back({y,c});
Gaux[y].push_back({x,c});
G[x].push_back(y);
G[y].push_back(x);
}
for(int i=1; i<=n; i++)
{
s.clear();
for(int j=1;j<=n;j++)
{
Max[j] = 0, d[j] = 0, l[j] = 0;
}
preset(i);
dfs(i);
Add(l[i]);
cout<<kmax()<<'\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4960 KB |
Output is correct |
3 |
Correct |
7 ms |
5048 KB |
Output is correct |
4 |
Correct |
6 ms |
4948 KB |
Output is correct |
5 |
Correct |
6 ms |
4948 KB |
Output is correct |
6 |
Correct |
5 ms |
4948 KB |
Output is correct |
7 |
Correct |
6 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4960 KB |
Output is correct |
3 |
Correct |
7 ms |
5048 KB |
Output is correct |
4 |
Correct |
6 ms |
4948 KB |
Output is correct |
5 |
Correct |
6 ms |
4948 KB |
Output is correct |
6 |
Correct |
5 ms |
4948 KB |
Output is correct |
7 |
Correct |
6 ms |
4948 KB |
Output is correct |
8 |
Correct |
101 ms |
5140 KB |
Output is correct |
9 |
Correct |
91 ms |
5196 KB |
Output is correct |
10 |
Correct |
59 ms |
5128 KB |
Output is correct |
11 |
Correct |
88 ms |
5152 KB |
Output is correct |
12 |
Correct |
69 ms |
5140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4960 KB |
Output is correct |
3 |
Correct |
7 ms |
5048 KB |
Output is correct |
4 |
Correct |
6 ms |
4948 KB |
Output is correct |
5 |
Correct |
6 ms |
4948 KB |
Output is correct |
6 |
Correct |
5 ms |
4948 KB |
Output is correct |
7 |
Correct |
6 ms |
4948 KB |
Output is correct |
8 |
Correct |
101 ms |
5140 KB |
Output is correct |
9 |
Correct |
91 ms |
5196 KB |
Output is correct |
10 |
Correct |
59 ms |
5128 KB |
Output is correct |
11 |
Correct |
88 ms |
5152 KB |
Output is correct |
12 |
Correct |
69 ms |
5140 KB |
Output is correct |
13 |
Correct |
465 ms |
5292 KB |
Output is correct |
14 |
Correct |
395 ms |
5380 KB |
Output is correct |
15 |
Correct |
225 ms |
5268 KB |
Output is correct |
16 |
Correct |
420 ms |
5412 KB |
Output is correct |
17 |
Correct |
307 ms |
5264 KB |
Output is correct |
18 |
Correct |
252 ms |
5248 KB |
Output is correct |
19 |
Correct |
465 ms |
5292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1086 ms |
18788 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4960 KB |
Output is correct |
3 |
Correct |
7 ms |
5048 KB |
Output is correct |
4 |
Correct |
6 ms |
4948 KB |
Output is correct |
5 |
Correct |
6 ms |
4948 KB |
Output is correct |
6 |
Correct |
5 ms |
4948 KB |
Output is correct |
7 |
Correct |
6 ms |
4948 KB |
Output is correct |
8 |
Correct |
101 ms |
5140 KB |
Output is correct |
9 |
Correct |
91 ms |
5196 KB |
Output is correct |
10 |
Correct |
59 ms |
5128 KB |
Output is correct |
11 |
Correct |
88 ms |
5152 KB |
Output is correct |
12 |
Correct |
69 ms |
5140 KB |
Output is correct |
13 |
Correct |
465 ms |
5292 KB |
Output is correct |
14 |
Correct |
395 ms |
5380 KB |
Output is correct |
15 |
Correct |
225 ms |
5268 KB |
Output is correct |
16 |
Correct |
420 ms |
5412 KB |
Output is correct |
17 |
Correct |
307 ms |
5264 KB |
Output is correct |
18 |
Correct |
252 ms |
5248 KB |
Output is correct |
19 |
Correct |
465 ms |
5292 KB |
Output is correct |
20 |
Execution timed out |
1086 ms |
18788 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |