이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define sz(x) ((int)x.size())
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
// Hi
const int M = 1e6;
vector<int> gr[M];
int *people;
ll dfs(int i,int par){
ll res = people[i];
for(auto u:gr[i]){
if(u != par){
res += dfs(u,i);
}
}
return res;
}
ll GLocateCentre(int n,int p[],int ff[],int d[]){
// for all but small
for(int i=0;i<n-1;i++){
gr[ff[i]].pb(d[i]);
gr[d[i]].pb(ff[i]);
}
people = p;
ll res = 1e18;
int city = 0;
for(int i=0;i<n;i++){
ll x = 0;
for(auto u:gr[i]){
x = max(x,dfs(u,i));
}
if(x < res){
city = i;
res = x;
}
}
return city;
}
ll SLocateCentre(int n,int p[],int ff[],int d[]){
// assuming all the cities are in straight line
// s[i] = i , d[i] = i+1;
vector<ll> sum(n+1,0);
for(int i=0;i<n;i++){
sum[i+1] = sum[i]+p[i];
}
ll res = 1e18;
int city = 0;
for(int i=1;i<=n;i++){
ll x = max(sum[i-1],sum[n]-sum[i]);
if(x <= res){
city = i;
res = x;
}
}
return city-1;
// 1 2 3
// 0 1 3 6
}
ll LocateCentre(int n,int p[],int ff[],int d[]){
// detect subtask type
for(int i=0;i<n-1;i++){
if(ff[i] != i || d[i]!=i+1){
return GLocateCentre(n,p,ff,d);
}
}
return SLocateCentre(n,p,ff,d);
}
/*
int main(){
int p[5] = {10,10,10,10,50},s[4] = {1,2,3,4},d[4] = {0,0,0,0};
cout<<LocateCentre(5,p,s,d)<<'\n';
return 0;
}
*/
# | 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... |