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 ll long long
#define pi pair<int, int>
#define endl '\n'
void setIO(string name = "") {
ios_base::sync_with_stdio(0);
cin.tie(0);
if (name.length()) {
freopen((name+".in").c_str(), "r", stdin);
freopen((name+".out").c_str(), "w", stdout);
}
}
struct node{
int id, person;
vector<node*> adj;
node(int i, int p, vector<node*> a){
id = i;
person = p;
adj = a;
}
};
int n;
node* A[1000000];
ll dfs(node* curr, int prev){
ll sum = curr->person;
for(node* next : curr->adj){
if(prev != next->id){
sum += dfs(next, curr->id);
}
}
return sum;
}
ll root(node* root){
ll ret = 0;
for(node* next : root->adj){
ret = max(ret, dfs(next, root->id) - root->person);
}
return ret;
}
int LocateCentre(int n, int* p, int* s, int* d){
for(int i = 0; i < n; i++){
A[i] = new node(i, p[i], vector<node*>());
}
for(int i = 0; i < n-1; i++){
A[s[i]]->adj.push_back(A[d[i]]);
A[d[i]]->adj.push_back(A[s[i]]);
}
ll res = LLONG_MAX, ret = -1;
for(int i = 0; i < n; i++){
ll curr = root(A[i]);
if(curr < res){
res = curr;
ret = A[i]->id;
}
}
return ret;
}
/*int main(){
setIO("");
int* p = new int[5];
int* s = new int[4];
int* d = new int[4];
p[0] = 10, p[1] = 10, p[2] = 10, p[3] = 20, p[4] = 20;
s[0] = 0, s[1] = 1, s[2] = 2, s[3] = 3;
d[0] = 2, d[1] = 2, d[2] = 3, d[3] = 4;
cout << LocateCentre(5, p, s, d) << endl;;
return 0;
}*/
Compilation message (stderr)
traffic.cpp: In function 'void setIO(std::string)':
traffic.cpp:12:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
12 | freopen((name+".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
traffic.cpp:13:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
13 | freopen((name+".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |