# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|
876333 | | MON | Race (IOI11_race) | C++14 | | 182 ms | 16724 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 "race.h"
#include<vector>
#include<unordered_map>
#include<bitset>
#include<algorithm>
#define fi first
#define se second
using namespace std;
using pii = pair<int,int>;
using ll = long long;
constexpr int NMAX = 2e5 + 1;
vector<pii> fii[NMAX]; bitset<NMAX> scos;
int sz[NMAX],n,k,mx,ans; unordered_map<int,int> m;
unordered_map<int,bool> avem;
void build(int r,int p = -1)
{
sz[r] = 1;
for(auto &it : fii[r])
if(it.first != p && !scos[it.first])
build(it.first,r),sz[r] += sz[it.first];
}
int centroid(int r,int n,int p = -1)
{
for(auto &it : fii[r])
if(it.first != p && !scos[it.first] && sz[it.first] * 2 > n)
return centroid(it.first,n,r);
return r;
}
void dfs(int a,ll cost,int d,int p,bool add)
{
if(d > k) return;
if(add)
{
bool &u = avem[cost];
if(!u) u = 1 , m[cost] = d;
else
{
int &h = m[cost];
h = min(h,d);
}
}
else
{
bool &u = avem[k-cost];
if(u) ans = min(ans,m[k-cost]+d);
}
for(auto &it : fii[a])
if(!scos[it.fi] && it.fi != p) dfs(it.fi,cost+it.se,d+1,a,add);
mx = max(mx,d);
}
void descompune(int root)
{
build(root); int c = centroid(root,sz[root]); avem[0] = 1;
for(auto &it : fii[c])
if(!scos[it.first])
dfs(it.fi,it.se,1,c,0),
dfs(it.fi,it.se,1,c,1);
mx = 0; scos[c] = 1;
m.clear(); avem.clear();
for(auto &it : fii[c])
if(!scos[it.fi]) descompune(it.fi);
}
int best_path(int N,int K,int H[][2],int L[])
{
n = N,k = K; ans = n + 1;
for(int i = 0 ; i < n - 1; i ++)
{
fii[H[i][0]].push_back({H[i][1],L[i]});
fii[H[i][1]].push_back({H[i][0],L[i]});
}
descompune(0); if(ans == n + 1) ans = -1;
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |