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>
#include "traffic.h"
#define ll long long
#define pii pair<int, int>
#define st first
#define nd second
#define pb push_back
using namespace std;
const int maxN = 1e6 + 10; //maximum number of N possible
vector<int> adj[maxN]; // adjacency list of the tree
int prt[maxN], dis[maxN]; // prt contains parent of the node dis contains the sum of people in lower nodes (and also in itself)
void dfs(int i, int p) {
//loop through all neighbors
for (auto j : adj[i]) {
//if j is the parent just ignore it
if (j == p) continue;
//recursion call, run dfs at node j to find dis[j]
dfs(j, i);
//after getting dis[j] add dis[j] to dis[i]
dis[i] += dis[j];
//set parent of j to be i
prt[j] = i;
}
}
int LocateCentre(int N, int P[], int S[], int D[]) {
for (int i = 0; i < N - 1; ++i) {
//initializing the adjacency list
adj[S[i]].pb(D[i]);
adj[D[i]].pb(S[i]);
}
for (int i = 0; i < N; ++i) {
//set all dis to be their own number
dis[i] = P[i];
}
//start dfs to gain dis and prt we'll let 0 be the root node and 0 will have no parent so just put in -1
dfs(0, -1);
//set ans to max possible number of people
//c will contains the centre with min traffic jam
int ans = 2e9, c;
for (int i = 0; i < N; ++i) {
//let tmp = 0, tmp will be the max number of dis going out of node i
//note that we only need to check the nodes that is neighbors of node i for the max people in traffic
int tmp = 0;
for (auto j : adj[i]) {
//if j is parent people going to to node j is sum of people - people that didn't go to j
if (j == prt[i]) tmp = max(tmp, dis[0] - dis[i] );
//else it's just the dis that we maintain
else tmp = max(tmp, dis[j]);
// use max to find the maximum traffic jam if the centre is at i
}
//if the tmp is less than our ans, we change the c (the centre) and the ans
if (tmp < ans) c = i, ans = tmp;
}
return c;
}
Compilation message (stderr)
traffic.cpp: In function 'int LocateCentre(int, int*, int*, int*)':
traffic.cpp:63:12: warning: 'c' may be used uninitialized in this function [-Wmaybe-uninitialized]
63 | return c;
| ^
# | 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... |