#include<bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define pp pop_back
#define all(x) (x).begin(), (x).end()
using namespace std;
void debug(){cerr<<"\n";}
template<typename H, typename... T>
void debug(H h, T... t){
cerr<<h;
if(sizeof...(t))cerr<<", ";
debug(t...);
}
#define deb(x...) cerr<<#x<<" = ";debug(x);
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vii = vector<pii>;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N=1e5+5;
vi E[N];
int pre[N], sz[N], par[N], sci[N], dep[N], val[N], co[N];
int wsk;
void dfs_sz(int v){
for(int u:E[v]){
if(u!=par[v]){
par[u]=v;
dep[u]=dep[v]+1;
dfs_sz(u);
sz[v]+=sz[u];
}
}
sz[v]+=1;
for(int &u:E[v])if(u==par[v])swap(u, E[v].back());
if(v!=1)E[v].pp();
for(int &u:E[v])if(sz[u]>sz[E[v][0]])swap(u, E[v][0]);
//deb(v, E[v].size());
}
void dfs(int v){
co[wsk]=v;
pre[v]=wsk++;
for(int u:E[v]){
//deb(v, u);
if(u==E[v][0])sci[wsk]=sci[pre[v]];
else{
sci[wsk]=wsk;
//deb(v, wsk);
}
dfs(u);
}
}
set<int> S[N];
ll calc(int v){
vector<pair<int, int> > V;
v=pre[v];
int t=val[v];
bool czy=1;
while(true){
int u=sci[v];
//deb(v, u, S[u].size());
if(S[u].size()){
//deb(*S[u].begin());
}
vector<pair<int, int> > V2;
int prv=u-1;
while(prv<v-czy){
//deb("a");
//assert(S[u].size());
int a=*S[u].begin();
if(a>v-czy){
V2.eb(v-czy-prv, val[a]);
}
else{
V2.eb(a-prv, val[a]);
S[u].erase(a);
}
prv=a;
}
reverse(all(V2));
for(pii &i:V2)V.pb(i);
S[u].insert(v);
val[v]=t;
if(u==0)break;
v=pre[par[co[u]]];
czy=0;
}
reverse(all(V));
//deb(V.size());
for(pii i:V){
//deb(i.st, i.nd);
}
ll ans=0;
for(int i=0; i<V.size(); i++){
for(int j=0; j<i; j++){
if(V[j].nd>V[i].nd)ans+=V[j].st*1ll*V[i].st;
}
}
return ans;
}
int main(){
//ios_base::sync_with_stdio(0);cin.tie(0);
int n;
cin>>n;
vi V(n);
for(int &i:V)cin>>i;
vector<pii> edg(n-1);
for(pii &i:edg)cin>>i.st>>i.nd, E[i.st].pb(i.nd), E[i.nd].pb(i.st);
dfs_sz(1);
dfs(1);
for(int i=1; i<=n; i++){
//deb(i, pre[i], par[i], val[i], sci[pre[i]], sz[i]);
}
S[0].insert(0);
for(int i=1; i<=n; i++){
val[pre[i]]=V[i-1];
}
for(pii i:edg){
cout<<calc(i.nd)<<"\n";
}
}
Compilation message
construction.cpp: In function 'll calc(int)':
construction.cpp:97:10: warning: variable 'i' set but not used [-Wunused-but-set-variable]
97 | for(pii i:V){
| ^
construction.cpp:101:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
101 | for(int i=0; i<V.size(); i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7368 KB |
Output is correct |
4 |
Correct |
5 ms |
7368 KB |
Output is correct |
5 |
Correct |
5 ms |
7380 KB |
Output is correct |
6 |
Correct |
5 ms |
7380 KB |
Output is correct |
7 |
Correct |
5 ms |
7452 KB |
Output is correct |
8 |
Correct |
6 ms |
7380 KB |
Output is correct |
9 |
Correct |
5 ms |
7380 KB |
Output is correct |
10 |
Correct |
5 ms |
7368 KB |
Output is correct |
11 |
Correct |
7 ms |
7508 KB |
Output is correct |
12 |
Correct |
7 ms |
7368 KB |
Output is correct |
13 |
Correct |
5 ms |
7372 KB |
Output is correct |
14 |
Correct |
5 ms |
7380 KB |
Output is correct |
15 |
Correct |
5 ms |
7372 KB |
Output is correct |
16 |
Correct |
5 ms |
7380 KB |
Output is correct |
17 |
Correct |
5 ms |
7380 KB |
Output is correct |
18 |
Correct |
5 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7368 KB |
Output is correct |
20 |
Correct |
4 ms |
7392 KB |
Output is correct |
21 |
Correct |
5 ms |
7380 KB |
Output is correct |
22 |
Correct |
4 ms |
7380 KB |
Output is correct |
23 |
Correct |
5 ms |
7368 KB |
Output is correct |
24 |
Correct |
5 ms |
7380 KB |
Output is correct |
25 |
Correct |
5 ms |
7380 KB |
Output is correct |
26 |
Correct |
5 ms |
7380 KB |
Output is correct |
27 |
Correct |
5 ms |
7460 KB |
Output is correct |
28 |
Correct |
4 ms |
7368 KB |
Output is correct |
29 |
Correct |
4 ms |
7380 KB |
Output is correct |
30 |
Correct |
5 ms |
7380 KB |
Output is correct |
31 |
Correct |
5 ms |
7380 KB |
Output is correct |
32 |
Correct |
5 ms |
7380 KB |
Output is correct |
33 |
Correct |
5 ms |
7380 KB |
Output is correct |
34 |
Correct |
5 ms |
7364 KB |
Output is correct |
35 |
Correct |
4 ms |
7380 KB |
Output is correct |
36 |
Correct |
4 ms |
7380 KB |
Output is correct |
37 |
Correct |
5 ms |
7444 KB |
Output is correct |
38 |
Correct |
5 ms |
7372 KB |
Output is correct |
39 |
Correct |
5 ms |
7380 KB |
Output is correct |
40 |
Correct |
5 ms |
7380 KB |
Output is correct |
41 |
Correct |
5 ms |
7380 KB |
Output is correct |
42 |
Correct |
5 ms |
7372 KB |
Output is correct |
43 |
Correct |
4 ms |
7364 KB |
Output is correct |
44 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7368 KB |
Output is correct |
4 |
Correct |
5 ms |
7368 KB |
Output is correct |
5 |
Correct |
5 ms |
7380 KB |
Output is correct |
6 |
Correct |
5 ms |
7380 KB |
Output is correct |
7 |
Correct |
5 ms |
7452 KB |
Output is correct |
8 |
Correct |
6 ms |
7380 KB |
Output is correct |
9 |
Correct |
5 ms |
7380 KB |
Output is correct |
10 |
Correct |
5 ms |
7368 KB |
Output is correct |
11 |
Correct |
7 ms |
7508 KB |
Output is correct |
12 |
Correct |
7 ms |
7368 KB |
Output is correct |
13 |
Correct |
5 ms |
7372 KB |
Output is correct |
14 |
Correct |
5 ms |
7380 KB |
Output is correct |
15 |
Correct |
5 ms |
7372 KB |
Output is correct |
16 |
Correct |
5 ms |
7380 KB |
Output is correct |
17 |
Correct |
5 ms |
7380 KB |
Output is correct |
18 |
Correct |
5 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7368 KB |
Output is correct |
20 |
Correct |
4 ms |
7392 KB |
Output is correct |
21 |
Correct |
5 ms |
7380 KB |
Output is correct |
22 |
Correct |
4 ms |
7380 KB |
Output is correct |
23 |
Correct |
5 ms |
7368 KB |
Output is correct |
24 |
Correct |
5 ms |
7380 KB |
Output is correct |
25 |
Correct |
5 ms |
7380 KB |
Output is correct |
26 |
Correct |
5 ms |
7380 KB |
Output is correct |
27 |
Correct |
5 ms |
7460 KB |
Output is correct |
28 |
Correct |
4 ms |
7368 KB |
Output is correct |
29 |
Correct |
4 ms |
7380 KB |
Output is correct |
30 |
Correct |
5 ms |
7380 KB |
Output is correct |
31 |
Correct |
5 ms |
7380 KB |
Output is correct |
32 |
Correct |
5 ms |
7380 KB |
Output is correct |
33 |
Correct |
5 ms |
7380 KB |
Output is correct |
34 |
Correct |
5 ms |
7364 KB |
Output is correct |
35 |
Correct |
4 ms |
7380 KB |
Output is correct |
36 |
Correct |
4 ms |
7380 KB |
Output is correct |
37 |
Correct |
5 ms |
7444 KB |
Output is correct |
38 |
Correct |
5 ms |
7372 KB |
Output is correct |
39 |
Correct |
5 ms |
7380 KB |
Output is correct |
40 |
Correct |
5 ms |
7380 KB |
Output is correct |
41 |
Correct |
5 ms |
7380 KB |
Output is correct |
42 |
Correct |
5 ms |
7372 KB |
Output is correct |
43 |
Correct |
4 ms |
7364 KB |
Output is correct |
44 |
Correct |
4 ms |
7380 KB |
Output is correct |
45 |
Correct |
6 ms |
7380 KB |
Output is correct |
46 |
Correct |
11 ms |
7800 KB |
Output is correct |
47 |
Correct |
11 ms |
7748 KB |
Output is correct |
48 |
Correct |
11 ms |
7892 KB |
Output is correct |
49 |
Correct |
12 ms |
8020 KB |
Output is correct |
50 |
Correct |
9 ms |
8020 KB |
Output is correct |
51 |
Correct |
12 ms |
8020 KB |
Output is correct |
52 |
Correct |
10 ms |
8016 KB |
Output is correct |
53 |
Correct |
14 ms |
8020 KB |
Output is correct |
54 |
Correct |
14 ms |
8140 KB |
Output is correct |
55 |
Correct |
14 ms |
8020 KB |
Output is correct |
56 |
Correct |
9 ms |
7932 KB |
Output is correct |
57 |
Correct |
16 ms |
7764 KB |
Output is correct |
58 |
Correct |
13 ms |
7840 KB |
Output is correct |
59 |
Correct |
13 ms |
7764 KB |
Output is correct |
60 |
Correct |
17 ms |
7756 KB |
Output is correct |
61 |
Correct |
9 ms |
7892 KB |
Output is correct |
62 |
Correct |
9 ms |
7932 KB |
Output is correct |
63 |
Correct |
9 ms |
7892 KB |
Output is correct |
64 |
Correct |
11 ms |
7764 KB |
Output is correct |
65 |
Correct |
10 ms |
7764 KB |
Output is correct |
66 |
Correct |
11 ms |
7804 KB |
Output is correct |
67 |
Correct |
11 ms |
7764 KB |
Output is correct |
68 |
Correct |
8 ms |
8016 KB |
Output is correct |
69 |
Correct |
11 ms |
8016 KB |
Output is correct |
70 |
Correct |
15 ms |
8032 KB |
Output is correct |
71 |
Correct |
9 ms |
7888 KB |
Output is correct |
72 |
Correct |
13 ms |
7892 KB |
Output is correct |
73 |
Correct |
12 ms |
7768 KB |
Output is correct |
74 |
Correct |
11 ms |
7932 KB |
Output is correct |
75 |
Correct |
9 ms |
7892 KB |
Output is correct |
76 |
Correct |
8 ms |
7884 KB |
Output is correct |
77 |
Correct |
8 ms |
7764 KB |
Output is correct |
78 |
Correct |
10 ms |
7784 KB |
Output is correct |
79 |
Correct |
9 ms |
7764 KB |
Output is correct |
80 |
Correct |
9 ms |
7764 KB |
Output is correct |
81 |
Correct |
10 ms |
7888 KB |
Output is correct |
82 |
Correct |
10 ms |
7836 KB |
Output is correct |
83 |
Correct |
9 ms |
7824 KB |
Output is correct |
84 |
Correct |
9 ms |
7892 KB |
Output is correct |
85 |
Correct |
9 ms |
7764 KB |
Output is correct |
86 |
Correct |
9 ms |
7760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7368 KB |
Output is correct |
4 |
Correct |
5 ms |
7368 KB |
Output is correct |
5 |
Correct |
5 ms |
7380 KB |
Output is correct |
6 |
Correct |
5 ms |
7380 KB |
Output is correct |
7 |
Correct |
5 ms |
7452 KB |
Output is correct |
8 |
Correct |
6 ms |
7380 KB |
Output is correct |
9 |
Correct |
5 ms |
7380 KB |
Output is correct |
10 |
Correct |
5 ms |
7368 KB |
Output is correct |
11 |
Correct |
7 ms |
7508 KB |
Output is correct |
12 |
Correct |
7 ms |
7368 KB |
Output is correct |
13 |
Correct |
5 ms |
7372 KB |
Output is correct |
14 |
Correct |
5 ms |
7380 KB |
Output is correct |
15 |
Correct |
5 ms |
7372 KB |
Output is correct |
16 |
Correct |
5 ms |
7380 KB |
Output is correct |
17 |
Correct |
5 ms |
7380 KB |
Output is correct |
18 |
Correct |
5 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7368 KB |
Output is correct |
20 |
Correct |
4 ms |
7392 KB |
Output is correct |
21 |
Correct |
5 ms |
7380 KB |
Output is correct |
22 |
Correct |
4 ms |
7380 KB |
Output is correct |
23 |
Correct |
5 ms |
7368 KB |
Output is correct |
24 |
Correct |
5 ms |
7380 KB |
Output is correct |
25 |
Correct |
5 ms |
7380 KB |
Output is correct |
26 |
Correct |
5 ms |
7380 KB |
Output is correct |
27 |
Correct |
5 ms |
7460 KB |
Output is correct |
28 |
Correct |
4 ms |
7368 KB |
Output is correct |
29 |
Correct |
4 ms |
7380 KB |
Output is correct |
30 |
Correct |
5 ms |
7380 KB |
Output is correct |
31 |
Correct |
5 ms |
7380 KB |
Output is correct |
32 |
Correct |
5 ms |
7380 KB |
Output is correct |
33 |
Correct |
5 ms |
7380 KB |
Output is correct |
34 |
Correct |
5 ms |
7364 KB |
Output is correct |
35 |
Correct |
4 ms |
7380 KB |
Output is correct |
36 |
Correct |
4 ms |
7380 KB |
Output is correct |
37 |
Correct |
5 ms |
7444 KB |
Output is correct |
38 |
Correct |
5 ms |
7372 KB |
Output is correct |
39 |
Correct |
5 ms |
7380 KB |
Output is correct |
40 |
Correct |
5 ms |
7380 KB |
Output is correct |
41 |
Correct |
5 ms |
7380 KB |
Output is correct |
42 |
Correct |
5 ms |
7372 KB |
Output is correct |
43 |
Correct |
4 ms |
7364 KB |
Output is correct |
44 |
Correct |
4 ms |
7380 KB |
Output is correct |
45 |
Correct |
6 ms |
7380 KB |
Output is correct |
46 |
Correct |
11 ms |
7800 KB |
Output is correct |
47 |
Correct |
11 ms |
7748 KB |
Output is correct |
48 |
Correct |
11 ms |
7892 KB |
Output is correct |
49 |
Correct |
12 ms |
8020 KB |
Output is correct |
50 |
Correct |
9 ms |
8020 KB |
Output is correct |
51 |
Correct |
12 ms |
8020 KB |
Output is correct |
52 |
Correct |
10 ms |
8016 KB |
Output is correct |
53 |
Correct |
14 ms |
8020 KB |
Output is correct |
54 |
Correct |
14 ms |
8140 KB |
Output is correct |
55 |
Correct |
14 ms |
8020 KB |
Output is correct |
56 |
Correct |
9 ms |
7932 KB |
Output is correct |
57 |
Correct |
16 ms |
7764 KB |
Output is correct |
58 |
Correct |
13 ms |
7840 KB |
Output is correct |
59 |
Correct |
13 ms |
7764 KB |
Output is correct |
60 |
Correct |
17 ms |
7756 KB |
Output is correct |
61 |
Correct |
9 ms |
7892 KB |
Output is correct |
62 |
Correct |
9 ms |
7932 KB |
Output is correct |
63 |
Correct |
9 ms |
7892 KB |
Output is correct |
64 |
Correct |
11 ms |
7764 KB |
Output is correct |
65 |
Correct |
10 ms |
7764 KB |
Output is correct |
66 |
Correct |
11 ms |
7804 KB |
Output is correct |
67 |
Correct |
11 ms |
7764 KB |
Output is correct |
68 |
Correct |
8 ms |
8016 KB |
Output is correct |
69 |
Correct |
11 ms |
8016 KB |
Output is correct |
70 |
Correct |
15 ms |
8032 KB |
Output is correct |
71 |
Correct |
9 ms |
7888 KB |
Output is correct |
72 |
Correct |
13 ms |
7892 KB |
Output is correct |
73 |
Correct |
12 ms |
7768 KB |
Output is correct |
74 |
Correct |
11 ms |
7932 KB |
Output is correct |
75 |
Correct |
9 ms |
7892 KB |
Output is correct |
76 |
Correct |
8 ms |
7884 KB |
Output is correct |
77 |
Correct |
8 ms |
7764 KB |
Output is correct |
78 |
Correct |
10 ms |
7784 KB |
Output is correct |
79 |
Correct |
9 ms |
7764 KB |
Output is correct |
80 |
Correct |
9 ms |
7764 KB |
Output is correct |
81 |
Correct |
10 ms |
7888 KB |
Output is correct |
82 |
Correct |
10 ms |
7836 KB |
Output is correct |
83 |
Correct |
9 ms |
7824 KB |
Output is correct |
84 |
Correct |
9 ms |
7892 KB |
Output is correct |
85 |
Correct |
9 ms |
7764 KB |
Output is correct |
86 |
Correct |
9 ms |
7760 KB |
Output is correct |
87 |
Correct |
29 ms |
8652 KB |
Output is correct |
88 |
Correct |
67 ms |
11024 KB |
Output is correct |
89 |
Correct |
363 ms |
19568 KB |
Output is correct |
90 |
Correct |
267 ms |
19608 KB |
Output is correct |
91 |
Correct |
273 ms |
19524 KB |
Output is correct |
92 |
Correct |
142 ms |
24472 KB |
Output is correct |
93 |
Correct |
140 ms |
24512 KB |
Output is correct |
94 |
Correct |
157 ms |
24472 KB |
Output is correct |
95 |
Correct |
1220 ms |
26040 KB |
Output is correct |
96 |
Execution timed out |
2078 ms |
26456 KB |
Time limit exceeded |
97 |
Halted |
0 ms |
0 KB |
- |