Submission #68576


ソースコード

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <vector>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
struct Node {
int parent;
int color;
int parent_color;
__gnu_pbds::gp_hash_table<int, int> lis;
};
vector<vector<int> > graph;
vector<Node> tree;
int dfs(int cur, int par) {
tree[cur].parent = par;
if (par != -1) {
tree[cur].lis[tree[par].color]++;
tree[cur].parent_color = tree[par].color;
}
for (int to : graph[cur]) {
if (to == par) continue;
int child = dfs(to, cur);
tree[cur].lis[tree[child].color]++;
}
return (cur);
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
int n, q;
cin >> n >> q;
graph.resize(n);
tree.resize(n);
for (int i = 0; i < n; ++i) {
cin >> tree[i].color;
}
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
graph[u].emplace_back(v);
graph[v].emplace_back(u);
}
dfs(0, -1);
auto add = [&](int node, int color) -> void {
++tree[node].lis[color];
};
auto del = [&](int node, int color) -> void {
if (--tree[node].lis[color] == 0) {
tree[node].lis.erase(color);
}
};
for (int i = 0; i < q; ++i) {
int x, c;
cin >> x >> c;
--x;
if (tree[x].parent != -1) {
del(x, tree[x].parent_color);
add(x, tree[tree[x].parent].color);
tree[x].parent_color = tree[tree[x].parent].color;
del(tree[x].parent, tree[x].color);
add(tree[x].parent, c);
}
tree[x].color = c;
cout << tree[x].lis.size() << '\n';
}
return (0);
}

ステータス

項目 データ
問題 1527 - Colorful Tree
ユーザー名 ei1903
投稿日時 2021-09-23 18:13:52
言語 C++17
状態 Accepted
得点 4
ソースコード長 1819 Byte
最大実行時間 744 ms
最大メモリ使用量 198880 KB

セット

セット 得点 Cases
1 ALL 4 / 4 *

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
in01.txt AC 18 ms 604 KB
1
in02.txt AC 19 ms 388 KB
1
in03.txt AC 21 ms 560 KB
1
in04.txt AC 22 ms 480 KB
1
in05.txt AC 18 ms 588 KB
1
in06.txt AC 26 ms 1600 KB
1
in07.txt AC 26 ms 1844 KB
1
in08.txt AC 23 ms 1064 KB
1
in09.txt AC 27 ms 1212 KB
1
in10.txt AC 24 ms 1624 KB
1
in11.txt AC 342 ms 88184 KB
1
in12.txt AC 330 ms 110676 KB
1
in13.txt AC 462 ms 97244 KB
1
in14.txt AC 207 ms 29228 KB
1
in15.txt AC 280 ms 40976 KB
1
in16.txt AC 116 ms 19276 KB
1
in17.txt AC 505 ms 151628 KB
1
in18.txt AC 137 ms 10408 KB
1
in19.txt AC 288 ms 100356 KB
1
in20.txt AC 592 ms 130668 KB
1
in21.txt AC 421 ms 130740 KB
1
in22.txt AC 298 ms 59400 KB
1
in23.txt AC 478 ms 87560 KB
1
in24.txt AC 673 ms 155980 KB
1
in25.txt AC 497 ms 155592 KB
1
in26.txt AC 192 ms 57068 KB
1
in27.txt AC 353 ms 164280 KB
1
in28.txt AC 375 ms 137408 KB
1
in29.txt AC 297 ms 93220 KB
1
in30.txt AC 404 ms 150136 KB
1
in31.txt AC 472 ms 172244 KB
1
in32.txt AC 468 ms 175732 KB
1
in33.txt AC 475 ms 179092 KB
1
in34.txt AC 459 ms 182448 KB
1
in35.txt AC 573 ms 185168 KB
1
in36.txt AC 744 ms 177924 KB
1
in37.txt AC 718 ms 178684 KB
1
in38.txt AC 701 ms 179820 KB
1
in39.txt AC 665 ms 170580 KB
1
in40.txt AC 672 ms 177312 KB
1
in41.txt AC 119 ms 31280 KB
1
in42.txt AC 114 ms 32252 KB
1
in43.txt AC 436 ms 194868 KB
1
in44.txt AC 517 ms 198880 KB
1
sample.txt AC 21 ms 37932 KB
1