#pragma GCC optimize("O3")
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <cmath>
#include <iomanip>
#include <cstdio>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <deque>
#include <bitset>
#include <cstring>
#include <unordered_map>
#define endl '\n'
using namespace std;
typedef long long ll;
vector<vector< pair<pair<ll, ll>, int> >>d;
ll mx = -1, pos = 0, local = 0;
void dfs(ll v, ll par, ll cost){
if(cost > mx){
mx = cost;
pos = v;
}
for(auto i: d[v]){
if(i.first.first != par){
dfs(i.first.first, v, cost + (i.second != local ? i.first.second : 0));
}
}
}
bool dfs_2(ll v, ll par){
if(v == pos)
return true;
for(auto &i: d[v]){
if(i.first.first == par)
continue;
if(dfs_2(i.first.first, v)){
i.second = local;
return true;
}
}
return false;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll n, k;
cin >> n >> k;
d.resize(n + 1);
for(int i = 0; i < n - 1; i++){
ll x, y, c;
cin >> x >> y >> c;
d[x].push_back({{y, c}, 0});
d[y].push_back({{x, c}, 0});
}
for(int root = 1; root <= n; root++){
local = root;
ll ans = 0;
for(int step = 1; step <= k; step++){
mx = -1, pos = 0;
dfs(root, -1, 0);
bool unused = dfs_2(root, -1);
ans += mx;
}
cout << ans << endl;
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:84:18: warning: unused variable 'unused' [-Wunused-variable]
84 | bool unused = dfs_2(root, -1);
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
12 ms |
336 KB |
Output is correct |
4 |
Correct |
12 ms |
316 KB |
Output is correct |
5 |
Correct |
14 ms |
324 KB |
Output is correct |
6 |
Correct |
11 ms |
344 KB |
Output is correct |
7 |
Correct |
13 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
12 ms |
336 KB |
Output is correct |
4 |
Correct |
12 ms |
316 KB |
Output is correct |
5 |
Correct |
14 ms |
324 KB |
Output is correct |
6 |
Correct |
11 ms |
344 KB |
Output is correct |
7 |
Correct |
13 ms |
344 KB |
Output is correct |
8 |
Execution timed out |
1074 ms |
408 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
12 ms |
336 KB |
Output is correct |
4 |
Correct |
12 ms |
316 KB |
Output is correct |
5 |
Correct |
14 ms |
324 KB |
Output is correct |
6 |
Correct |
11 ms |
344 KB |
Output is correct |
7 |
Correct |
13 ms |
344 KB |
Output is correct |
8 |
Execution timed out |
1074 ms |
408 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1081 ms |
9292 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
12 ms |
336 KB |
Output is correct |
4 |
Correct |
12 ms |
316 KB |
Output is correct |
5 |
Correct |
14 ms |
324 KB |
Output is correct |
6 |
Correct |
11 ms |
344 KB |
Output is correct |
7 |
Correct |
13 ms |
344 KB |
Output is correct |
8 |
Execution timed out |
1074 ms |
408 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |