# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1101832 |
2024-10-17T01:27:55 Z |
shidou26 |
Race (IOI11_race) |
C++14 |
|
0 ms |
0 KB |
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<int, int> ii;
const int N = 1e6 + 3;
const int INF = 1e9 + 3;
int n, k;
vector<ii> adj[N];
int deepest = 0, answer = INF;
int siz[N], dp[N];
bool is_centroid[N];
void input() {
cin >> n >> k;
for(int i = 1; i < n; i++) {
int u, v, d; cin >> u >> v >> d;
u++; v++;
// cout << u << " " << v << endl;
adj[u].push_back({v, d});
adj[v].push_back({u, d});
}
}
void dfs(int u, int previous) {
siz[u] = 1;
for(ii p : adj[u]) {
int v = p.first;
if(v != previous && !is_centroid[v]) {
dfs(v, u);
siz[u] += siz[v];
}
}
}
int find_centroid(int u, int tree_size, int previous) {
for(ii p : adj[u]) {
int v = p.first;
if(v != previous && !is_centroid[v] && siz[v] * 2 > tree_size) {
return find_centroid(v, tree_size, u);
}
}
return u;
}
void get(int u, int previous, int height, int edge) {
if(height <= k) answer = min(answer, edge + dp[k - height]);
// cout << u << " " << edge << " " << height << endl;
for(ii p : adj[u]) {
int v, d; tie(v, d) = p;
if(v != previous && !is_centroid[v]) {
get(v, u, height + d, edge + 1);
}
}
}
void update(int u, int previous, int height, int edge) {
if(height <= k) dp[height] = min(dp[height], edge), deepest = max(deepest, height);
for(ii p : adj[u]) {
int v, d; tie(v, d) = p;
if(v != previous && !is_centroid[v]) {
update(v, u, height + d, edge + 1);
}
}
}
void build_centroid(int u) {
dfs(u, -1);
int centroid = find_centroid(u, siz[u], -1);
deepest = 0; dp[0] = 0;
for(ii p : adj[centroid]) {
int v, d; tie(v, d) = p;
if(!is_centroid[v]) {
get(v, centroid, d, 1);
update(v, centroid, d, 1);
// cout << endl;
}
}
for(int i = 0; i <= deepest; i++) dp[i] = INF;
is_centroid[centroid] = true;
for(ii p : adj[centroid]) {
int v = p.first;
if(!is_centroid[v]) {
// cout << v << endl;
build_centroid(v);
}
}
}
void process() {
for(int i = 0; i <= k; i++) dp[i] = INF;
build_centroid(1);
cout << (answer == INF ? -1 : answer) << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#define task "DateALive"
if(fopen(task".INP", "r")) {
freopen(task".INP", "r", stdin);
freopen(task".OUT", "w", stdout);
}
input();
process();
return 0;
}
Compilation message
race.cpp: In function 'int main()':
race.cpp:111:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
111 | freopen(task".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
race.cpp:112:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
112 | freopen(task".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/ld: /tmp/cc7FFzIP.o: in function `main':
race.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/cccfUj6R.o:grader.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/cccfUj6R.o: in function `main':
grader.cpp:(.text.startup+0x28): undefined reference to `best_path(int, int, int (*) [2], int*)'
collect2: error: ld returned 1 exit status