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>
#include<iomanip>
#include<algorithm>
#include<stack>
#include<queue>
#include<string>
#include<string.h>
#include<cmath>
#include<vector>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<cstdio>
#include<bitset>
#include<chrono>
#include<random>
#include<ext/rope>
/* ordered_set
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
*/
#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e6 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e9;
int n, k;
vector<pair<int, int>> adj[maxN];
int vis[maxN];
int sz[maxN], parCent[maxN];
int f[maxN];
int res = oo;
int dfs(int u, int par)
{
sz[u] = 1;
for(auto a : adj[u])
{
int v = a.fi;
if(v == par || vis[v]) continue;
sz[u] += dfs(v, u);
}
return sz[u];
}
int Find_Centroid(int u, int par, int val)
{
for(auto a : adj[u])
{
int v = a.fi;
if(v == par || vis[v]) continue;
if(sz[v] > val / 2) Find_Centroid(v, u, val);
}
return u;
}
int get(int u, int par, int d, int cnt)
{
if(d > k) return oo;
int res = f[k - d] + cnt;
for(auto a : adj[u])
{
int v = a.fi, w = a.se;
if(v == par || vis[v] || d + w > k) continue;
res = min(res, get(v, u, d + w, cnt + 1));
}
return res;
}
void add(int u, int par, int d, int cnt)
{
if(d > k) return;
f[d] = min(f[d], cnt);
for(auto a : adj[u])
{
int v = a.fi, w = a.se;
if(v == par || vis[v] || d + w > k) continue;
add(v, u, d + w, cnt + 1);
}
}
void del(int u, int par, int d)
{
if(d > k) return;
f[d] = oo;
for(auto a : adj[u])
{
int v = a.fi, w = a.se;
if(v == par || vis[v] || d + w > k) continue;
del(v, u, d + w);
}
}
int Decompose(int u)
{
int root = Find_Centroid(u, 0, dfs(u, 0));
vis[root] = -1;
f[0] = 0;
for(auto a : adj[root])
{
int v = a.fi, w = a.se;
if(vis[v]) continue;
res = min(res, get(v, root, w, 1));
add(v, root, w, 1);
}
del(root, -1, 0);
for(auto a : adj[root])
{
int v = a.fi;
if(vis[v]) continue;
parCent[Decompose(v)] = root;
}
return root;
}
int best_path(int N, int K, int H[][2], int L[2])
{
n = N;
k = K;
for(int i=0; i<n; i++)
{
H[i][0]++;
H[i][1]++;
adj[H[i][0]].pb({H[i][1], L[i]});
adj[H[i][1]].pb({H[i][0], L[i]});
}
fill(f, f + maxN, oo);
Decompose(1);
return (res < oo ? res : -1);
}
void ReadInput()
{
cin >> n >> k;
for(int i=1; i<n; i++)
{
int u, v, w;
cin >> u >> v >> w;
u++;
v++;
adj[u].pb({v, w});
adj[v].pb({u, w});
}
}
/*int main()
{
freopen("x.inp", "r", stdin);
ReadInput();
Solve();
}*/
# | 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... |