# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1096140 |
2024-10-04T01:35:20 Z |
Cadoc |
Race (IOI11_race) |
C++14 |
|
0 ms |
0 KB |
/*
Author: Cadocx
Codeforces: https://codeforces.com/profile/Kadoc
VNOJ: oj.vnoi.info/user/Cadoc
*/
#include <bits/stdc++.h>
using namespace std;
// input/output
#define fastIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define el cout << '\n'
#define debug(x) cout << #x << " = " << x << '\n'
#define execute cerr << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << "s"
// #pragma GCC optimize("O2", "unroll-loops", "Ofast")
// #pragma GCC target("avx,avx2,fma")
//data type
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define piv pair<int, vector<int>>
#define vi vector<int>
#define vl vector<ll>
#define vc vector<char>
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return 1; }; return 0; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return 1; }; return 0; }
//STL
#define sz(x) (int)(x).size()
#define FOR(i,l,r) for(auto i = l; i <= r; i++)
#define FORD(i,r,l) for(auto i = r; i >= l; i--)
#define forin(i,a) for(auto i : a)
#define pb push_back
#define eb emplace_back
#define pf push_front
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
//bitmask
#define bitcnt(n) __builtin_popcount(n)
#define mask(i) (1 << (i))
#define bit(n, i) (((n) >> (i)) & 1)
#define set_on(n, i) ((n) | mask(i))
#define set_off(n, i) ((n) & ~mask(i))
//constant
#define maxN 200005
#define MOD 1000230007
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define base 31
#define Kadoc 0
int n, k;
int H[maxN][2], L[maxN];
int pos[maxN], p[maxN << 2][22], Timer = 0;
int h[maxN], d[maxN];
int sz[maxN], del[maxN];
vector<pii> g[maxN];
int f[1000005];
vector<int> cache;
int Ans = INF;
void dfs(int u, int pr = -1){
pos[u] = ++Timer;
p[Timer][0] = u;
for(auto [v, w]:g[u]) if(v != pr){
h[v] = h[u] + 1;
d[v] = d[u] + w;
dfs(v, u);
p[++Timer][0] = u;
}
}
void dfs_sz(int u, int p = -1){
sz[u] = 1;
for(auto [v, w]:g[u]) if(v != p && !del[v]){
dfs_sz(v, u);
sz[u] += sz[v];
}
}
int getMin(int u, int v){
return (h[u] < h[v]? u:v);
}
int lca(int u, int v){
int l = pos[u], r = pos[v];
if(l > r) swap(l, r);
int k = 31 - __builtin_clz(r-l+1);
return getMin(p[l][k], p[r-mask(k)+1][k]);
}
int dist(int u, int v){
return h[u] + h[v] - 2 * h[lca(u, v)];
}
int distW(int u, int v){
return d[u] + d[v] - 2 * d[lca(u, v)];
}
void upd(int u, int root, int p = -1){
int hu = dist(u, root), du = distW(u, root);
if(k >= du){
minimize(f[du], hu);
cache.pb(du);
}
for(auto [v, w]:g[u]) if(v != p && !del[v]) upd(v, root, u);
}
void get(int u, int root, int p = -1){
int hu = dist(u, root), du = distW(u, root);
if(k >= du) minimize(Ans, hu + f[k - du]);
for(auto [v, w]:g[u]) if(v != p && !del[v]) get(v, root, u);
}
int centroid(int u, int p, int n){
for(auto [v, w]:g[u]) if(v != p && !del[v] && sz[v] > n/2) return centroid(v, u, n);
return u;
}
void cd(int u, int p = -1){
dfs_sz(u);
u = centroid(u, p, sz[u]);
del[u] = 1;
f[0] = 0;
for(auto [v, w]:g[u]) if(!del[v]){
get(v, u);
upd(v, u);
}
for(int depth:cache) f[depth] = INF;
cache.clear();
for(auto [v, w]:g[u]) if(!del[v]) cd(v, u);
}
int best_path(int N, int K, int H[][2], int L[]){
n = N; k = K;
FOR(i, 0, n-2){
int u = H[i][0], v = H[i][1], w = L[i];
g[u].pb({v, w});
g[v].pb({u, w});
}
dfs(1);
for(int j=1; mask(j)<=Timer; ++j) for(int i=1; i+mask(j)-1<=Timer; ++i){
p[i][j] = getMin(p[i][j-1], p[i+mask(j-1)][j-1]);
}
memset(f, 0x3f, sizeof f);
cd(1);
if(Ans >= INF) Ans = -1;
return Ans;
}
void solve(){
cin >> n >> k;
FOR(i, 0, n-2){
int u, v, w; cin >> u >> v >> w;
H[i][0] = ++u;
H[i][1] = ++v;
L[i] = w;
}
cout << best_path(n, k, H, L);
}
int main(){
#define NAME "TASK"
if(fopen(NAME".inp", "r")){
freopen(NAME".inp", "r", stdin);
freopen(NAME".out", "w", stdout);
}
fastIO;
if(Kadoc){
int tc; cin >> tc;
while(tc--){
solve();
}
} else solve();
}
Compilation message
race.cpp: In function 'void dfs(int, int)':
race.cpp:66:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
66 | for(auto [v, w]:g[u]) if(v != pr){
| ^
race.cpp: In function 'void dfs_sz(int, int)':
race.cpp:76:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
76 | for(auto [v, w]:g[u]) if(v != p && !del[v]){
| ^
race.cpp: In function 'void upd(int, int, int)':
race.cpp:109:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
109 | for(auto [v, w]:g[u]) if(v != p && !del[v]) upd(v, root, u);
| ^
race.cpp: In function 'void get(int, int, int)':
race.cpp:116:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
116 | for(auto [v, w]:g[u]) if(v != p && !del[v]) get(v, root, u);
| ^
race.cpp: In function 'int centroid(int, int, int)':
race.cpp:120:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
120 | for(auto [v, w]:g[u]) if(v != p && !del[v] && sz[v] > n/2) return centroid(v, u, n);
| ^
race.cpp: In function 'void cd(int, int)':
race.cpp:131:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
131 | for(auto [v, w]:g[u]) if(!del[v]){
| ^
race.cpp:139:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
139 | for(auto [v, w]:g[u]) if(!del[v]) cd(v, u);
| ^
race.cpp: In function 'int main()':
race.cpp:178:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
178 | freopen(NAME".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
race.cpp:179:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
179 | freopen(NAME".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/ld: /tmp/cc7s2CmY.o: in function `main':
race.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccrkQeJY.o:grader.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status