Submission #68572


ソースコード

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
#include <iostream>
#include <vector>
#include <map>
using namespace std;
vector<vector<int> > graph, tree;
vector<int> color, parent, parent_color;
vector<map<int, int> > list;
int dfs(int cur, int par) {
parent[cur] = par;
if (par != -1) {
list[cur][color[par]]++;
parent_color[cur] = color[par];
}
for (int to : graph[cur]) {
if (to == par) continue;
list[cur][color[dfs(to, cur)]]++;
}
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);
color.resize(n);
parent.resize(n);
parent_color.resize(n);
list.resize(n);
for (int i = 0; i < n; ++i) {
cin >> color[i];
}
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 {
list[node][color]++;
};
auto del = [&](int node, int color) -> void {
if (--list[node][color] == 0) {
list[node].erase(color);
}
};
for (int i = 0; i < q; ++i) {
int x, c;
cin >> x >> c;
--x;
if (parent[x] != -1) {
del(x, parent_color[x]);
add(x, color[parent[x]]);
parent_color[x] = color[parent[x]];
del(parent[x], color[x]);
add(parent[x], c);
}
color[x] = c;
cout << list[x].size() << '\n';
}
return (0);
}

ステータス

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

セット

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

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
in01.txt AC 38 ms 604 KB
1
in02.txt AC 19 ms 400 KB
1
in03.txt AC 17 ms 580 KB
1
in04.txt AC 22 ms 548 KB
1
in05.txt AC 17 ms 560 KB
1
in06.txt AC 27 ms 1336 KB
1
in07.txt AC 33 ms 1468 KB
1
in08.txt AC 21 ms 1028 KB
1
in09.txt AC 22 ms 1192 KB
1
in10.txt AC 24 ms 1400 KB
1
in11.txt AC 388 ms 66976 KB
1
in12.txt AC 347 ms 83908 KB
1
in13.txt AC 552 ms 73864 KB
1
in14.txt AC 217 ms 22548 KB
1
in15.txt AC 329 ms 31632 KB
1
in16.txt AC 107 ms 15348 KB
1
in17.txt AC 607 ms 116868 KB
1
in18.txt AC 152 ms 9252 KB
1
in19.txt AC 340 ms 83472 KB
1
in20.txt AC 708 ms 100172 KB
1
in21.txt AC 426 ms 100192 KB
1
in22.txt AC 333 ms 46620 KB
1
in23.txt AC 548 ms 68856 KB
1
in24.txt AC 775 ms 121360 KB
1
in25.txt AC 565 ms 121068 KB
1
in26.txt AC 237 ms 45632 KB
1
in27.txt AC 917 ms 121472 KB
1
in28.txt AC 883 ms 101900 KB
1
in29.txt AC 968 ms 71328 KB
1
in30.txt AC 995 ms 112528 KB
1
in31.txt AC 1143 ms 131452 KB
1
in32.txt AC 1467 ms 134872 KB
1
in33.txt AC 1244 ms 138296 KB
1
in34.txt AC 876 ms 141716 KB
1
in35.txt AC 1377 ms 142704 KB
1
in36.txt AC 845 ms 142304 KB
1
in37.txt AC 835 ms 143188 KB
1
in38.txt AC 885 ms 144200 KB
1
in39.txt AC 648 ms 121792 KB
1
in40.txt AC 785 ms 139200 KB
1
in41.txt AC 115 ms 31344 KB
1
in42.txt AC 125 ms 32320 KB
1
in43.txt AC 838 ms 152956 KB
1
in44.txt AC 1299 ms 155112 KB
1
sample.txt AC 20 ms 37984 KB
1