#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()){
if(!pares.empty() && pares.back().first == quantities.back().first){
pares.back().second += quantities.back().second;
}
else{
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:124:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < pares.size(); i++){
~~^~~~~~~~~~~~~~
construction.cpp:125: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:138:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
construction.cpp:141:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &color[i]);
~~~~~^~~~~~~~~~~~~~~~~
construction.cpp:145: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 |
47 ms |
68472 KB |
Output is correct |
2 |
Correct |
49 ms |
68472 KB |
Output is correct |
3 |
Correct |
47 ms |
68472 KB |
Output is correct |
4 |
Correct |
48 ms |
68472 KB |
Output is correct |
5 |
Correct |
49 ms |
68472 KB |
Output is correct |
6 |
Correct |
48 ms |
68472 KB |
Output is correct |
7 |
Correct |
49 ms |
68476 KB |
Output is correct |
8 |
Correct |
48 ms |
68480 KB |
Output is correct |
9 |
Correct |
50 ms |
68472 KB |
Output is correct |
10 |
Correct |
50 ms |
68472 KB |
Output is correct |
11 |
Correct |
49 ms |
68472 KB |
Output is correct |
12 |
Correct |
48 ms |
68472 KB |
Output is correct |
13 |
Correct |
49 ms |
68472 KB |
Output is correct |
14 |
Correct |
48 ms |
68472 KB |
Output is correct |
15 |
Correct |
48 ms |
68600 KB |
Output is correct |
16 |
Correct |
48 ms |
68480 KB |
Output is correct |
17 |
Correct |
49 ms |
68472 KB |
Output is correct |
18 |
Correct |
48 ms |
68532 KB |
Output is correct |
19 |
Correct |
48 ms |
68472 KB |
Output is correct |
20 |
Correct |
48 ms |
68480 KB |
Output is correct |
21 |
Correct |
49 ms |
68472 KB |
Output is correct |
22 |
Correct |
50 ms |
68480 KB |
Output is correct |
23 |
Correct |
50 ms |
68472 KB |
Output is correct |
24 |
Correct |
49 ms |
68472 KB |
Output is correct |
25 |
Correct |
50 ms |
68472 KB |
Output is correct |
26 |
Correct |
49 ms |
68608 KB |
Output is correct |
27 |
Correct |
50 ms |
68472 KB |
Output is correct |
28 |
Correct |
48 ms |
68472 KB |
Output is correct |
29 |
Correct |
48 ms |
68472 KB |
Output is correct |
30 |
Correct |
50 ms |
68480 KB |
Output is correct |
31 |
Correct |
49 ms |
68472 KB |
Output is correct |
32 |
Correct |
49 ms |
68476 KB |
Output is correct |
33 |
Correct |
52 ms |
68472 KB |
Output is correct |
34 |
Correct |
50 ms |
68472 KB |
Output is correct |
35 |
Correct |
49 ms |
68472 KB |
Output is correct |
36 |
Correct |
51 ms |
68484 KB |
Output is correct |
37 |
Correct |
49 ms |
68472 KB |
Output is correct |
38 |
Correct |
62 ms |
68472 KB |
Output is correct |
39 |
Correct |
49 ms |
68472 KB |
Output is correct |
40 |
Correct |
60 ms |
68472 KB |
Output is correct |
41 |
Correct |
56 ms |
68472 KB |
Output is correct |
42 |
Correct |
50 ms |
68472 KB |
Output is correct |
43 |
Correct |
55 ms |
68472 KB |
Output is correct |
44 |
Correct |
52 ms |
68472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
68472 KB |
Output is correct |
2 |
Correct |
49 ms |
68472 KB |
Output is correct |
3 |
Correct |
47 ms |
68472 KB |
Output is correct |
4 |
Correct |
48 ms |
68472 KB |
Output is correct |
5 |
Correct |
49 ms |
68472 KB |
Output is correct |
6 |
Correct |
48 ms |
68472 KB |
Output is correct |
7 |
Correct |
49 ms |
68476 KB |
Output is correct |
8 |
Correct |
48 ms |
68480 KB |
Output is correct |
9 |
Correct |
50 ms |
68472 KB |
Output is correct |
10 |
Correct |
50 ms |
68472 KB |
Output is correct |
11 |
Correct |
49 ms |
68472 KB |
Output is correct |
12 |
Correct |
48 ms |
68472 KB |
Output is correct |
13 |
Correct |
49 ms |
68472 KB |
Output is correct |
14 |
Correct |
48 ms |
68472 KB |
Output is correct |
15 |
Correct |
48 ms |
68600 KB |
Output is correct |
16 |
Correct |
48 ms |
68480 KB |
Output is correct |
17 |
Correct |
49 ms |
68472 KB |
Output is correct |
18 |
Correct |
48 ms |
68532 KB |
Output is correct |
19 |
Correct |
48 ms |
68472 KB |
Output is correct |
20 |
Correct |
48 ms |
68480 KB |
Output is correct |
21 |
Correct |
49 ms |
68472 KB |
Output is correct |
22 |
Correct |
50 ms |
68480 KB |
Output is correct |
23 |
Correct |
50 ms |
68472 KB |
Output is correct |
24 |
Correct |
49 ms |
68472 KB |
Output is correct |
25 |
Correct |
50 ms |
68472 KB |
Output is correct |
26 |
Correct |
49 ms |
68608 KB |
Output is correct |
27 |
Correct |
50 ms |
68472 KB |
Output is correct |
28 |
Correct |
48 ms |
68472 KB |
Output is correct |
29 |
Correct |
48 ms |
68472 KB |
Output is correct |
30 |
Correct |
50 ms |
68480 KB |
Output is correct |
31 |
Correct |
49 ms |
68472 KB |
Output is correct |
32 |
Correct |
49 ms |
68476 KB |
Output is correct |
33 |
Correct |
52 ms |
68472 KB |
Output is correct |
34 |
Correct |
50 ms |
68472 KB |
Output is correct |
35 |
Correct |
49 ms |
68472 KB |
Output is correct |
36 |
Correct |
51 ms |
68484 KB |
Output is correct |
37 |
Correct |
49 ms |
68472 KB |
Output is correct |
38 |
Correct |
62 ms |
68472 KB |
Output is correct |
39 |
Correct |
49 ms |
68472 KB |
Output is correct |
40 |
Correct |
60 ms |
68472 KB |
Output is correct |
41 |
Correct |
56 ms |
68472 KB |
Output is correct |
42 |
Correct |
50 ms |
68472 KB |
Output is correct |
43 |
Correct |
55 ms |
68472 KB |
Output is correct |
44 |
Correct |
52 ms |
68472 KB |
Output is correct |
45 |
Correct |
54 ms |
68480 KB |
Output is correct |
46 |
Correct |
68 ms |
68600 KB |
Output is correct |
47 |
Correct |
62 ms |
68636 KB |
Output is correct |
48 |
Correct |
55 ms |
68600 KB |
Output is correct |
49 |
Correct |
54 ms |
69112 KB |
Output is correct |
50 |
Correct |
57 ms |
68984 KB |
Output is correct |
51 |
Correct |
53 ms |
68984 KB |
Output is correct |
52 |
Correct |
58 ms |
68932 KB |
Output is correct |
53 |
Correct |
63 ms |
68924 KB |
Output is correct |
54 |
Correct |
64 ms |
68984 KB |
Output is correct |
55 |
Correct |
67 ms |
68856 KB |
Output is correct |
56 |
Correct |
55 ms |
68856 KB |
Output is correct |
57 |
Correct |
61 ms |
68584 KB |
Output is correct |
58 |
Correct |
59 ms |
68728 KB |
Output is correct |
59 |
Correct |
60 ms |
68608 KB |
Output is correct |
60 |
Correct |
59 ms |
68604 KB |
Output is correct |
61 |
Correct |
54 ms |
68864 KB |
Output is correct |
62 |
Correct |
55 ms |
68856 KB |
Output is correct |
63 |
Correct |
55 ms |
68876 KB |
Output is correct |
64 |
Correct |
57 ms |
68592 KB |
Output is correct |
65 |
Correct |
56 ms |
68688 KB |
Output is correct |
66 |
Correct |
57 ms |
68600 KB |
Output is correct |
67 |
Correct |
57 ms |
68608 KB |
Output is correct |
68 |
Correct |
57 ms |
69040 KB |
Output is correct |
69 |
Correct |
60 ms |
68856 KB |
Output is correct |
70 |
Correct |
65 ms |
68856 KB |
Output is correct |
71 |
Correct |
52 ms |
68856 KB |
Output is correct |
72 |
Correct |
61 ms |
68620 KB |
Output is correct |
73 |
Correct |
64 ms |
68600 KB |
Output is correct |
74 |
Correct |
60 ms |
68812 KB |
Output is correct |
75 |
Correct |
54 ms |
68728 KB |
Output is correct |
76 |
Correct |
56 ms |
68704 KB |
Output is correct |
77 |
Correct |
56 ms |
68728 KB |
Output is correct |
78 |
Correct |
55 ms |
68728 KB |
Output is correct |
79 |
Correct |
58 ms |
68704 KB |
Output is correct |
80 |
Correct |
56 ms |
68728 KB |
Output is correct |
81 |
Correct |
55 ms |
68728 KB |
Output is correct |
82 |
Correct |
69 ms |
68732 KB |
Output is correct |
83 |
Correct |
54 ms |
68728 KB |
Output is correct |
84 |
Correct |
54 ms |
68796 KB |
Output is correct |
85 |
Correct |
55 ms |
68728 KB |
Output is correct |
86 |
Correct |
70 ms |
68728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
68472 KB |
Output is correct |
2 |
Correct |
49 ms |
68472 KB |
Output is correct |
3 |
Correct |
47 ms |
68472 KB |
Output is correct |
4 |
Correct |
48 ms |
68472 KB |
Output is correct |
5 |
Correct |
49 ms |
68472 KB |
Output is correct |
6 |
Correct |
48 ms |
68472 KB |
Output is correct |
7 |
Correct |
49 ms |
68476 KB |
Output is correct |
8 |
Correct |
48 ms |
68480 KB |
Output is correct |
9 |
Correct |
50 ms |
68472 KB |
Output is correct |
10 |
Correct |
50 ms |
68472 KB |
Output is correct |
11 |
Correct |
49 ms |
68472 KB |
Output is correct |
12 |
Correct |
48 ms |
68472 KB |
Output is correct |
13 |
Correct |
49 ms |
68472 KB |
Output is correct |
14 |
Correct |
48 ms |
68472 KB |
Output is correct |
15 |
Correct |
48 ms |
68600 KB |
Output is correct |
16 |
Correct |
48 ms |
68480 KB |
Output is correct |
17 |
Correct |
49 ms |
68472 KB |
Output is correct |
18 |
Correct |
48 ms |
68532 KB |
Output is correct |
19 |
Correct |
48 ms |
68472 KB |
Output is correct |
20 |
Correct |
48 ms |
68480 KB |
Output is correct |
21 |
Correct |
49 ms |
68472 KB |
Output is correct |
22 |
Correct |
50 ms |
68480 KB |
Output is correct |
23 |
Correct |
50 ms |
68472 KB |
Output is correct |
24 |
Correct |
49 ms |
68472 KB |
Output is correct |
25 |
Correct |
50 ms |
68472 KB |
Output is correct |
26 |
Correct |
49 ms |
68608 KB |
Output is correct |
27 |
Correct |
50 ms |
68472 KB |
Output is correct |
28 |
Correct |
48 ms |
68472 KB |
Output is correct |
29 |
Correct |
48 ms |
68472 KB |
Output is correct |
30 |
Correct |
50 ms |
68480 KB |
Output is correct |
31 |
Correct |
49 ms |
68472 KB |
Output is correct |
32 |
Correct |
49 ms |
68476 KB |
Output is correct |
33 |
Correct |
52 ms |
68472 KB |
Output is correct |
34 |
Correct |
50 ms |
68472 KB |
Output is correct |
35 |
Correct |
49 ms |
68472 KB |
Output is correct |
36 |
Correct |
51 ms |
68484 KB |
Output is correct |
37 |
Correct |
49 ms |
68472 KB |
Output is correct |
38 |
Correct |
62 ms |
68472 KB |
Output is correct |
39 |
Correct |
49 ms |
68472 KB |
Output is correct |
40 |
Correct |
60 ms |
68472 KB |
Output is correct |
41 |
Correct |
56 ms |
68472 KB |
Output is correct |
42 |
Correct |
50 ms |
68472 KB |
Output is correct |
43 |
Correct |
55 ms |
68472 KB |
Output is correct |
44 |
Correct |
52 ms |
68472 KB |
Output is correct |
45 |
Correct |
54 ms |
68480 KB |
Output is correct |
46 |
Correct |
68 ms |
68600 KB |
Output is correct |
47 |
Correct |
62 ms |
68636 KB |
Output is correct |
48 |
Correct |
55 ms |
68600 KB |
Output is correct |
49 |
Correct |
54 ms |
69112 KB |
Output is correct |
50 |
Correct |
57 ms |
68984 KB |
Output is correct |
51 |
Correct |
53 ms |
68984 KB |
Output is correct |
52 |
Correct |
58 ms |
68932 KB |
Output is correct |
53 |
Correct |
63 ms |
68924 KB |
Output is correct |
54 |
Correct |
64 ms |
68984 KB |
Output is correct |
55 |
Correct |
67 ms |
68856 KB |
Output is correct |
56 |
Correct |
55 ms |
68856 KB |
Output is correct |
57 |
Correct |
61 ms |
68584 KB |
Output is correct |
58 |
Correct |
59 ms |
68728 KB |
Output is correct |
59 |
Correct |
60 ms |
68608 KB |
Output is correct |
60 |
Correct |
59 ms |
68604 KB |
Output is correct |
61 |
Correct |
54 ms |
68864 KB |
Output is correct |
62 |
Correct |
55 ms |
68856 KB |
Output is correct |
63 |
Correct |
55 ms |
68876 KB |
Output is correct |
64 |
Correct |
57 ms |
68592 KB |
Output is correct |
65 |
Correct |
56 ms |
68688 KB |
Output is correct |
66 |
Correct |
57 ms |
68600 KB |
Output is correct |
67 |
Correct |
57 ms |
68608 KB |
Output is correct |
68 |
Correct |
57 ms |
69040 KB |
Output is correct |
69 |
Correct |
60 ms |
68856 KB |
Output is correct |
70 |
Correct |
65 ms |
68856 KB |
Output is correct |
71 |
Correct |
52 ms |
68856 KB |
Output is correct |
72 |
Correct |
61 ms |
68620 KB |
Output is correct |
73 |
Correct |
64 ms |
68600 KB |
Output is correct |
74 |
Correct |
60 ms |
68812 KB |
Output is correct |
75 |
Correct |
54 ms |
68728 KB |
Output is correct |
76 |
Correct |
56 ms |
68704 KB |
Output is correct |
77 |
Correct |
56 ms |
68728 KB |
Output is correct |
78 |
Correct |
55 ms |
68728 KB |
Output is correct |
79 |
Correct |
58 ms |
68704 KB |
Output is correct |
80 |
Correct |
56 ms |
68728 KB |
Output is correct |
81 |
Correct |
55 ms |
68728 KB |
Output is correct |
82 |
Correct |
69 ms |
68732 KB |
Output is correct |
83 |
Correct |
54 ms |
68728 KB |
Output is correct |
84 |
Correct |
54 ms |
68796 KB |
Output is correct |
85 |
Correct |
55 ms |
68728 KB |
Output is correct |
86 |
Correct |
70 ms |
68728 KB |
Output is correct |
87 |
Correct |
69 ms |
68984 KB |
Output is correct |
88 |
Correct |
96 ms |
69940 KB |
Output is correct |
89 |
Correct |
254 ms |
73308 KB |
Output is correct |
90 |
Correct |
240 ms |
73208 KB |
Output is correct |
91 |
Correct |
253 ms |
73288 KB |
Output is correct |
92 |
Correct |
180 ms |
83576 KB |
Output is correct |
93 |
Correct |
173 ms |
83576 KB |
Output is correct |
94 |
Correct |
167 ms |
83544 KB |
Output is correct |
95 |
Correct |
1458 ms |
78620 KB |
Output is correct |
96 |
Execution timed out |
2080 ms |
78580 KB |
Time limit exceeded |
97 |
Halted |
0 ms |
0 KB |
- |