# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1113048 |
2024-11-15T14:23:25 Z |
Matkap |
Race (IOI11_race) |
C++17 |
|
0 ms |
0 KB |
/*
Author:Matkap(prefix_sum)
*/
#include <bits/stdc++.h>
#include <array>
using namespace std;
#define int long long
#define endl "\n"
#define ar array
#define all(x) x.begin(),x.end()
const int INF = 1e17 , MOD = 1e9 + 7;
const int mxN = 2e5 + 7;
vector<ar<int,2>> adj[mxN];
int rem[mxN];
int sub[mxN];
int ans = INF;
int k1;
int find_subtree_size(int pos,int par = -1)
{
sub[pos] = 1;
for(auto [it, dist] : adj[pos])
{
if(it == par || rem[it]) continue;
sub[pos] += find_subtree_size(it, pos);
}
return sub[pos];
}
int find_centroid(int sub_size, int pos,int par = -1)
{
for(auto [it, dist] : adj[pos])
{
if(it == par || rem[it]) continue;
if(sub[it] > sub_size / 2) return find_centroid(sub_size, it, pos);
}
return pos;
}
void solve(int pos,int par,int cur_path, int depth = 1)
{
if(cur_path > k1) return;
if(cur_path == k1)
{
ans = min(ans, depth);
}
for(auto [it, dist] : adj[pos])
{
if(rem[it]) continue;
solve(it,pos,cur_path + dist,depth + 1);
}
}
void build_decomp(int pos)
{
int centroid = find_centroid(find_subtree_size(pos), pos);
rem[centroid] = 1;
for(auto [it, dist] : adj[centroid])
{
if(!rem[it])
{
solve(it,-1,dist);
}
}
for(auto [it, dist] : adj[centroid])
{
if(rem[it]) continue;
build_decomp(it);
}
}
int best_path(int n,int k, int h[][2], int l[])
{
k1 = k;
for(int i = 0;n - 1 > i;i++)
{
adj[h[i][0]].push_back({h[i][1], l[i]});
adj[h[i][0]].push_back({h[i][0], l[i]});
}
build_decomp(0);
if(ans == INF) return -1;
return ans;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int h[1000][2];
int l[1000];
int n;
int k;
cin >> n >> k;
for(int i = 0;n - 1 > i;i++) for(int j = 0;2 > j;j++) cin >> h[i][j];
for(int i = 0;n - 1 > i;i++) cin >> l[i];
cout << best_path(n, k, h, l) << endl;
}
Compilation message
/usr/bin/ld: /tmp/ccooxupu.o: in function `main':
race.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/cc0te5Ws.o:grader.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/cc0te5Ws.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