#include <bits/stdc++.h>
#include "race.h"
#define ll long long
#define deb(x) cout << #x << ':' << ' ' << x << endl;
using namespace std;
struct tpos
{
ll v, w;
};
ll freq[1<<18], subarbol[1<<18], padre[1<<18];
bool visi[1<<18];
ll resp = 1LL<<62;
ll N, k;
vector<tpos> adj[1<<18];
ll centroide (ll yo, ll pa, ll n)
{
for (tpos v: adj[yo])
{
if (v.v == pa) continue;
if (subarbol[v.v] >= n/2) return centroide(v.v, yo, n);
}
return yo;
}
ll dfs(ll yo, ll pa)
{
subarbol[yo] = 1;
padre[yo] = pa;
for (tpos v: adj[yo])
{
if (v.v == pa) continue;
subarbol[yo] += dfs(v.v, yo);
}
return subarbol[yo];
}
ll MM;
void dfs2 (ll yo, ll pa, ll peso, ll camino)
{
if (peso > k) return;
if (peso == k) resp = min(resp, camino);
//cout << MM << ':' << ' ' << yo << ' ' << peso << endl;
if (freq[k-peso] != 0) resp = min(resp, camino+freq[k-peso]);
if (freq[peso] == 0) freq[peso] = camino;
freq[peso] = min(camino, freq[peso]);
for (tpos v: adj[yo])
{
if (v.v == pa) continue;
if (visi[v.v]) continue;
dfs2(v.v, yo, peso + v.w, camino+1);
}
}
void dfs3(ll yo, ll pa, ll peso)
{
if (peso > k) return;
freq[peso] = 0;
for (tpos v: adj[yo])
{
if (v.v == pa) continue;
dfs3(v.v, yo, peso + v.w);
}
}
int iter = 0;
void responde (ll pa, ll mero_mero)
{
dfs2(mero_mero, -1, 0, 0);
MM = mero_mero;
//deb(mero_mero);
visi[mero_mero] = 1;
dfs3(mero_mero, -1, 0);
for (tpos v: adj[mero_mero])
{
if (v.v == pa) continue;
if (visi[v.v]) continue;
dfs(v.v, mero_mero);
ll mm = centroide(v.v, mero_mero, subarbol[v.v]);
responde(-1, mm);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
k = K;
//cout << N << ' ' << K << endl;
for (ll i = 0; i < N-1; i++)
{
adj[H[i][1]].push_back({H[i][0], L[i]});
adj[H[i][0]].push_back({H[i][1], L[i]});
}
dfs(0, -1);
ll mm = centroide(0, -1, subarbol[0]);
MM = mm;
//responde(padre[mm], mm);
if (resp == 1LL<<62) resp = -1;
return resp;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
12636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
12636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
12636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
12636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |