#include <bits/stdc++.h>
using namespace std;
typedef tuple<int,int,int> trinca;
typedef pair<int,int> ii;
const int MAXN = 1e5 + 10;
vector<int> grafo[MAXN];
vector<ii> quantities;
deque<trinca> componente[MAXN];
int pai[MAXN], szTree[MAXN], color[MAXN], aresta1[MAXN], aresta2[MAXN], N;
int chainId[MAXN], posChain[MAXN], headChain[MAXN], chainPtr, lastChain;
void dfs1(int v, int p){
szTree[v] = 1;
pai[v] = p;
for(int u: grafo[v]){
if(u == p) continue;
dfs1(u, v);
szTree[v] += szTree[u];
}
}
void dfs2(int v, int p){
int mx = -1, big = -1;
if(chainId[v] == 0){
chainId[v] = ++lastChain;
headChain[chainId[v]] = v;
posChain[v] = ++chainPtr;
}
componente[chainId[v]].push_back(make_tuple(color[v], posChain[v], posChain[v]));
for(int u: grafo[v]){
if(u == p) continue;
if(szTree[u] > mx){
mx = szTree[u];
big = u;
}
}
if(big != -1){
chainId[big] = chainId[v];
posChain[big] = ++chainPtr;
dfs2(big, v);
}
for(int u: grafo[v]){
if(u == p || u == big) continue;
dfs2(u, v);
}
}
long long query_up(int v){
//printf("#######\nQuery %d\n", v);
vector<ii> pares;
int new_color = color[v];
v = pai[v];
long long ans = 0;
while(v != -1){
int thisChain = chainId[v];
int thisHead = headChain[thisChain];
int lo = posChain[thisHead];
int hi = posChain[v];
//printf("V %d tC %d tH %d (%d, %d)\n", v, thisChain, thisHead, lo, hi);
while(!componente[thisChain].empty()){
//printf("Loop\n");
trinca davez = componente[thisChain].front();
if(get<1>(davez) > hi){
break;
}
else if(get<2>(davez) <= hi){
int whichColor = get<0>(davez);
int whichSize = get<2>(davez) - get<1>(davez) + 1;
componente[thisChain].pop_front();
quantities.push_back(ii(whichColor, whichSize));
}
else{
int whichColor = get<0>(davez);
int oldBegin = get<1>(davez);
int oldEnd = get<2>(davez);
componente[thisChain].pop_front();
componente[thisChain].push_front(make_tuple(whichColor, hi + 1, oldEnd));
int whichSize = hi - oldBegin + 1;
quantities.push_back(ii(whichColor, whichSize));
}
}
while(!quantities.empty()){
pares.push_back(quantities.back());
quantities.pop_back();
}
componente[thisChain].push_front(make_tuple(new_color, lo, hi));
v = pai[thisHead];
}
reverse(pares.begin(), pares.end());
/*
for(int i = 0; i < pares.size(); i++){
printf("(%d, %d)", pares[i].first, pares[i].second);
}
printf("\n");
printf("#######\n");*/
for(int i = 0; i < pares.size(); i++){
for(int j = i+1; j < pares.size(); j++){
if(pares[i].first > pares[j].first){
ans += 1LL*pares[i].second*pares[j].second;
}
}
}
return ans;
}
int main(){
scanf("%d", &N);
for(int i = 1; i <= N; i++){
scanf("%d", &color[i]);
}
for(int i = 1; i < N; i++){
scanf("%d %d", &aresta1[i], &aresta2[i]);
grafo[aresta1[i]].push_back(aresta2[i]);
}
dfs1(1, -1);
dfs2(1, -1);
for(int i = 1; i < N; i++){
int v = aresta2[i];
long long ans = query_up(v);
printf("%lld\n", ans);
}
return 0;
}
Compilation message
construction.cpp: In function 'long long int query_up(int)':
construction.cpp:119:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < pares.size(); i++){
~~^~~~~~~~~~~~~~
construction.cpp:120:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = i+1; j < pares.size(); j++){
~~^~~~~~~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:133:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
construction.cpp:136:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &color[i]);
~~~~~^~~~~~~~~~~~~~~~~
construction.cpp:140:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &aresta1[i], &aresta2[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
68472 KB |
Output is correct |
2 |
Correct |
48 ms |
68476 KB |
Output is correct |
3 |
Correct |
49 ms |
68472 KB |
Output is correct |
4 |
Correct |
51 ms |
68480 KB |
Output is correct |
5 |
Correct |
60 ms |
68472 KB |
Output is correct |
6 |
Correct |
51 ms |
68472 KB |
Output is correct |
7 |
Correct |
49 ms |
68472 KB |
Output is correct |
8 |
Correct |
56 ms |
68592 KB |
Output is correct |
9 |
Correct |
59 ms |
68472 KB |
Output is correct |
10 |
Correct |
53 ms |
68600 KB |
Output is correct |
11 |
Correct |
60 ms |
68472 KB |
Output is correct |
12 |
Correct |
60 ms |
68472 KB |
Output is correct |
13 |
Correct |
50 ms |
68480 KB |
Output is correct |
14 |
Correct |
52 ms |
68600 KB |
Output is correct |
15 |
Correct |
49 ms |
68472 KB |
Output is correct |
16 |
Correct |
61 ms |
68472 KB |
Output is correct |
17 |
Correct |
62 ms |
68472 KB |
Output is correct |
18 |
Correct |
50 ms |
68472 KB |
Output is correct |
19 |
Correct |
50 ms |
68472 KB |
Output is correct |
20 |
Correct |
50 ms |
68564 KB |
Output is correct |
21 |
Correct |
49 ms |
68472 KB |
Output is correct |
22 |
Correct |
56 ms |
68516 KB |
Output is correct |
23 |
Correct |
49 ms |
68472 KB |
Output is correct |
24 |
Correct |
49 ms |
68540 KB |
Output is correct |
25 |
Correct |
56 ms |
68472 KB |
Output is correct |
26 |
Correct |
49 ms |
68472 KB |
Output is correct |
27 |
Correct |
49 ms |
68472 KB |
Output is correct |
28 |
Correct |
49 ms |
68480 KB |
Output is correct |
29 |
Correct |
50 ms |
68472 KB |
Output is correct |
30 |
Correct |
48 ms |
68460 KB |
Output is correct |
31 |
Correct |
52 ms |
68472 KB |
Output is correct |
32 |
Correct |
50 ms |
68472 KB |
Output is correct |
33 |
Correct |
50 ms |
68556 KB |
Output is correct |
34 |
Correct |
49 ms |
68472 KB |
Output is correct |
35 |
Correct |
49 ms |
68472 KB |
Output is correct |
36 |
Correct |
50 ms |
68472 KB |
Output is correct |
37 |
Correct |
60 ms |
68472 KB |
Output is correct |
38 |
Correct |
49 ms |
68472 KB |
Output is correct |
39 |
Correct |
49 ms |
68472 KB |
Output is correct |
40 |
Correct |
49 ms |
68556 KB |
Output is correct |
41 |
Correct |
50 ms |
68472 KB |
Output is correct |
42 |
Correct |
58 ms |
68504 KB |
Output is correct |
43 |
Correct |
55 ms |
68472 KB |
Output is correct |
44 |
Correct |
48 ms |
68480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
68472 KB |
Output is correct |
2 |
Correct |
48 ms |
68476 KB |
Output is correct |
3 |
Correct |
49 ms |
68472 KB |
Output is correct |
4 |
Correct |
51 ms |
68480 KB |
Output is correct |
5 |
Correct |
60 ms |
68472 KB |
Output is correct |
6 |
Correct |
51 ms |
68472 KB |
Output is correct |
7 |
Correct |
49 ms |
68472 KB |
Output is correct |
8 |
Correct |
56 ms |
68592 KB |
Output is correct |
9 |
Correct |
59 ms |
68472 KB |
Output is correct |
10 |
Correct |
53 ms |
68600 KB |
Output is correct |
11 |
Correct |
60 ms |
68472 KB |
Output is correct |
12 |
Correct |
60 ms |
68472 KB |
Output is correct |
13 |
Correct |
50 ms |
68480 KB |
Output is correct |
14 |
Correct |
52 ms |
68600 KB |
Output is correct |
15 |
Correct |
49 ms |
68472 KB |
Output is correct |
16 |
Correct |
61 ms |
68472 KB |
Output is correct |
17 |
Correct |
62 ms |
68472 KB |
Output is correct |
18 |
Correct |
50 ms |
68472 KB |
Output is correct |
19 |
Correct |
50 ms |
68472 KB |
Output is correct |
20 |
Correct |
50 ms |
68564 KB |
Output is correct |
21 |
Correct |
49 ms |
68472 KB |
Output is correct |
22 |
Correct |
56 ms |
68516 KB |
Output is correct |
23 |
Correct |
49 ms |
68472 KB |
Output is correct |
24 |
Correct |
49 ms |
68540 KB |
Output is correct |
25 |
Correct |
56 ms |
68472 KB |
Output is correct |
26 |
Correct |
49 ms |
68472 KB |
Output is correct |
27 |
Correct |
49 ms |
68472 KB |
Output is correct |
28 |
Correct |
49 ms |
68480 KB |
Output is correct |
29 |
Correct |
50 ms |
68472 KB |
Output is correct |
30 |
Correct |
48 ms |
68460 KB |
Output is correct |
31 |
Correct |
52 ms |
68472 KB |
Output is correct |
32 |
Correct |
50 ms |
68472 KB |
Output is correct |
33 |
Correct |
50 ms |
68556 KB |
Output is correct |
34 |
Correct |
49 ms |
68472 KB |
Output is correct |
35 |
Correct |
49 ms |
68472 KB |
Output is correct |
36 |
Correct |
50 ms |
68472 KB |
Output is correct |
37 |
Correct |
60 ms |
68472 KB |
Output is correct |
38 |
Correct |
49 ms |
68472 KB |
Output is correct |
39 |
Correct |
49 ms |
68472 KB |
Output is correct |
40 |
Correct |
49 ms |
68556 KB |
Output is correct |
41 |
Correct |
50 ms |
68472 KB |
Output is correct |
42 |
Correct |
58 ms |
68504 KB |
Output is correct |
43 |
Correct |
55 ms |
68472 KB |
Output is correct |
44 |
Correct |
48 ms |
68480 KB |
Output is correct |
45 |
Correct |
50 ms |
68480 KB |
Output is correct |
46 |
Correct |
71 ms |
68728 KB |
Output is correct |
47 |
Correct |
80 ms |
68728 KB |
Output is correct |
48 |
Correct |
64 ms |
68728 KB |
Output is correct |
49 |
Correct |
60 ms |
69112 KB |
Output is correct |
50 |
Correct |
64 ms |
69112 KB |
Output is correct |
51 |
Correct |
54 ms |
69112 KB |
Output is correct |
52 |
Correct |
57 ms |
68984 KB |
Output is correct |
53 |
Correct |
65 ms |
68984 KB |
Output is correct |
54 |
Correct |
60 ms |
68988 KB |
Output is correct |
55 |
Correct |
62 ms |
68984 KB |
Output is correct |
56 |
Correct |
52 ms |
68856 KB |
Output is correct |
57 |
Correct |
58 ms |
68728 KB |
Output is correct |
58 |
Correct |
57 ms |
68732 KB |
Output is correct |
59 |
Correct |
59 ms |
68728 KB |
Output is correct |
60 |
Correct |
67 ms |
68856 KB |
Output is correct |
61 |
Correct |
52 ms |
68856 KB |
Output is correct |
62 |
Correct |
53 ms |
68856 KB |
Output is correct |
63 |
Correct |
53 ms |
68856 KB |
Output is correct |
64 |
Correct |
54 ms |
68728 KB |
Output is correct |
65 |
Correct |
55 ms |
68728 KB |
Output is correct |
66 |
Correct |
54 ms |
68728 KB |
Output is correct |
67 |
Correct |
52 ms |
68728 KB |
Output is correct |
68 |
Correct |
53 ms |
69112 KB |
Output is correct |
69 |
Correct |
56 ms |
68984 KB |
Output is correct |
70 |
Correct |
61 ms |
68984 KB |
Output is correct |
71 |
Correct |
51 ms |
68856 KB |
Output is correct |
72 |
Correct |
57 ms |
68856 KB |
Output is correct |
73 |
Correct |
69 ms |
68728 KB |
Output is correct |
74 |
Correct |
53 ms |
68856 KB |
Output is correct |
75 |
Correct |
53 ms |
68856 KB |
Output is correct |
76 |
Correct |
55 ms |
68772 KB |
Output is correct |
77 |
Correct |
62 ms |
68732 KB |
Output is correct |
78 |
Correct |
54 ms |
68856 KB |
Output is correct |
79 |
Correct |
62 ms |
68728 KB |
Output is correct |
80 |
Correct |
52 ms |
68728 KB |
Output is correct |
81 |
Correct |
53 ms |
68856 KB |
Output is correct |
82 |
Correct |
59 ms |
68856 KB |
Output is correct |
83 |
Correct |
54 ms |
68728 KB |
Output is correct |
84 |
Correct |
63 ms |
68864 KB |
Output is correct |
85 |
Correct |
53 ms |
68728 KB |
Output is correct |
86 |
Correct |
63 ms |
68728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
68472 KB |
Output is correct |
2 |
Correct |
48 ms |
68476 KB |
Output is correct |
3 |
Correct |
49 ms |
68472 KB |
Output is correct |
4 |
Correct |
51 ms |
68480 KB |
Output is correct |
5 |
Correct |
60 ms |
68472 KB |
Output is correct |
6 |
Correct |
51 ms |
68472 KB |
Output is correct |
7 |
Correct |
49 ms |
68472 KB |
Output is correct |
8 |
Correct |
56 ms |
68592 KB |
Output is correct |
9 |
Correct |
59 ms |
68472 KB |
Output is correct |
10 |
Correct |
53 ms |
68600 KB |
Output is correct |
11 |
Correct |
60 ms |
68472 KB |
Output is correct |
12 |
Correct |
60 ms |
68472 KB |
Output is correct |
13 |
Correct |
50 ms |
68480 KB |
Output is correct |
14 |
Correct |
52 ms |
68600 KB |
Output is correct |
15 |
Correct |
49 ms |
68472 KB |
Output is correct |
16 |
Correct |
61 ms |
68472 KB |
Output is correct |
17 |
Correct |
62 ms |
68472 KB |
Output is correct |
18 |
Correct |
50 ms |
68472 KB |
Output is correct |
19 |
Correct |
50 ms |
68472 KB |
Output is correct |
20 |
Correct |
50 ms |
68564 KB |
Output is correct |
21 |
Correct |
49 ms |
68472 KB |
Output is correct |
22 |
Correct |
56 ms |
68516 KB |
Output is correct |
23 |
Correct |
49 ms |
68472 KB |
Output is correct |
24 |
Correct |
49 ms |
68540 KB |
Output is correct |
25 |
Correct |
56 ms |
68472 KB |
Output is correct |
26 |
Correct |
49 ms |
68472 KB |
Output is correct |
27 |
Correct |
49 ms |
68472 KB |
Output is correct |
28 |
Correct |
49 ms |
68480 KB |
Output is correct |
29 |
Correct |
50 ms |
68472 KB |
Output is correct |
30 |
Correct |
48 ms |
68460 KB |
Output is correct |
31 |
Correct |
52 ms |
68472 KB |
Output is correct |
32 |
Correct |
50 ms |
68472 KB |
Output is correct |
33 |
Correct |
50 ms |
68556 KB |
Output is correct |
34 |
Correct |
49 ms |
68472 KB |
Output is correct |
35 |
Correct |
49 ms |
68472 KB |
Output is correct |
36 |
Correct |
50 ms |
68472 KB |
Output is correct |
37 |
Correct |
60 ms |
68472 KB |
Output is correct |
38 |
Correct |
49 ms |
68472 KB |
Output is correct |
39 |
Correct |
49 ms |
68472 KB |
Output is correct |
40 |
Correct |
49 ms |
68556 KB |
Output is correct |
41 |
Correct |
50 ms |
68472 KB |
Output is correct |
42 |
Correct |
58 ms |
68504 KB |
Output is correct |
43 |
Correct |
55 ms |
68472 KB |
Output is correct |
44 |
Correct |
48 ms |
68480 KB |
Output is correct |
45 |
Correct |
50 ms |
68480 KB |
Output is correct |
46 |
Correct |
71 ms |
68728 KB |
Output is correct |
47 |
Correct |
80 ms |
68728 KB |
Output is correct |
48 |
Correct |
64 ms |
68728 KB |
Output is correct |
49 |
Correct |
60 ms |
69112 KB |
Output is correct |
50 |
Correct |
64 ms |
69112 KB |
Output is correct |
51 |
Correct |
54 ms |
69112 KB |
Output is correct |
52 |
Correct |
57 ms |
68984 KB |
Output is correct |
53 |
Correct |
65 ms |
68984 KB |
Output is correct |
54 |
Correct |
60 ms |
68988 KB |
Output is correct |
55 |
Correct |
62 ms |
68984 KB |
Output is correct |
56 |
Correct |
52 ms |
68856 KB |
Output is correct |
57 |
Correct |
58 ms |
68728 KB |
Output is correct |
58 |
Correct |
57 ms |
68732 KB |
Output is correct |
59 |
Correct |
59 ms |
68728 KB |
Output is correct |
60 |
Correct |
67 ms |
68856 KB |
Output is correct |
61 |
Correct |
52 ms |
68856 KB |
Output is correct |
62 |
Correct |
53 ms |
68856 KB |
Output is correct |
63 |
Correct |
53 ms |
68856 KB |
Output is correct |
64 |
Correct |
54 ms |
68728 KB |
Output is correct |
65 |
Correct |
55 ms |
68728 KB |
Output is correct |
66 |
Correct |
54 ms |
68728 KB |
Output is correct |
67 |
Correct |
52 ms |
68728 KB |
Output is correct |
68 |
Correct |
53 ms |
69112 KB |
Output is correct |
69 |
Correct |
56 ms |
68984 KB |
Output is correct |
70 |
Correct |
61 ms |
68984 KB |
Output is correct |
71 |
Correct |
51 ms |
68856 KB |
Output is correct |
72 |
Correct |
57 ms |
68856 KB |
Output is correct |
73 |
Correct |
69 ms |
68728 KB |
Output is correct |
74 |
Correct |
53 ms |
68856 KB |
Output is correct |
75 |
Correct |
53 ms |
68856 KB |
Output is correct |
76 |
Correct |
55 ms |
68772 KB |
Output is correct |
77 |
Correct |
62 ms |
68732 KB |
Output is correct |
78 |
Correct |
54 ms |
68856 KB |
Output is correct |
79 |
Correct |
62 ms |
68728 KB |
Output is correct |
80 |
Correct |
52 ms |
68728 KB |
Output is correct |
81 |
Correct |
53 ms |
68856 KB |
Output is correct |
82 |
Correct |
59 ms |
68856 KB |
Output is correct |
83 |
Correct |
54 ms |
68728 KB |
Output is correct |
84 |
Correct |
63 ms |
68864 KB |
Output is correct |
85 |
Correct |
53 ms |
68728 KB |
Output is correct |
86 |
Correct |
63 ms |
68728 KB |
Output is correct |
87 |
Correct |
62 ms |
69112 KB |
Output is correct |
88 |
Correct |
100 ms |
70520 KB |
Output is correct |
89 |
Correct |
277 ms |
75460 KB |
Output is correct |
90 |
Correct |
267 ms |
75384 KB |
Output is correct |
91 |
Correct |
246 ms |
75384 KB |
Output is correct |
92 |
Correct |
168 ms |
85624 KB |
Output is correct |
93 |
Correct |
165 ms |
85752 KB |
Output is correct |
94 |
Correct |
162 ms |
85672 KB |
Output is correct |
95 |
Correct |
1384 ms |
80368 KB |
Output is correct |
96 |
Execution timed out |
2086 ms |
80628 KB |
Time limit exceeded |
97 |
Halted |
0 ms |
0 KB |
- |