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;
#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
const int MOD = 1e9 + 7;
const int INF = 2e15;
const int MAXN = 1e5 + 5;
vector<int> adj[MAXN];
int value[MAXN];
int sz[MAXN] = {0};
int dfs(int s, int p){
int ans = value[s];
for(auto v: adj[s]){
if(v ==p) continue;
ans += dfs(v, s);
}
sz[s] = ans;
return ans;
}
int ans = INT_MAX;
void dfs2(int s, int p, int last){
int mx = last;
int sum = last;
for(auto v: adj[s]){
if(v==p) continue;
sum += sz[v];
mx = max(mx, sz[v]);
}
ans = min(ans, mx);
for(auto v: adj[s]){
if(v==p) continue;
int 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);
cout<<ans<<endl;
return 0;
}
// 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];
// }
// LocateCentre(n, val, a, b);
// return 0;
// }
Compilation message (stderr)
traffic.cpp:17:17: warning: overflow in conversion from 'double' to 'int' changes value from '2.0e+15' to '2147483647' [-Woverflow]
17 | const int INF = 2e15;
| ^~~~
# | 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... |