This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//https://pastebin.com/jVURaNCJ
#include<bits/stdc++.h>
#include <cstdio>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define N 200005
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define vi vector<int>
#define mii map<int,int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define w(x) int x; cin>>x; while(x--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
void c_p_c()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}
vector<int>gr[N];
map<int, map<int, int>>edge;
map<int, int>m[N];
int depth[N];
int fin_ans = inf;
int val[N];
int k;
int dfs1(int node, int par)
{
int ans = 0;
for (auto child : gr[node])
{
if (child != par)
{
ans = max(ans, dfs1(child, node) + 1);
}
}
return depth[node] = ans;
}
int dfs(int node, int par)
{
///cout << node << "++";
int sz = 0;
int ind;
for (auto child : gr[node])
{
// cout << child << endl;
if (child != par )
{
//cout << child << endl;
int temp = dfs(child, node);
if (temp > sz)
{
sz = temp;
ind = child;
}
}
}
if (sz == 0) {
val[node] = 0;
m[node][0] = 0;
return 1;
}
for (auto child : gr[node])
{
if (child == par)
continue;
int to_find = k - edge[node][child];
//cout << "to_find" << to_find << endl;
if (to_find == 0)
fin_ans = 1;
to_find = to_find - val[child];
if (m[child].find(to_find) != m[child].end())
{
//cout << "came" << endl;
fin_ans = min(fin_ans, 1 + depth[child] - m[child][to_find]);
}
}
m[node].swap(m[ind]);
val[node] = val[ind] + edge[node][ind];
m[node][edge[node][ind] - val[node]] = depth[node] - 1;
////////////////////////////////alll deal with child to node
// and inserted bigger_child and done with that
for (auto child : gr[node])
{
if (child != par && child != ind)
{
// go in this
for (auto i : m[child])
{
int num = i.ff + val[child] + edge[node][child];
int to_find = k - num - val[node];
if (m[node].find(to_find) != m[node].end())
{
// cout << "came" << endl;
fin_ans = min(fin_ans, depth[node] - m[node][to_find] + depth[child] - i.ss + 1);
}
}
int findd = k - edge[node][child];
if (m[node].find(findd) != m[node].end())
{
//
// cout << "came" << endl;
fin_ans = min(fin_ans, depth[node] - m[node][findd] + 1);
}
for (auto i : m[child])
{
int num = i.ff + val[child];
int to_insert = num - val[node];
int d = i.ss;
int newd = depth[node] - 1 - (depth[child] - i.ss);
if (m[node].find(to_insert) != m[node].end())
{
if (m[node][to_insert] < newd)
m[node][to_insert] = newd;
}
else
m[node][to_insert] = newd;
}
m[node][edge[node][child] - val[node]] = depth[node] - 1;
}
}
//cout << node << " " << val[node] << " " << depth[node] << " " << fin_ans << endl;
return m[node].size();
}
int best_path(int32_t n, int32_t K, int32_t H[][2], int32_t L[]) {
int i, j, a, b, c;
//scanf("%lld%lld",&n,&l);
k = K;
for (int i = 0; i <= n; i++)
{
gr[i].clear();
m[i].clear();
depth[i] = 0;
}
fin_ans = inf;
edge.clear();
for (i = 0; i < n - 1; i++) {
//scanf("%lld%lld%lld",&a,&b,&c);
//a++;b++;
a = H[i][0]; b = H[i][1]; c = L[i];
a++; b++;
//cout << a << b << c << endl;
edge[a][b] = c;
edge[b][a] = c;
gr[a].pb(b);
gr[b].pb(a);
}
dfs1(1, 0);
dfs(1, 0);
if (fin_ans == inf)
return -1;
return (int32_t)fin_ans;
}
Compilation message (stderr)
race.cpp: In function 'long long int dfs(long long int, long long int)':
race.cpp:132:13: warning: unused variable 'd' [-Wunused-variable]
132 | int d = i.ss;
| ^
race.cpp: In function 'long long int best_path(int32_t, int32_t, int32_t (*)[2], int32_t*)':
race.cpp:151:10: warning: unused variable 'j' [-Wunused-variable]
151 | int i, j, a, b, c;
| ^
# | 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... |