이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fr first
#define sc second
#define clr(a, x) memset(a, x, sizeof(a))
#define dbg(x) cout<<"("<<#x<<"): "<<x<<endl;
#define printvector(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout<<*it<<" "; cout<<endl;
#define all(v) v.begin(), v.end()
#define lcm(a, b) (a * b)/__gcd(a, b)
#define printvecpairs(vec) for(auto it: vec) cout<<it.fr<<' '<<it.sc<<endl;
#define endl '\n'
#define float long double
#define lli long long int
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 5;
vector<int> adj[MAXN];
int value[MAXN];
lli sz[MAXN] = {0};
lli dfs(int s, int p){
lli ans = value[s];
for(auto v: adj[s]){
if(v ==p) continue;
ans += dfs(v, s);
}
sz[s] = ans;
return ans;
}
lli ans = LLONG_MAX;
lli res = 0;
void dfs2(int s, int p, int last){
lli mx = last;
lli sum = last;
for(auto v: adj[s]){
if(v==p) continue;
sum += sz[v];
mx = max(mx, sz[v]);
}
if(ans > mx){
ans = mx;
res = s;
}
for(auto v: adj[s]){
if(v==p) continue;
lli newlast = sum-sz[v];
newlast += value[s];
dfs2(v, s, newlast);
}
}
int LocateCentre(int n, int p[], int d[], int s[]) {
sz[0] = 0;
value[0]= 0;
for(int i= 0;i<n-1;i++){
d[i]++; s[i]++;
adj[d[i]].pb(s[i]);
adj[s[i]].pb(d[i]);
}
for(int i= 0;i<n;i++){
value[i+1] = p[i];
}
dfs(1, -1);
dfs2(1, 0, 0);
return res-1;
}
// signed main() {
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
// #ifdef LOCAL
// freopen("input.txt", "r", stdin);
// #endif
// int n;
// cin>>n;
// int val[n];
// for(int i= 0;i<n;i++){
// cin>>val[i];
// }
// int a[n], b[n];
// for(int i= 0;i<n-1;i++){
// cin>>a[i]>>b[i];
// }
// cout<<LocateCentre(n, val, a, b)<<endl;
// 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... |