이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
vector<vector<size_t>> adj;
vector<vector<pair<size_t, int>>> graph;
const int oo = INT_MAX / 3;
int& rd(map<int, int>& m, int key)
{
auto it = m.find(key);
if(it == m.end()) it = m.emplace_hint(it, key, +oo);
return it->second;
}
void mini(map<int, int>& m, int key, int value)
{
rd(m, key) = min(rd(m, key), value);
}
void dfs_map_paths(size_t u, int s, int d, map<int, int>& m,
size_t glock = SIZE_MAX, size_t lock = SIZE_MAX)
{
if(d > +oo) return;
mini(m, s, d);
for(auto [v, w] : graph[u])
if(v != lock and v != glock)
dfs_map_paths(v, s + w, d + 1, m, glock, u);
}
size_t gl_vertices;
vector<size_t> gl_parent, gl_subsize;
void dfs_root(size_t u, size_t glock = SIZE_MAX, size_t lock = SIZE_MAX)
{
gl_vertices++;
gl_subsize[u] = 1;
for(auto v : adj[u])
if(v != lock and v != glock)
gl_parent[v] = u, dfs_root(v, glock, u), gl_subsize[u] += gl_subsize[v];
}
size_t subtree_size(size_t from, size_t to)
{
return from == gl_parent[to] ? gl_subsize[to] : gl_vertices - gl_subsize[from];
}
size_t find_centroid(size_t init, size_t lock = SIZE_MAX)
{
(void)lock;
gl_vertices = 0;
dfs_root(init, lock);
gl_parent[init] = SIZE_MAX;
size_t u = init;
while(true)
{
bool any = false;
for(auto v : adj[u])
if(v != lock and subtree_size(u, v) > subtree_size(v, u))
{ any = true; u = v; break; }
if(not any) break;
}
return u;
}
int solve(size_t init, int k, size_t lock = SIZE_MAX)
{
auto fix = find_centroid(init, lock);
int result = +oo;
map<int, int> pre = {{0, 0}};
for(auto [v, w] : graph[fix])
if(v != lock)
{
map<int, int> cur;
dfs_map_paths(v, w, 1, cur, fix);
for(auto [s, d] : cur)
result = min(result, d + rd(pre, k - s));
for(auto [s, d] : cur)
mini(pre, s, d);
}
for(auto v : adj[fix])
if(v != lock)
result = min(result, solve(v, k, fix));
return result;
}
int best_path(int _n, int k, int H[][2], int L[])
{
const size_t n = _n;
graph.resize(n);
adj.resize(n);
for(size_t i = 0; i < n - 1; i++)
{
graph[H[i][0]].emplace_back(H[i][1], L[i]);
graph[H[i][1]].emplace_back(H[i][0], L[i]);
adj[H[i][0]].push_back(H[i][1]);
adj[H[i][1]].push_back(H[i][0]);
}
gl_parent.resize(n);
gl_subsize.resize(n);
int result = solve(0, k);
return result >= +oo ? -1 : result;
}
#ifdef XHOVA
#define MAX_N 500000
static int N, K;
static int H[MAX_N][2];
static int L[MAX_N];
static int solution;
inline
void my_assert(int e) {if (!e) abort();}
void read_input()
{
int i;
my_assert(2==scanf("%d %d",&N,&K));
for(i=0; i<N-1; i++)
my_assert(3==scanf("%d %d %d",&H[i][0],&H[i][1],&L[i]));
my_assert(1==scanf("%d",&solution));
}
int main()
{
int ans;
read_input();
ans = best_path(N,K,H,L);
if(ans==solution)
printf("Correct.\n");
else
printf("Incorrect. Returned %d, Expected %d.\n",ans,solution);
return 0;
}
#endif
# | 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... |