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;
const int MAXN=1e6+5;
vector<int> adj[MAXN];
int subtree[MAXN],atas[MAXN];
void dfs(int now,int par){
atas[now]=par;
for(auto nxt : adj[now]){
if(nxt==par)continue;
dfs(nxt,now);
subtree[now]+=subtree[nxt];
}
}
int LocateCentre(int N, int P[], int S[], int D[])
{
for(int i=0;i<N;i++)subtree[i]=P[i];
for(int i=0;i<N-1;i++){
adj[S[i]].push_back(D[i]);
adj[D[i]].push_back(S[i]);
}
dfs(0,-1);
int ans=INT_MAX,idx;
for(int i=0;i<N;i++){
int ret=0;
for(auto j : adj[i]){
if(j==atas[i])ret=max(ret,subtree[0]-subtree[i]);
else ret=max(ret,subtree[j]);
}
// cout << i << " " << ret << '\n';
if(ret<ans){
ans=ret;
idx=i;
}
}
return idx;
}
//int main(){
// int N;
// cin >> N;
// int P[N],S[N-1],D[N-1];
// for(int i=0;i<N;i++)cin >> P[i];
// for(int i=0;i<N-1;i++)cin >> S[i] >> D[i];
// cout << LocateCentre(N,P,S,D) << '\n';
// return 0;
//}
Compilation message (stderr)
traffic.cpp: In function 'int LocateCentre(int, int*, int*, int*)':
traffic.cpp:38:9: warning: 'idx' may be used uninitialized in this function [-Wmaybe-uninitialized]
38 | return idx;
| ^~~
# | 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... |