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>
using namespace std;
ifstream fin("milkvisits.in");
ofstream fout("milkvisits.out");
#define NMAX 1000005
typedef long long ll;
vector<int> v[NMAX];
ll val[NMAX];
ll P[NMAX];
ll minim = (1<<30);
int res;
void dfs(int nod,int tata)
{
val[nod] = P[nod];
for(auto u : v[nod])
{
if(u != tata)
{
dfs(u,nod);
val[nod] += val[u];
}
}
}
void dfs2(int nod,int tata)
{
ll maxim = val[0] - val[nod];
for(auto u : v[nod])
{
if(u != tata)
{
maxim = max(maxim,val[u]);
}
}
//cout<<nod<<" "<<maxim<<endl;
if(maxim < minim)
{
minim = maxim;
res = nod;
}
for(auto u : v[nod])
{
if(u != tata)
{
dfs2(u,nod);
}
}
}
int LocateCentre(int n,int p[],int s[],int d[])
{
for(int i=0;i<=n-2;i++)
{
int a = s[i];
int b = d[i];
v[a].push_back(b);
v[b].push_back(a);
}
for(int i=0;i<=n-1;i++)
P[i] = p[i];
dfs(0,-1);
dfs2(0,-1);
return res;
}
/*
int main()
{
int p[NMAX],s[NMAX],d[NMAX];
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>p[i];
}
for(int i=0;i<=n-2;i++)
{
int a,b;
cin>>s[i]>>d[i];
}
cout<<LocateCentre(n,p,s,d);
}
*/
# | 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... |