# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
479624 | Protostar | Race (IOI11_race) | C++14 | 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>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
using namespace std;
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("O3")
#pragma GCC target ("avx")
typedef long long ll;
typedef long double ld;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#define Confundo ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define minheap priority_queue<int,vector<int>,greater<int>>
#define print2d(dp,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++)cout<<dp[i][j]<<" ";cout<<"\n";}
#define ftt int t;cin>>t;for(int tt=1;tt<=t;++tt)
#define Sum(v) accumulate(v.begin(),v.end(),(ll)0)
#define Rev(v) reverse(v.begin(),v.end());
#define Sort(v) sort(v.begin(),v.end());
#define Input(v) for(auto &x:v) cin>>x;
#define Output(v) for(auto x:v) cout<<x<<" ";
#define mem(a, b) memset(a, b, sizeof(a))
#define dbgx(x) cout<<"\nhi "<<x<<"\n"
#define double long double
#define int long long
#define maxheap priority_queue<int>
#define dbg cout<<"\nhi\n"
#define sayy cout<<"YES\n"
#define sayn cout<<"NO\n"
#define pii pair<int,int>
#define mii map<int,int>
#define umii unordered_map<int,int>
#define vii vector<pair<int,int>>
#define vvi vector<vector<int>>
#define vb vector<bool>
#define vi vector<int>
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define snd second
#define fst first
#define endl "\n"
const int INF = numeric_limits<int>::max() / 2;
const double PI = 3.1415926535898;
const int MOD = 1e9 + 7;
const int LIM = 2e5 + 5;
// O(log y)
int fpow(int x, int y) {
int temp;
if (y == 0)
return 1;
temp = fpow(x, y / 2);
if (y % 2 == 0)
return (temp * temp) % MOD;
else
return (x * ((temp * temp) % MOD)) % MOD;
}
// O(log max(a, b))
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
set<pii> adj[LIM];
vector<int> visited;
int k, ans, sz[LIM], arr[1000000 + 5];
int dfsForSize(int u, int p)
{
sz[u] = 1;
for (auto x : adj[u])
{
if (x.fst != p)
sz[u] += dfsForSize(x.fst, u);
}
return sz[u];
}
int findCentroid(int u, int p, int n)
{
for (auto x : adj[u])
{
if (x.fst != p)
{
if (sz[x.fst] > n / 2)
return findCentroid(x.fst, u, n);
}
}
return u;
}
void dfs1(int u, int p, int curLevel, int curLength)
{
if (curLength <= k)
ans = min(ans, arr[k - curLength] + curLevel);
for (auto edge : adj[u])
{
if (edge.fst != p)
dfs1(edge.fst, u, curLevel + 1, curLength + edge.snd);
}
}
void dfs2(int u, int p, int curLevel, int curLength)
{
if (curLength <= k)
{
arr[curLength] = min(arr[curLength], curLevel);
visited.pb(curLength);
}
for (auto edge : adj[u])
{
if (edge.fst != p)
dfs2(edge.fst, u, curLevel + 1, curLength + edge.snd);
}
}
void centroidDecomposition(int u, int p)
{
int n = dfsForSize(u, p);
int centroid = findCentroid(u, p, n);
visited.clear();
for (auto edge : adj[centroid])
{
if (edge.fst != p)
{
dfs1(edge.fst, centroid, 1, edge.snd);
dfs2(edge.fst, centroid, 1, edge.snd);
}
}
for (int x : visited)
arr[x] = INF;
arr[0] = 0;
visited.clear();
vector<pii> v;
for (auto x : adj[centroid])
v.pb(x);
for (auto x : v)
{
pii p = {centroid, x.snd};
adj[x.fst].erase(p);
adj[centroid].erase(x);
centroidDecomposition(x.fst, centroid);
}
}
int best_path(int n, int K, int h[][2], int l[])
{
k = K;
for (int i = 0; i < LIM; ++i)
{
adj[i].clear();
sz[i] = 0;
arr[i] = INF;
}
arr[0] = 0;
for (int i = 0; i < n - 1; ++i)
{
int u = h[i][0], v = h[i][1], len = l[i];
adj[u].insert({v, len});
adj[v].insert({u, len});
}
ans = INF;
centroidDecomposition(0, -1);
if (ans == INF)
ans = -1;
return ans;
}
// int32_t main()
// {
// int n, k;
// cin >> n >> k;
// int h[n - 1][2], l[n - 1];
// for (int i = 0; i < n - 1; ++i)
// {
// int u, v, len;
// cin >> u >> v >> len;
// h[i][0] = u;
// h[i][1] = v;
// l[i] = len;
// }
// cout << best_path(n, k, h, l);
// return 0;
// }