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;
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 (stderr)
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) {
~~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |