# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
958330 | IsaL | Bosses (BOI16_bosses) | C++17 | 0 ms | 0 KiB |
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 "traffic.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
const int NN = 1000069;
const ll mod = 1e9 + 7;
int tc, ans;
ll sub[NN], tot, dp[NN], a[NN];
vector<int> al[NN];
void dfs(int x, int p)
{
sub[x] = a[x];
for(auto z : al[x])
{
if(z == p) continue;
dfs(z, x);
sub[x] += sub[z];
}
}
void dfs2(int x, int p)
{
for(auto z : al[x])
{
if(z == p) continue;
dfs2(z, x);
dp[x] = max(dp[x], sub[z]);
}
dp[x] = max(dp[x], tot - sub[x]);
}
int LocateCentre(int N, int P[], int S[], int D[])
{
int i, j;
ans = 0;
for(i = 0; i < N; i++) tot += (ll)P[i], a[i] = (ll)P[i];
for(i = 0; i < N-1; i++)
{
al[S[i]].push_back(D[i]);
al[D[i]].push_back(S[i]);
}
dfs(0, -1);
dfs2(0, 0);
for(i = 1; i < N; i++) if(dp[ans] > dp[i]) ans = i;
return ans;
}
// int main()
// {
// ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
// int i, j;
// int n;
// cin >> n;
// int P[n], S[n], D[n];
// for(i = 0; i < n; i++) cin >> P[i];
// for(i = 0; i < n-1; i++) cin >> S[i] >> D[i];
// cout << LocateCentre(n, P, S, D);
// for(i = 0; i < n; i++)
// {
// cout << i << " : " << sub[i] << " " << dp[i] << '\n';
// }
// }