#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 1e5 + 7;
vector <int> g[N];
int sz[N], st[N], en[N], par[N], in[N], out[N], h[N];
int A[N], B[N], C[N];
deque <pair <int, int>> col[N];
void dfs(int u = 0, int p = -1) {
par[u] = p;
for (int i = 0; i < g[u].size(); ++i) {
if (g[u][i] == p) {
g[u].erase(g[u].begin() + i);
break;
}
}
sz[u] = 1;
for (int v : g[u]) {
dfs(v, u);
sz[u] += sz[v];
}
for (int i = 1; i < (int)g[u].size(); ++i) {
if (sz[g[u][i]] > sz[g[u][0]]) swap(g[u][i], g[u][0]);
}
}
vector <int> e;
int dfs_hld(int u = 0, int head = 0) {
e.push_back(u);
in[u] = (int)e.size() - 1;
st[u] = head;
if (par[u] != -1) h[u] = h[par[u]] + 1;
else h[u] = 0;
if (!g[u].empty()) {
en[u] = dfs_hld(g[u][0], head);
for (int i = 1; i < (int)g[u].size(); ++i) {
dfs_hld(g[u][i], g[u][i]);
}
} else {
en[u] = u;
}
out[u] = (int)e.size() - 1;
return en[u];
}
bool anc(int a, int b) {
return in[a] <= in[b] && out[b] <= out[a];
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.setf(ios::fixed); cout.precision(20);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n;
cin >> n;
for (int i = 0; i < n; ++i) cin >> C[i];
for (int i = 0; i + 1 < n; ++i) {
cin >> A[i] >> B[i];
--A[i], --B[i];
g[A[i]].push_back(B[i]);
g[B[i]].push_back(A[i]);
}
dfs();
dfs_hld();
col[0].push_back({C[0], 0});
for (int id = 0; id + 1 < n; ++id) {
int ans = 0;
int u = B[id];
vector <pair <int, int>> hv;
while (u != -1) {
int pth = st[u];
vector <pair <int, int>> cur;
for (int i = 0; i < (int)col[pth].size(); ++i) {
if (anc(col[pth][i].second, u)) {
cur.push_back({col[pth][i].first, h[col[pth][i].second] - (i == 0 ? h[pth] - 1: h[col[pth][i - 1].second])});
} else {
cur.push_back({col[pth][i].first, h[u] - (i == 0 ? h[pth] - 1: h[col[pth][i - 1].second])});
break;
}
}
reverse(cur.begin(), cur.end());
for (auto p : cur) hv.push_back(p);
while (!col[pth].empty() && anc(col[pth][0].second, B[id])) col[pth].pop_front();
col[pth].push_front({C[B[id]], u});
u = par[st[u]];
}
reverse(hv.begin(), hv.end());
for (int i = 0; i < hv.size(); ++i) {
for (int j = i + 1; j < hv.size(); ++j) {
if (hv[i].first > hv[j].first) ans += hv[i].second * hv[j].second;
}
}
cout << ans << '\n';
}
}
Compilation message
construction.cpp: In function 'void dfs(int, int)':
construction.cpp:17:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < g[u].size(); ++i) {
~~^~~~~~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:97:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < hv.size(); ++i) {
~~^~~~~~~~~~~
construction.cpp:98:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = i + 1; j < hv.size(); ++j) {
~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
70136 KB |
Output is correct |
2 |
Correct |
87 ms |
70264 KB |
Output is correct |
3 |
Correct |
80 ms |
70108 KB |
Output is correct |
4 |
Correct |
100 ms |
70136 KB |
Output is correct |
5 |
Correct |
83 ms |
70264 KB |
Output is correct |
6 |
Correct |
86 ms |
70176 KB |
Output is correct |
7 |
Correct |
98 ms |
70264 KB |
Output is correct |
8 |
Correct |
74 ms |
70084 KB |
Output is correct |
9 |
Correct |
79 ms |
70264 KB |
Output is correct |
10 |
Correct |
76 ms |
70136 KB |
Output is correct |
11 |
Correct |
80 ms |
70264 KB |
Output is correct |
12 |
Correct |
82 ms |
70392 KB |
Output is correct |
13 |
Correct |
80 ms |
70300 KB |
Output is correct |
14 |
Correct |
81 ms |
70264 KB |
Output is correct |
15 |
Correct |
81 ms |
70264 KB |
Output is correct |
16 |
Correct |
99 ms |
70264 KB |
Output is correct |
17 |
Correct |
98 ms |
70264 KB |
Output is correct |
18 |
Correct |
98 ms |
70264 KB |
Output is correct |
19 |
Correct |
88 ms |
70344 KB |
Output is correct |
20 |
Correct |
105 ms |
70384 KB |
Output is correct |
21 |
Correct |
97 ms |
70208 KB |
Output is correct |
22 |
Correct |
94 ms |
70192 KB |
Output is correct |
23 |
Correct |
92 ms |
70264 KB |
Output is correct |
24 |
Correct |
99 ms |
70272 KB |
Output is correct |
25 |
Correct |
88 ms |
70264 KB |
Output is correct |
26 |
Correct |
74 ms |
70136 KB |
Output is correct |
27 |
Correct |
102 ms |
70264 KB |
Output is correct |
28 |
Correct |
79 ms |
70168 KB |
Output is correct |
29 |
Correct |
98 ms |
70264 KB |
Output is correct |
30 |
Correct |
86 ms |
70264 KB |
Output is correct |
31 |
Correct |
79 ms |
70264 KB |
Output is correct |
32 |
Correct |
80 ms |
70264 KB |
Output is correct |
33 |
Correct |
80 ms |
70264 KB |
Output is correct |
34 |
Correct |
80 ms |
70388 KB |
Output is correct |
35 |
Correct |
80 ms |
70392 KB |
Output is correct |
36 |
Correct |
81 ms |
70288 KB |
Output is correct |
37 |
Correct |
80 ms |
70264 KB |
Output is correct |
38 |
Correct |
81 ms |
70236 KB |
Output is correct |
39 |
Correct |
81 ms |
70264 KB |
Output is correct |
40 |
Correct |
80 ms |
70288 KB |
Output is correct |
41 |
Correct |
79 ms |
70268 KB |
Output is correct |
42 |
Correct |
80 ms |
70264 KB |
Output is correct |
43 |
Correct |
81 ms |
70392 KB |
Output is correct |
44 |
Correct |
80 ms |
70264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
70136 KB |
Output is correct |
2 |
Correct |
87 ms |
70264 KB |
Output is correct |
3 |
Correct |
80 ms |
70108 KB |
Output is correct |
4 |
Correct |
100 ms |
70136 KB |
Output is correct |
5 |
Correct |
83 ms |
70264 KB |
Output is correct |
6 |
Correct |
86 ms |
70176 KB |
Output is correct |
7 |
Correct |
98 ms |
70264 KB |
Output is correct |
8 |
Correct |
74 ms |
70084 KB |
Output is correct |
9 |
Correct |
79 ms |
70264 KB |
Output is correct |
10 |
Correct |
76 ms |
70136 KB |
Output is correct |
11 |
Correct |
80 ms |
70264 KB |
Output is correct |
12 |
Correct |
82 ms |
70392 KB |
Output is correct |
13 |
Correct |
80 ms |
70300 KB |
Output is correct |
14 |
Correct |
81 ms |
70264 KB |
Output is correct |
15 |
Correct |
81 ms |
70264 KB |
Output is correct |
16 |
Correct |
99 ms |
70264 KB |
Output is correct |
17 |
Correct |
98 ms |
70264 KB |
Output is correct |
18 |
Correct |
98 ms |
70264 KB |
Output is correct |
19 |
Correct |
88 ms |
70344 KB |
Output is correct |
20 |
Correct |
105 ms |
70384 KB |
Output is correct |
21 |
Correct |
97 ms |
70208 KB |
Output is correct |
22 |
Correct |
94 ms |
70192 KB |
Output is correct |
23 |
Correct |
92 ms |
70264 KB |
Output is correct |
24 |
Correct |
99 ms |
70272 KB |
Output is correct |
25 |
Correct |
88 ms |
70264 KB |
Output is correct |
26 |
Correct |
74 ms |
70136 KB |
Output is correct |
27 |
Correct |
102 ms |
70264 KB |
Output is correct |
28 |
Correct |
79 ms |
70168 KB |
Output is correct |
29 |
Correct |
98 ms |
70264 KB |
Output is correct |
30 |
Correct |
86 ms |
70264 KB |
Output is correct |
31 |
Correct |
79 ms |
70264 KB |
Output is correct |
32 |
Correct |
80 ms |
70264 KB |
Output is correct |
33 |
Correct |
80 ms |
70264 KB |
Output is correct |
34 |
Correct |
80 ms |
70388 KB |
Output is correct |
35 |
Correct |
80 ms |
70392 KB |
Output is correct |
36 |
Correct |
81 ms |
70288 KB |
Output is correct |
37 |
Correct |
80 ms |
70264 KB |
Output is correct |
38 |
Correct |
81 ms |
70236 KB |
Output is correct |
39 |
Correct |
81 ms |
70264 KB |
Output is correct |
40 |
Correct |
80 ms |
70288 KB |
Output is correct |
41 |
Correct |
79 ms |
70268 KB |
Output is correct |
42 |
Correct |
80 ms |
70264 KB |
Output is correct |
43 |
Correct |
81 ms |
70392 KB |
Output is correct |
44 |
Correct |
80 ms |
70264 KB |
Output is correct |
45 |
Correct |
81 ms |
70392 KB |
Output is correct |
46 |
Correct |
88 ms |
71488 KB |
Output is correct |
47 |
Correct |
88 ms |
71544 KB |
Output is correct |
48 |
Correct |
93 ms |
71536 KB |
Output is correct |
49 |
Correct |
80 ms |
70776 KB |
Output is correct |
50 |
Correct |
89 ms |
70776 KB |
Output is correct |
51 |
Correct |
95 ms |
70776 KB |
Output is correct |
52 |
Correct |
90 ms |
71584 KB |
Output is correct |
53 |
Correct |
115 ms |
71676 KB |
Output is correct |
54 |
Correct |
111 ms |
71672 KB |
Output is correct |
55 |
Correct |
115 ms |
71672 KB |
Output is correct |
56 |
Correct |
141 ms |
71672 KB |
Output is correct |
57 |
Correct |
110 ms |
71416 KB |
Output is correct |
58 |
Correct |
108 ms |
71528 KB |
Output is correct |
59 |
Correct |
109 ms |
71672 KB |
Output is correct |
60 |
Correct |
194 ms |
71388 KB |
Output is correct |
61 |
Correct |
109 ms |
71544 KB |
Output is correct |
62 |
Correct |
88 ms |
71580 KB |
Output is correct |
63 |
Correct |
85 ms |
71544 KB |
Output is correct |
64 |
Correct |
99 ms |
71432 KB |
Output is correct |
65 |
Correct |
101 ms |
71380 KB |
Output is correct |
66 |
Correct |
108 ms |
71488 KB |
Output is correct |
67 |
Correct |
108 ms |
71420 KB |
Output is correct |
68 |
Correct |
87 ms |
70768 KB |
Output is correct |
69 |
Correct |
93 ms |
71620 KB |
Output is correct |
70 |
Correct |
94 ms |
71800 KB |
Output is correct |
71 |
Correct |
85 ms |
71672 KB |
Output is correct |
72 |
Correct |
104 ms |
71544 KB |
Output is correct |
73 |
Correct |
88 ms |
71416 KB |
Output is correct |
74 |
Correct |
85 ms |
71544 KB |
Output is correct |
75 |
Correct |
86 ms |
71544 KB |
Output is correct |
76 |
Correct |
87 ms |
71536 KB |
Output is correct |
77 |
Correct |
87 ms |
71416 KB |
Output is correct |
78 |
Correct |
86 ms |
71684 KB |
Output is correct |
79 |
Correct |
87 ms |
71496 KB |
Output is correct |
80 |
Correct |
104 ms |
71544 KB |
Output is correct |
81 |
Correct |
112 ms |
71792 KB |
Output is correct |
82 |
Correct |
106 ms |
71552 KB |
Output is correct |
83 |
Correct |
87 ms |
71408 KB |
Output is correct |
84 |
Correct |
105 ms |
71544 KB |
Output is correct |
85 |
Correct |
85 ms |
71416 KB |
Output is correct |
86 |
Correct |
86 ms |
71544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
70136 KB |
Output is correct |
2 |
Correct |
87 ms |
70264 KB |
Output is correct |
3 |
Correct |
80 ms |
70108 KB |
Output is correct |
4 |
Correct |
100 ms |
70136 KB |
Output is correct |
5 |
Correct |
83 ms |
70264 KB |
Output is correct |
6 |
Correct |
86 ms |
70176 KB |
Output is correct |
7 |
Correct |
98 ms |
70264 KB |
Output is correct |
8 |
Correct |
74 ms |
70084 KB |
Output is correct |
9 |
Correct |
79 ms |
70264 KB |
Output is correct |
10 |
Correct |
76 ms |
70136 KB |
Output is correct |
11 |
Correct |
80 ms |
70264 KB |
Output is correct |
12 |
Correct |
82 ms |
70392 KB |
Output is correct |
13 |
Correct |
80 ms |
70300 KB |
Output is correct |
14 |
Correct |
81 ms |
70264 KB |
Output is correct |
15 |
Correct |
81 ms |
70264 KB |
Output is correct |
16 |
Correct |
99 ms |
70264 KB |
Output is correct |
17 |
Correct |
98 ms |
70264 KB |
Output is correct |
18 |
Correct |
98 ms |
70264 KB |
Output is correct |
19 |
Correct |
88 ms |
70344 KB |
Output is correct |
20 |
Correct |
105 ms |
70384 KB |
Output is correct |
21 |
Correct |
97 ms |
70208 KB |
Output is correct |
22 |
Correct |
94 ms |
70192 KB |
Output is correct |
23 |
Correct |
92 ms |
70264 KB |
Output is correct |
24 |
Correct |
99 ms |
70272 KB |
Output is correct |
25 |
Correct |
88 ms |
70264 KB |
Output is correct |
26 |
Correct |
74 ms |
70136 KB |
Output is correct |
27 |
Correct |
102 ms |
70264 KB |
Output is correct |
28 |
Correct |
79 ms |
70168 KB |
Output is correct |
29 |
Correct |
98 ms |
70264 KB |
Output is correct |
30 |
Correct |
86 ms |
70264 KB |
Output is correct |
31 |
Correct |
79 ms |
70264 KB |
Output is correct |
32 |
Correct |
80 ms |
70264 KB |
Output is correct |
33 |
Correct |
80 ms |
70264 KB |
Output is correct |
34 |
Correct |
80 ms |
70388 KB |
Output is correct |
35 |
Correct |
80 ms |
70392 KB |
Output is correct |
36 |
Correct |
81 ms |
70288 KB |
Output is correct |
37 |
Correct |
80 ms |
70264 KB |
Output is correct |
38 |
Correct |
81 ms |
70236 KB |
Output is correct |
39 |
Correct |
81 ms |
70264 KB |
Output is correct |
40 |
Correct |
80 ms |
70288 KB |
Output is correct |
41 |
Correct |
79 ms |
70268 KB |
Output is correct |
42 |
Correct |
80 ms |
70264 KB |
Output is correct |
43 |
Correct |
81 ms |
70392 KB |
Output is correct |
44 |
Correct |
80 ms |
70264 KB |
Output is correct |
45 |
Correct |
81 ms |
70392 KB |
Output is correct |
46 |
Correct |
88 ms |
71488 KB |
Output is correct |
47 |
Correct |
88 ms |
71544 KB |
Output is correct |
48 |
Correct |
93 ms |
71536 KB |
Output is correct |
49 |
Correct |
80 ms |
70776 KB |
Output is correct |
50 |
Correct |
89 ms |
70776 KB |
Output is correct |
51 |
Correct |
95 ms |
70776 KB |
Output is correct |
52 |
Correct |
90 ms |
71584 KB |
Output is correct |
53 |
Correct |
115 ms |
71676 KB |
Output is correct |
54 |
Correct |
111 ms |
71672 KB |
Output is correct |
55 |
Correct |
115 ms |
71672 KB |
Output is correct |
56 |
Correct |
141 ms |
71672 KB |
Output is correct |
57 |
Correct |
110 ms |
71416 KB |
Output is correct |
58 |
Correct |
108 ms |
71528 KB |
Output is correct |
59 |
Correct |
109 ms |
71672 KB |
Output is correct |
60 |
Correct |
194 ms |
71388 KB |
Output is correct |
61 |
Correct |
109 ms |
71544 KB |
Output is correct |
62 |
Correct |
88 ms |
71580 KB |
Output is correct |
63 |
Correct |
85 ms |
71544 KB |
Output is correct |
64 |
Correct |
99 ms |
71432 KB |
Output is correct |
65 |
Correct |
101 ms |
71380 KB |
Output is correct |
66 |
Correct |
108 ms |
71488 KB |
Output is correct |
67 |
Correct |
108 ms |
71420 KB |
Output is correct |
68 |
Correct |
87 ms |
70768 KB |
Output is correct |
69 |
Correct |
93 ms |
71620 KB |
Output is correct |
70 |
Correct |
94 ms |
71800 KB |
Output is correct |
71 |
Correct |
85 ms |
71672 KB |
Output is correct |
72 |
Correct |
104 ms |
71544 KB |
Output is correct |
73 |
Correct |
88 ms |
71416 KB |
Output is correct |
74 |
Correct |
85 ms |
71544 KB |
Output is correct |
75 |
Correct |
86 ms |
71544 KB |
Output is correct |
76 |
Correct |
87 ms |
71536 KB |
Output is correct |
77 |
Correct |
87 ms |
71416 KB |
Output is correct |
78 |
Correct |
86 ms |
71684 KB |
Output is correct |
79 |
Correct |
87 ms |
71496 KB |
Output is correct |
80 |
Correct |
104 ms |
71544 KB |
Output is correct |
81 |
Correct |
112 ms |
71792 KB |
Output is correct |
82 |
Correct |
106 ms |
71552 KB |
Output is correct |
83 |
Correct |
87 ms |
71408 KB |
Output is correct |
84 |
Correct |
105 ms |
71544 KB |
Output is correct |
85 |
Correct |
85 ms |
71416 KB |
Output is correct |
86 |
Correct |
86 ms |
71544 KB |
Output is correct |
87 |
Correct |
124 ms |
73644 KB |
Output is correct |
88 |
Correct |
169 ms |
80888 KB |
Output is correct |
89 |
Correct |
460 ms |
105792 KB |
Output is correct |
90 |
Correct |
448 ms |
105896 KB |
Output is correct |
91 |
Correct |
542 ms |
105888 KB |
Output is correct |
92 |
Correct |
183 ms |
87664 KB |
Output is correct |
93 |
Correct |
179 ms |
87664 KB |
Output is correct |
94 |
Correct |
184 ms |
87816 KB |
Output is correct |
95 |
Correct |
1280 ms |
110652 KB |
Output is correct |
96 |
Execution timed out |
2071 ms |
111180 KB |
Time limit exceeded |
97 |
Halted |
0 ms |
0 KB |
- |