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 <bits/stdc++.h>
#include "traffic.h"
using namespace std;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
//#undef LOCALKL
#define IO \
ios_base::sync_with_stdio(0);(cin).tie(0);(cout).tie(0)
#define y1 y1_
#define prev prev_
#define all(a) (a).begin(),(a).end()
#define pb push_back
#define eb emplace_back
#define F first
#define S second
#ifdef LOCALKL
// #define cerr cerr<<"\33[1;32m"
// #define cout cout<<"\33[0m"
#else
#ifndef OO
#define endl '\n'
#define cerr if(1){}else cerr
#endif
#endif
#define OK cout<<"OK\n"<<endl;
#define setpre(k) fixed<<setprecision(k)
#define mmset(k,y) memset(k,y,sizeof(k))
#define sz(x) ((int)(x).size())
using pii = pair<int,int>;
using pll = pair<long long,long long>;
using ull = unsigned long long;
using intt = long long;
using ll = long long;
using ld = long double;
const ll m9 = 998244353;
const ll m7 = 1000000007;
const ll m18 = 1000000000000000000;
const ll i127 = 2139062143;
const ll l127 = 9187201950435737471;
vector<int>g[1000001];
int *a;
pll p[1000001];
pll ans = {LLONG_MAX, 0};
pll dfs(int v, int parent)
{
ll sum=0, res=0;
for(int u : g[v])
if(u!=parent)
{
pll x = dfs(u, v);
sum+=x.F;
res+=x.S;
}
p[v]={sum, sum+res};
// cerr<<v<<'-'<<p[v].F<<'-'<<p[v].S<<endl;
return {sum+a[v], sum+res};
}
// {sum, res}
void dfs2(int v, int parent, pll up)
{
// ll sum=0, res=0;
// cerr<<v<<' '<<up.F<<' '<<up.S<<endl;
for(int u : g[v])
if(u!=parent)
{
dfs2(u, v, {up.F+p[v].F-p[u].F+a[v]-a[u], (up.F+p[v].F-p[u].F+a[v]-a[u])+(up.S+p[v].S-(p[u].S+p[u].F+a[u]))});
}
ans=min(ans, {up.S+p[v].S, v});
}
// 2 1 3 3 0
int LocateCentre(int n, int pp[], int S[], int D[])
{
a=pp;
for(int i=0;i<n-1;i++)
{
g[S[i]].emplace_back(D[i]);
g[D[i]].emplace_back(S[i]);
}
dfs(0, -1);
dfs2(0, -1, {0,0});
cerr<<"> "<<ans.F<<endl;
return ans.S;
}
# | 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... |