#include <bits/stdc++.h>
using namespace std;
#define el "\n"
#define FOR(i,a,b) for(int i = (a), _b = (b); i <= _b; i++)
#define FORD(i,a,b) for(int i = (a), _b = (b); i >= _b; i--)
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define lg(x) __lg(x)
#define alla(a,n) a+1,a+n+1
#define ll long long
template <class T> bool maxi(T &x, T y) { if(x < y) { x = y ; return true ;} return false;}
template <class T> bool mini(T &x, T y) { if(x > y) { x = y ; return true ;} return false;}
const int N = 1e5 + 2;
int n, a[N];
vector<int> adj[N];
pair<int, int> E[N];
void inp()
{
cin >> n;
FOR(i, 1, n) cin >> a[i];
FOR(i, 1, n - 1) {
int x, y; cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
E[i] = {x, y};
}
}
namespace subtask_1
{
int h[N], par[N];
void dfs(int u, int p)
{
for(int v : adj[u]) if(v != p) {
h[v] = h[u] + 1;
par[v] = u;
dfs(v, u);
}
}
int calc(int x)
{
int ans = 0;
int cur = x;
while(cur > 0) {
if(a[cur] > a[x]) ans++;
cur = par[cur];
}
return ans;
}
void slv()
{
dfs(1, 1);
FOR(i, 1, n - 1) {
int x = E[i].first, y = E[i].second;
int cur = x;
int ans = 0;
while(cur > 0) {
ans += calc(cur);
cur = par[cur];
}
cur = x;
while(cur > 0) {
a[cur] = a[y];
cur = par[cur];
}
cout << ans << el;
}
}
}
namespace subtask_2
{
int sz[N], h[N], par[N];
void dfs(int u, int p)
{
sz[u] = 1;
for(int v : adj[u]) if(v != p) {
h[v] = h[u] + 1;
par[v] = u;
dfs(v, u);
sz[u] += sz[v];
}
}
int head[N], chainID[N], mtc = 1, time = 0, pos[N], euler[N];
void hld(int u, int p)
{
if(!head[mtc]) {
head[mtc] = u;
}
chainID[u] = mtc;
euler[++time] = u;
pos[u] = time;
int ma = 0;
for(int v : adj[u]) if(v != p) {
if(!ma || sz[v] > sz[ma]) ma = v;
}
if(ma) hld(ma, u);
for(int v : adj[u]) if(v != p && v != ma) {
mtc++;
hld(v, u);
}
}
int st[4 * N], lz[4 * N];
int calc(int x)
{
int ans = 0;
while(chainID[x] != chainID[1]) {
}
}
void slv()
{
dfs(1, 1);
hld(1, 1);
FOR(i, 1, n - 1) {
int x = E[i].first, y = E[i].second;
int ans = 0;
int cur = x;
while(cur > 0) {
ans += calc(cur);
cur = par[cur];
}
}
}
}
main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define __Azul__ "JOI18_construction"
if(fopen(__Azul__".inp", "r")) {
freopen(__Azul__".inp", "r", stdin);
freopen(__Azul__".out", "w", stdout);
}
bool qs = 0;
int T = 1;
if(qs) cin >> T;
while(T--) {
inp();
subtask_1::slv();
}
cerr << "\nTime" << 0.001 * clock() << "s "; return 0;
}
Compilation message (stderr)
construction.cpp: In function 'int subtask_2::calc(int)':
construction.cpp:130:5: warning: no return statement in function returning non-void [-Wreturn-type]
130 | }
| ^
construction.cpp: At global scope:
construction.cpp:149:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
149 | main()
| ^~~~
construction.cpp: In function 'int main()':
construction.cpp:155:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
155 | freopen(__Azul__".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
construction.cpp:156:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
156 | freopen(__Azul__".out", "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... |