#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e6 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
int n, k;
vector<pair<int, int>> adj[maxN];
int f[maxN];
void ReadInput()
{
cin >> n >> k;
for(int i=1; i<n; i++)
{
int u, v, w;
cin >> u >> v >> w;
adj[u].pb({v, w});
adj[v].pb({u, w});
}
}
void predfs(int u, int par)
{
for(auto a : adj[u])
{
int v = a.fi, w = a.se;
if(v == par) continue;
predfs(v, u);
f[u] = max(f[u], f[v] + w);
}
}
vector<int> vc;
void dfs(int u, int par, int root)
{
int _max = 0, id = 0;
for(auto a : adj[u])
{
int v = a.fi, w = a.se;
if(v == par) continue;
dfs(v, u, root);
//if(u == 3) cout << f[4] << " ";
if(f[v] + w > _max)
{
_max = f[v] + w;
id = v;
}
}
// cout << u << " " << _max << "\n";
for(auto a : adj[u])
{
int v = a.fi, w = a.se;
if(u != root && (v == par || v == id)) continue;
vc.pb(f[v] + w);
}
}
void Solve()
{
for(int i=1; i<=n; i++)
{
fill(f, f + n + 1, 0);
vc.clear();
predfs(i, 0);
dfs(i, 0, i);
sort(vc.begin(), vc.end(), greater<int>());
int res = 0;
for(int i=0; i<k; i++) res += vc[i];
cout << res << '\n';
}
}
int32_t main()
{
// freopen("x.inp", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ReadInput();
Solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
15 ms |
23788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
15 ms |
23788 KB |
Output is correct |
3 |
Correct |
14 ms |
23828 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
14 ms |
23796 KB |
Output is correct |
6 |
Correct |
14 ms |
23820 KB |
Output is correct |
7 |
Correct |
15 ms |
23764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
15 ms |
23788 KB |
Output is correct |
3 |
Correct |
14 ms |
23828 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
14 ms |
23796 KB |
Output is correct |
6 |
Correct |
14 ms |
23820 KB |
Output is correct |
7 |
Correct |
15 ms |
23764 KB |
Output is correct |
8 |
Correct |
66 ms |
23892 KB |
Output is correct |
9 |
Correct |
70 ms |
23964 KB |
Output is correct |
10 |
Correct |
50 ms |
23920 KB |
Output is correct |
11 |
Correct |
62 ms |
23904 KB |
Output is correct |
12 |
Correct |
61 ms |
23908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
15 ms |
23788 KB |
Output is correct |
3 |
Correct |
14 ms |
23828 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
14 ms |
23796 KB |
Output is correct |
6 |
Correct |
14 ms |
23820 KB |
Output is correct |
7 |
Correct |
15 ms |
23764 KB |
Output is correct |
8 |
Correct |
66 ms |
23892 KB |
Output is correct |
9 |
Correct |
70 ms |
23964 KB |
Output is correct |
10 |
Correct |
50 ms |
23920 KB |
Output is correct |
11 |
Correct |
62 ms |
23904 KB |
Output is correct |
12 |
Correct |
61 ms |
23908 KB |
Output is correct |
13 |
Correct |
264 ms |
24012 KB |
Output is correct |
14 |
Correct |
245 ms |
24100 KB |
Output is correct |
15 |
Correct |
173 ms |
24020 KB |
Output is correct |
16 |
Correct |
237 ms |
23996 KB |
Output is correct |
17 |
Correct |
206 ms |
23884 KB |
Output is correct |
18 |
Correct |
160 ms |
24108 KB |
Output is correct |
19 |
Correct |
284 ms |
23892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1066 ms |
32348 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
15 ms |
23788 KB |
Output is correct |
3 |
Correct |
14 ms |
23828 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
14 ms |
23796 KB |
Output is correct |
6 |
Correct |
14 ms |
23820 KB |
Output is correct |
7 |
Correct |
15 ms |
23764 KB |
Output is correct |
8 |
Correct |
66 ms |
23892 KB |
Output is correct |
9 |
Correct |
70 ms |
23964 KB |
Output is correct |
10 |
Correct |
50 ms |
23920 KB |
Output is correct |
11 |
Correct |
62 ms |
23904 KB |
Output is correct |
12 |
Correct |
61 ms |
23908 KB |
Output is correct |
13 |
Correct |
264 ms |
24012 KB |
Output is correct |
14 |
Correct |
245 ms |
24100 KB |
Output is correct |
15 |
Correct |
173 ms |
24020 KB |
Output is correct |
16 |
Correct |
237 ms |
23996 KB |
Output is correct |
17 |
Correct |
206 ms |
23884 KB |
Output is correct |
18 |
Correct |
160 ms |
24108 KB |
Output is correct |
19 |
Correct |
284 ms |
23892 KB |
Output is correct |
20 |
Execution timed out |
1066 ms |
32348 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |