#include <bits/stdc++.h>
#include "traffic.h"
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair <ll, ll> pii;
const int N = 1e6 + 7;
int sum;
int a[N], siz[N];
vector <int> adj[N];
int dfs(int x, bool rev, int p = -1, int d = sum) {
if (!rev) {
siz[x] = a[x];
for (auto y : adj[x]) {
if (y == p) continue;
siz[x] += dfs(y, 0, x);
}
return siz[x];
}
else {
int mx = d;
for (auto y : adj[x]) if (y != p) mx = max(mx, siz[y]);
int mn = mx;
for (auto y : adj[x]) if (y != p) mn = min(mn, dfs(y, 1, x, sum-siz[y]));
return mn;
}
}
int LocateCentre(int n, int pp[], int S[], int D[]) {
for (int i = 0; i < n; i++) a[i] = pp[i], sum += pp[i];
for (int i = 0; i < n-1; i++) {
adj[S[i]].pb(D[i]);
adj[D[i]].pb(S[i]);
}
dfs(0, 0);
return dfs(0, 1);
}
//-----------------------------------------
/*
static int NN,PP[1000000],SS[1000000],DD[1000000];
int main(){
int i;
scanf("%d",&NN);
for (i=0;i<NN;i++) scanf("%d",&PP[i]);
for (i=0;i<NN-1;i++) scanf("%d%d",&SS[i],&DD[i]);
int r = LocateCentre(NN,PP,SS,DD);
printf("%d\n",r);
return 0;
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23896 KB |
Output is correct |
2 |
Incorrect |
10 ms |
23896 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23896 KB |
Output is correct |
2 |
Incorrect |
10 ms |
23896 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23896 KB |
Output is correct |
2 |
Incorrect |
10 ms |
23896 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23896 KB |
Output is correct |
2 |
Incorrect |
10 ms |
23896 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |