# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
917887 | trMatherz | 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 <iostream> //cin, cout
#include <race.h>
/*
#include <fstream>
std::ifstream cin ("ex.in");
std::ofstream cout ("ex.out");
*/
// includes
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <vector>
#include <array>
#include <algorithm>
#include <numeric>
#include <iomanip>
#include <unordered_set>
#include <stack>
#include <ext/pb_ds/assoc_container.hpp>
#include <random>
#include <chrono>
//usings
using namespace std;
using namespace __gnu_pbds;
// misc
#define ll long long
#define pb push_back
#define pq priority_queue
#define ub upper_bound
#define lb lower_bound
template<typename T, typename U> bool emin(T &a, const U &b){ return b < a ? a = b, true : false; }
template<typename T, typename U> bool emax(T &a, const U &b){ return b > a ? a = b, true : false; }
typedef uint64_t hash_t;
// vectors
#define vi vector<int>
#define vvi vector<vi>
#define vvvi vector<vvi>
#define vpii vector<pair<int, int>>
#define vvpii vector<vector<pair<int, int>>>
#define vppipi vector<pair<int, pair<int, int>>>
#define vl vector<ll>
#define vvl vector<vl>
#define vvvl vector<vvl>
#define vpll vector<pair<ll, ll>>
#define vb vector<bool>
#define vvb vector<vb>
#define vs vector<string>
#define sz(x) (int)x.size()
#define rz resize
#define all(x) x.begin(), x.end()
// pairs
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define f first
#define s second
// sets
#define si set<int>
#define sl set<ll>
#define ss set<string>
#define in insert
template <class T> using iset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// maps
#define mii map<int, int>
#define mll map<ll, ll>
// loops
#define FR(x, z, y) for (int x = z; x < y; x++)
#define FRE(x, z, y) FR(x, z, y + 1)
#define F(x, y) FR(x, 0, y)
#define FE(x, y) F(x, y + 1)
#define A(x, y) for(auto &x : y)
vvpii a;
vl dis, sum;
vector<mll> v;
ll an = 0;
int n, k;
void pre(int x, int p, ll c, int d){
v[x][c] = d;
sum[x] = c;
dis[x] = d;
A(u, a[x]) if(u.f != p) pre(u.f, x, c + u.s, d + 1);
}
void dfs(int x, int p){
ll tar = k + 2 * sum[x];
A(u, a[x]){
if(u.s == p) continue;
dfs(u.f, x);
if(sz(v[u.f]) > sz(v[x])) swap(v[u.f], v[x]);
A(k, v[u.f]){
if(v[x].find(tar - k.f) != v[x].end()) emin(an, v[x][tar - k.f] + k.s - 2 * dis[x]);
}
A(k, v[u.f]){
if(v[x].find(k.f) == v[x].end()) v[x].in(k);
else emin(v[x][k.f], k.s);
}
}
}
int best_path(int N, int K, int H[][2], int L[]){
if(K == 1) return 0;
n = N, m = K;
an = n + 7;
a = vvpii(n);
dis = sum = vl(n, 0);
v = vector<mll>(n);
F(i, n - 1){
a[H[i][0]].pb(H[i][1]);
a[H[i][1]].pb(H[i][0]);
}
pre(0, -1, 0, 0);
dfs(0, -1);
return (an == n + 7 ? -1 : an);
}