| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 848645 | efedmrlr | Race (IOI11_race) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define n_l '\n'
#define dbg(...) cout << "[" << #__VA_ARGS__ << "]: "; cout << to_string(__VA_ARGS__) << endl
template <typename T, size_t N> int SIZE(const T (&t)[N]){ return N; } template<typename T> int SIZE(const T &t){ return t.size(); } string to_string(const string s, int x1=0, int x2=1e9){ return '"' + ((x1 < s.size()) ? s.substr(x1, x2-x1+1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c){ return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1=0, int x2=1e9){ string t = ""; for(int __iii__ = min(x1,SIZE(b)), __jjj__ = min(x2, SIZE(b)-1); __iii__ <= __jjj__; ++__iii__){ t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1=0, int x2=1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if(l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2-x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if(e != l){ if(rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else{ t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if(l_v_l_v_l == 0) res += n_l; return res; } void dbgm(){;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T){ cout << to_string(H) << " | "; dbgm(T...); }
#define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgm(__VA_ARGS__); cout << endl
#define int long long int
#define MP make_pair
#define pb push_back
#define REP(i,n) for(int (i) = 0; (i) < (n); (i)++)
#include "race.h"
const double EPS = 0.00001;
const int INF = 1e9+500;
const int ALPH = 26;
const int MOD = 1e9+7;
const int M = 1e6+5;
int n,m,q, k;
int cnt[M];
int ans = INF;
vector<int> vis, subt;
vector<vector<array<int,2> > > adj;
vector<int> centers;
void subtdfs(int node, int par) {
subt[node] = 1;
for(auto c : adj[node]) {
if(c[0] == par) continue;
if(vis[c[0]]) continue;
subtdfs(c[0], node);
subt[node] += subt[c[0]];
}
}
int cdfs(int node, int par, int sz) {
for(auto c : adj[node]) {
if(c[0] == par) continue;
if(vis[c[0]]) continue;
if(subt[c[0]] > sz/2) {
return cdfs(c[0], node, sz);
}
}
return node;
}
void find_centroids() {
subtdfs(1, 0);
int tmp = cdfs(1, 0, subt[1]);
centers.pb(tmp);
int ind = 0;
vis[tmp] = 1;
centers.pb(-1);
while(ind < centers.size()-1) {
if(centers[ind] == -1) {
centers.pb(-1);
ind++;
continue;
}
for(auto c : adj[centers[ind]]) {
if(vis[c[0]]) continue;
subtdfs(c[0], 0);
if(subt[c[0]] < 2) continue;
tmp = cdfs(c[0], 0, subt[c[0]]);
vis[tmp] = 1;
centers.pb(tmp);
}
ind++;
}
}
void dfs(int node ,int par, int cost, int leng) {
if(leng > k) return;
cnt[leng] = min(cnt[leng], cost);
for(auto c : adj[node]) {
if(c[0] == par) continue;
if(vis[c[0]]) continue;
dfs(c[0], node, cost + 1, leng + c[1]);
}
}
void dfs2(int node, int par, int cost, int leng) {
if(leng > k) return;
ans = min(ans ,cnt[k - leng] + cost);
for(auto c : adj[node]) {
if(c[0] == par) continue;
if(vis[c[0]]) continue;
dfs2(c[0], node, cost + 1, leng + c[1]);
}
}
void find_paths() {
for(int j = 0; j<k+1; j++) {
cnt[j] = INF;
}
cnt[0] = 0;
vis.clear();
vis.resize(n+1);
for(auto i : centers) {
if(i == -1) {
for(int j = 0; j<k+1; j++) {
cnt[j] = INF;
}
cnt[0] = 0;
continue;
}
vis[i] = 1;
for(auto c : adj[i]) {
dfs2(c[0], i, 1, c[1]);
dfs(c[0], i, 1, c[1]);
}
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n = N;
adj.resize(n+1); subt.resize(n+1); vis.resize(n+1,0);
k = K;
for(int i = 0; i<n-1; i++) {
H[i][0]++;
H[i][1]++;
adj[H[i][0]].pb({H[i][1], L[i]});
adj[H[i][1]].pb({H[i][0], L[i]});
}
find_centroids();
find_paths();
if(ans == INF) ans = -1;
return ans;
}
