#include <bits/stdc++.h>
#include "traffic.h"
#pragma GCC optimize("Ofast,unroll-loops")
using namespace std;
#define endl '\n'
#define ll long long
#define all(x) x.begin(), x.end()
vector<int> g[1000003];
int sz[1000003], ppl[1000003];
bool visited[1000003];
void dfs(int n) {
sz[n] = ppl[n];
visited[n] = 1;
for (auto x : g[n]) {
if (visited[x]) continue;
dfs(x);
sz[n] += sz[x];
}
}
int LocateCentre(int N, int pp[], int S[], int D[]) {
for (int i = 0; i < N - 1; i++) g[S[i]].push_back(D[i]);
for (int i = 0; i < N; i++) ppl[i] = pp[i];
dfs(0);
queue<pair<int, int>> q;
q.push({0, 0});
memset(visited, 0, sizeof(visited));
visited[0] = 1;
ll best = LLONG_MAX, id = 0;
while (q.size() > 0) {
int place = q.front().first, sum = q.front().second, cnt = 0, mx = q.front().second;
q.pop();
for (auto x : g[place]) {
if (visited[x]) continue;
mx = max(mx, sz[x]);
cnt += sz[x];
}
if (mx < best) {
best = mx;
id = place;
}
for (auto x : g[place]) {
if (visited[x]) continue;
visited[x] = 1;
q.push({x, pp[place] + sum + cnt - sz[x]});
}
}
return id;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24788 KB |
Output is correct |
2 |
Correct |
14 ms |
24788 KB |
Output is correct |
3 |
Correct |
14 ms |
24700 KB |
Output is correct |
4 |
Correct |
15 ms |
24776 KB |
Output is correct |
5 |
Correct |
12 ms |
24788 KB |
Output is correct |
6 |
Correct |
14 ms |
24800 KB |
Output is correct |
7 |
Correct |
13 ms |
24788 KB |
Output is correct |
8 |
Correct |
13 ms |
24788 KB |
Output is correct |
9 |
Correct |
15 ms |
24700 KB |
Output is correct |
10 |
Correct |
13 ms |
24744 KB |
Output is correct |
11 |
Correct |
13 ms |
24696 KB |
Output is correct |
12 |
Correct |
12 ms |
24796 KB |
Output is correct |
13 |
Correct |
13 ms |
24788 KB |
Output is correct |
14 |
Correct |
16 ms |
24692 KB |
Output is correct |
15 |
Correct |
13 ms |
24700 KB |
Output is correct |
16 |
Correct |
13 ms |
24788 KB |
Output is correct |
17 |
Correct |
13 ms |
24788 KB |
Output is correct |
18 |
Correct |
13 ms |
24740 KB |
Output is correct |
19 |
Correct |
13 ms |
24700 KB |
Output is correct |
20 |
Correct |
13 ms |
24700 KB |
Output is correct |
21 |
Correct |
12 ms |
24796 KB |
Output is correct |
22 |
Correct |
15 ms |
24788 KB |
Output is correct |
23 |
Correct |
12 ms |
24696 KB |
Output is correct |
24 |
Correct |
14 ms |
24788 KB |
Output is correct |
25 |
Correct |
15 ms |
24704 KB |
Output is correct |
26 |
Correct |
13 ms |
24696 KB |
Output is correct |
27 |
Correct |
12 ms |
24788 KB |
Output is correct |
28 |
Correct |
12 ms |
24804 KB |
Output is correct |
29 |
Correct |
13 ms |
24788 KB |
Output is correct |
30 |
Correct |
16 ms |
24788 KB |
Output is correct |
31 |
Correct |
19 ms |
24788 KB |
Output is correct |
32 |
Correct |
14 ms |
24792 KB |
Output is correct |
33 |
Correct |
15 ms |
24788 KB |
Output is correct |
34 |
Correct |
15 ms |
24752 KB |
Output is correct |
35 |
Correct |
13 ms |
24832 KB |
Output is correct |
36 |
Correct |
14 ms |
24832 KB |
Output is correct |
37 |
Correct |
13 ms |
24788 KB |
Output is correct |
38 |
Correct |
13 ms |
24788 KB |
Output is correct |
39 |
Correct |
13 ms |
24868 KB |
Output is correct |
40 |
Correct |
13 ms |
24840 KB |
Output is correct |
41 |
Correct |
13 ms |
24788 KB |
Output is correct |
42 |
Correct |
12 ms |
24800 KB |
Output is correct |
43 |
Correct |
12 ms |
24780 KB |
Output is correct |
44 |
Correct |
13 ms |
24696 KB |
Output is correct |
45 |
Correct |
12 ms |
24788 KB |
Output is correct |
46 |
Correct |
13 ms |
24788 KB |
Output is correct |
47 |
Correct |
15 ms |
24700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24788 KB |
Output is correct |
2 |
Correct |
14 ms |
24788 KB |
Output is correct |
3 |
Correct |
14 ms |
24700 KB |
Output is correct |
4 |
Correct |
15 ms |
24776 KB |
Output is correct |
5 |
Correct |
12 ms |
24788 KB |
Output is correct |
6 |
Correct |
14 ms |
24800 KB |
Output is correct |
7 |
Correct |
13 ms |
24788 KB |
Output is correct |
8 |
Correct |
13 ms |
24788 KB |
Output is correct |
9 |
Correct |
15 ms |
24700 KB |
Output is correct |
10 |
Correct |
13 ms |
24744 KB |
Output is correct |
11 |
Correct |
13 ms |
24696 KB |
Output is correct |
12 |
Correct |
12 ms |
24796 KB |
Output is correct |
13 |
Correct |
13 ms |
24788 KB |
Output is correct |
14 |
Correct |
16 ms |
24692 KB |
Output is correct |
15 |
Correct |
13 ms |
24700 KB |
Output is correct |
16 |
Correct |
13 ms |
24788 KB |
Output is correct |
17 |
Correct |
13 ms |
24788 KB |
Output is correct |
18 |
Correct |
13 ms |
24740 KB |
Output is correct |
19 |
Correct |
13 ms |
24700 KB |
Output is correct |
20 |
Correct |
13 ms |
24700 KB |
Output is correct |
21 |
Correct |
12 ms |
24796 KB |
Output is correct |
22 |
Correct |
15 ms |
24788 KB |
Output is correct |
23 |
Correct |
12 ms |
24696 KB |
Output is correct |
24 |
Correct |
14 ms |
24788 KB |
Output is correct |
25 |
Correct |
15 ms |
24704 KB |
Output is correct |
26 |
Correct |
13 ms |
24696 KB |
Output is correct |
27 |
Correct |
12 ms |
24788 KB |
Output is correct |
28 |
Correct |
12 ms |
24804 KB |
Output is correct |
29 |
Correct |
13 ms |
24788 KB |
Output is correct |
30 |
Correct |
16 ms |
24788 KB |
Output is correct |
31 |
Correct |
19 ms |
24788 KB |
Output is correct |
32 |
Correct |
14 ms |
24792 KB |
Output is correct |
33 |
Correct |
15 ms |
24788 KB |
Output is correct |
34 |
Correct |
15 ms |
24752 KB |
Output is correct |
35 |
Correct |
13 ms |
24832 KB |
Output is correct |
36 |
Correct |
14 ms |
24832 KB |
Output is correct |
37 |
Correct |
13 ms |
24788 KB |
Output is correct |
38 |
Correct |
13 ms |
24788 KB |
Output is correct |
39 |
Correct |
13 ms |
24868 KB |
Output is correct |
40 |
Correct |
13 ms |
24840 KB |
Output is correct |
41 |
Correct |
13 ms |
24788 KB |
Output is correct |
42 |
Correct |
12 ms |
24800 KB |
Output is correct |
43 |
Correct |
12 ms |
24780 KB |
Output is correct |
44 |
Correct |
13 ms |
24696 KB |
Output is correct |
45 |
Correct |
12 ms |
24788 KB |
Output is correct |
46 |
Correct |
13 ms |
24788 KB |
Output is correct |
47 |
Correct |
15 ms |
24700 KB |
Output is correct |
48 |
Correct |
14 ms |
24788 KB |
Output is correct |
49 |
Correct |
12 ms |
24700 KB |
Output is correct |
50 |
Correct |
12 ms |
24788 KB |
Output is correct |
51 |
Correct |
12 ms |
24700 KB |
Output is correct |
52 |
Correct |
13 ms |
24712 KB |
Output is correct |
53 |
Correct |
13 ms |
24700 KB |
Output is correct |
54 |
Correct |
13 ms |
24744 KB |
Output is correct |
55 |
Correct |
13 ms |
24696 KB |
Output is correct |
56 |
Correct |
12 ms |
24728 KB |
Output is correct |
57 |
Correct |
12 ms |
24704 KB |
Output is correct |
58 |
Correct |
13 ms |
24788 KB |
Output is correct |
59 |
Correct |
13 ms |
24704 KB |
Output is correct |
60 |
Correct |
16 ms |
24744 KB |
Output is correct |
61 |
Correct |
14 ms |
24700 KB |
Output is correct |
62 |
Correct |
14 ms |
24788 KB |
Output is correct |
63 |
Correct |
14 ms |
24788 KB |
Output is correct |
64 |
Correct |
13 ms |
24744 KB |
Output is correct |
65 |
Correct |
13 ms |
24696 KB |
Output is correct |
66 |
Correct |
13 ms |
24788 KB |
Output is correct |
67 |
Correct |
12 ms |
24788 KB |
Output is correct |
68 |
Correct |
38 ms |
29652 KB |
Output is correct |
69 |
Correct |
47 ms |
33704 KB |
Output is correct |
70 |
Correct |
13 ms |
24788 KB |
Output is correct |
71 |
Correct |
57 ms |
37652 KB |
Output is correct |
72 |
Correct |
81 ms |
41864 KB |
Output is correct |
73 |
Correct |
83 ms |
45692 KB |
Output is correct |
74 |
Correct |
104 ms |
49768 KB |
Output is correct |
75 |
Correct |
113 ms |
53808 KB |
Output is correct |
76 |
Correct |
130 ms |
57784 KB |
Output is correct |
77 |
Correct |
140 ms |
61860 KB |
Output is correct |
78 |
Correct |
166 ms |
65904 KB |
Output is correct |
79 |
Correct |
172 ms |
69852 KB |
Output is correct |
80 |
Correct |
199 ms |
73836 KB |
Output is correct |
81 |
Correct |
15 ms |
24784 KB |
Output is correct |
82 |
Correct |
202 ms |
77840 KB |
Output is correct |
83 |
Correct |
214 ms |
81996 KB |
Output is correct |
84 |
Correct |
238 ms |
85800 KB |
Output is correct |
85 |
Correct |
244 ms |
89792 KB |
Output is correct |
86 |
Correct |
259 ms |
93680 KB |
Output is correct |
87 |
Correct |
275 ms |
97712 KB |
Output is correct |
88 |
Correct |
288 ms |
101088 KB |
Output is correct |
89 |
Correct |
318 ms |
105108 KB |
Output is correct |
90 |
Correct |
18 ms |
24780 KB |
Output is correct |
91 |
Correct |
15 ms |
24700 KB |
Output is correct |
92 |
Correct |
13 ms |
24788 KB |
Output is correct |
93 |
Correct |
14 ms |
24732 KB |
Output is correct |
94 |
Correct |
13 ms |
24696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24788 KB |
Output is correct |
2 |
Correct |
14 ms |
24788 KB |
Output is correct |
3 |
Correct |
14 ms |
24700 KB |
Output is correct |
4 |
Correct |
15 ms |
24776 KB |
Output is correct |
5 |
Correct |
12 ms |
24788 KB |
Output is correct |
6 |
Correct |
14 ms |
24800 KB |
Output is correct |
7 |
Correct |
13 ms |
24788 KB |
Output is correct |
8 |
Correct |
13 ms |
24788 KB |
Output is correct |
9 |
Correct |
15 ms |
24700 KB |
Output is correct |
10 |
Correct |
13 ms |
24744 KB |
Output is correct |
11 |
Correct |
13 ms |
24696 KB |
Output is correct |
12 |
Correct |
12 ms |
24796 KB |
Output is correct |
13 |
Correct |
13 ms |
24788 KB |
Output is correct |
14 |
Correct |
16 ms |
24692 KB |
Output is correct |
15 |
Correct |
13 ms |
24700 KB |
Output is correct |
16 |
Correct |
13 ms |
24788 KB |
Output is correct |
17 |
Correct |
13 ms |
24788 KB |
Output is correct |
18 |
Correct |
13 ms |
24740 KB |
Output is correct |
19 |
Correct |
13 ms |
24700 KB |
Output is correct |
20 |
Correct |
13 ms |
24700 KB |
Output is correct |
21 |
Correct |
12 ms |
24796 KB |
Output is correct |
22 |
Correct |
15 ms |
24788 KB |
Output is correct |
23 |
Correct |
12 ms |
24696 KB |
Output is correct |
24 |
Correct |
14 ms |
24788 KB |
Output is correct |
25 |
Correct |
15 ms |
24704 KB |
Output is correct |
26 |
Correct |
13 ms |
24696 KB |
Output is correct |
27 |
Correct |
12 ms |
24788 KB |
Output is correct |
28 |
Correct |
12 ms |
24804 KB |
Output is correct |
29 |
Correct |
13 ms |
24788 KB |
Output is correct |
30 |
Correct |
16 ms |
24788 KB |
Output is correct |
31 |
Correct |
19 ms |
24788 KB |
Output is correct |
32 |
Correct |
14 ms |
24792 KB |
Output is correct |
33 |
Correct |
15 ms |
24788 KB |
Output is correct |
34 |
Correct |
15 ms |
24752 KB |
Output is correct |
35 |
Correct |
13 ms |
24832 KB |
Output is correct |
36 |
Correct |
14 ms |
24832 KB |
Output is correct |
37 |
Correct |
13 ms |
24788 KB |
Output is correct |
38 |
Correct |
13 ms |
24788 KB |
Output is correct |
39 |
Correct |
13 ms |
24868 KB |
Output is correct |
40 |
Correct |
13 ms |
24840 KB |
Output is correct |
41 |
Correct |
13 ms |
24788 KB |
Output is correct |
42 |
Correct |
12 ms |
24800 KB |
Output is correct |
43 |
Correct |
12 ms |
24780 KB |
Output is correct |
44 |
Correct |
13 ms |
24696 KB |
Output is correct |
45 |
Correct |
12 ms |
24788 KB |
Output is correct |
46 |
Correct |
13 ms |
24788 KB |
Output is correct |
47 |
Correct |
15 ms |
24700 KB |
Output is correct |
48 |
Incorrect |
13 ms |
24788 KB |
Output isn't correct |
49 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24788 KB |
Output is correct |
2 |
Correct |
14 ms |
24788 KB |
Output is correct |
3 |
Correct |
14 ms |
24700 KB |
Output is correct |
4 |
Correct |
15 ms |
24776 KB |
Output is correct |
5 |
Correct |
12 ms |
24788 KB |
Output is correct |
6 |
Correct |
14 ms |
24800 KB |
Output is correct |
7 |
Correct |
13 ms |
24788 KB |
Output is correct |
8 |
Correct |
13 ms |
24788 KB |
Output is correct |
9 |
Correct |
15 ms |
24700 KB |
Output is correct |
10 |
Correct |
13 ms |
24744 KB |
Output is correct |
11 |
Correct |
13 ms |
24696 KB |
Output is correct |
12 |
Correct |
12 ms |
24796 KB |
Output is correct |
13 |
Correct |
13 ms |
24788 KB |
Output is correct |
14 |
Correct |
16 ms |
24692 KB |
Output is correct |
15 |
Correct |
13 ms |
24700 KB |
Output is correct |
16 |
Correct |
13 ms |
24788 KB |
Output is correct |
17 |
Correct |
13 ms |
24788 KB |
Output is correct |
18 |
Correct |
13 ms |
24740 KB |
Output is correct |
19 |
Correct |
13 ms |
24700 KB |
Output is correct |
20 |
Correct |
13 ms |
24700 KB |
Output is correct |
21 |
Correct |
12 ms |
24796 KB |
Output is correct |
22 |
Correct |
15 ms |
24788 KB |
Output is correct |
23 |
Correct |
12 ms |
24696 KB |
Output is correct |
24 |
Correct |
14 ms |
24788 KB |
Output is correct |
25 |
Correct |
15 ms |
24704 KB |
Output is correct |
26 |
Correct |
13 ms |
24696 KB |
Output is correct |
27 |
Correct |
12 ms |
24788 KB |
Output is correct |
28 |
Correct |
12 ms |
24804 KB |
Output is correct |
29 |
Correct |
13 ms |
24788 KB |
Output is correct |
30 |
Correct |
16 ms |
24788 KB |
Output is correct |
31 |
Correct |
19 ms |
24788 KB |
Output is correct |
32 |
Correct |
14 ms |
24792 KB |
Output is correct |
33 |
Correct |
15 ms |
24788 KB |
Output is correct |
34 |
Correct |
15 ms |
24752 KB |
Output is correct |
35 |
Correct |
13 ms |
24832 KB |
Output is correct |
36 |
Correct |
14 ms |
24832 KB |
Output is correct |
37 |
Correct |
13 ms |
24788 KB |
Output is correct |
38 |
Correct |
13 ms |
24788 KB |
Output is correct |
39 |
Correct |
13 ms |
24868 KB |
Output is correct |
40 |
Correct |
13 ms |
24840 KB |
Output is correct |
41 |
Correct |
13 ms |
24788 KB |
Output is correct |
42 |
Correct |
12 ms |
24800 KB |
Output is correct |
43 |
Correct |
12 ms |
24780 KB |
Output is correct |
44 |
Correct |
13 ms |
24696 KB |
Output is correct |
45 |
Correct |
12 ms |
24788 KB |
Output is correct |
46 |
Correct |
13 ms |
24788 KB |
Output is correct |
47 |
Correct |
15 ms |
24700 KB |
Output is correct |
48 |
Correct |
14 ms |
24788 KB |
Output is correct |
49 |
Correct |
12 ms |
24700 KB |
Output is correct |
50 |
Correct |
12 ms |
24788 KB |
Output is correct |
51 |
Correct |
12 ms |
24700 KB |
Output is correct |
52 |
Correct |
13 ms |
24712 KB |
Output is correct |
53 |
Correct |
13 ms |
24700 KB |
Output is correct |
54 |
Correct |
13 ms |
24744 KB |
Output is correct |
55 |
Correct |
13 ms |
24696 KB |
Output is correct |
56 |
Correct |
12 ms |
24728 KB |
Output is correct |
57 |
Correct |
12 ms |
24704 KB |
Output is correct |
58 |
Correct |
13 ms |
24788 KB |
Output is correct |
59 |
Correct |
13 ms |
24704 KB |
Output is correct |
60 |
Correct |
16 ms |
24744 KB |
Output is correct |
61 |
Correct |
14 ms |
24700 KB |
Output is correct |
62 |
Correct |
14 ms |
24788 KB |
Output is correct |
63 |
Correct |
14 ms |
24788 KB |
Output is correct |
64 |
Correct |
13 ms |
24744 KB |
Output is correct |
65 |
Correct |
13 ms |
24696 KB |
Output is correct |
66 |
Correct |
13 ms |
24788 KB |
Output is correct |
67 |
Correct |
12 ms |
24788 KB |
Output is correct |
68 |
Correct |
38 ms |
29652 KB |
Output is correct |
69 |
Correct |
47 ms |
33704 KB |
Output is correct |
70 |
Correct |
13 ms |
24788 KB |
Output is correct |
71 |
Correct |
57 ms |
37652 KB |
Output is correct |
72 |
Correct |
81 ms |
41864 KB |
Output is correct |
73 |
Correct |
83 ms |
45692 KB |
Output is correct |
74 |
Correct |
104 ms |
49768 KB |
Output is correct |
75 |
Correct |
113 ms |
53808 KB |
Output is correct |
76 |
Correct |
130 ms |
57784 KB |
Output is correct |
77 |
Correct |
140 ms |
61860 KB |
Output is correct |
78 |
Correct |
166 ms |
65904 KB |
Output is correct |
79 |
Correct |
172 ms |
69852 KB |
Output is correct |
80 |
Correct |
199 ms |
73836 KB |
Output is correct |
81 |
Correct |
15 ms |
24784 KB |
Output is correct |
82 |
Correct |
202 ms |
77840 KB |
Output is correct |
83 |
Correct |
214 ms |
81996 KB |
Output is correct |
84 |
Correct |
238 ms |
85800 KB |
Output is correct |
85 |
Correct |
244 ms |
89792 KB |
Output is correct |
86 |
Correct |
259 ms |
93680 KB |
Output is correct |
87 |
Correct |
275 ms |
97712 KB |
Output is correct |
88 |
Correct |
288 ms |
101088 KB |
Output is correct |
89 |
Correct |
318 ms |
105108 KB |
Output is correct |
90 |
Correct |
18 ms |
24780 KB |
Output is correct |
91 |
Correct |
15 ms |
24700 KB |
Output is correct |
92 |
Correct |
13 ms |
24788 KB |
Output is correct |
93 |
Correct |
14 ms |
24732 KB |
Output is correct |
94 |
Correct |
13 ms |
24696 KB |
Output is correct |
95 |
Incorrect |
13 ms |
24788 KB |
Output isn't correct |
96 |
Halted |
0 ms |
0 KB |
- |