Submission #68573


ソースコード

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 <map>
using namespace std;
struct Node {
int parent;
int color;
int parent_color;
map<int, int> list;
};
vector<vector<int> > graph;
vector<Node> tree;
int dfs(int cur, int par) {
tree[cur].parent = par;
if (par != -1) {
tree[cur].list[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].list[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].list[color];
};
auto del = [&](int node, int color) -> void {
if (--tree[node].list[color] == 0) {
tree[node].list.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].list.size() << '\n';
}
return (0);
}

ステータス

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

セット

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

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
in01.txt AC 26 ms 472 KB
1
in02.txt AC 18 ms 520 KB
1
in03.txt AC 19 ms 580 KB
1
in04.txt AC 16 ms 552 KB
1
in05.txt AC 22 ms 568 KB
1
in06.txt AC 24 ms 1232 KB
1
in07.txt AC 18 ms 1316 KB
1
in08.txt AC 24 ms 824 KB
1
in09.txt AC 27 ms 892 KB
1
in10.txt AC 30 ms 1140 KB
1
in11.txt AC 379 ms 61156 KB
1
in12.txt AC 346 ms 76752 KB
1
in13.txt AC 529 ms 67692 KB
1
in14.txt AC 201 ms 20924 KB
1
in15.txt AC 306 ms 29300 KB
1
in16.txt AC 100 ms 14412 KB
1
in17.txt AC 571 ms 107360 KB
1
in18.txt AC 150 ms 8660 KB
1
in19.txt AC 311 ms 74592 KB
1
in20.txt AC 669 ms 92284 KB
1
in21.txt AC 405 ms 94224 KB
1
in22.txt AC 328 ms 43088 KB
1
in23.txt AC 527 ms 63688 KB
1
in24.txt AC 739 ms 111756 KB
1
in25.txt AC 548 ms 111388 KB
1
in26.txt AC 207 ms 42356 KB
1
in27.txt AC 695 ms 112204 KB
1
in28.txt AC 747 ms 94196 KB
1
in29.txt AC 867 ms 66212 KB
1
in30.txt AC 825 ms 104060 KB
1
in31.txt AC 991 ms 121628 KB
1
in32.txt AC 1062 ms 125096 KB
1
in33.txt AC 1083 ms 128564 KB
1
in34.txt AC 758 ms 131900 KB
1
in35.txt AC 1235 ms 133964 KB
1
in36.txt AC 780 ms 132464 KB
1
in37.txt AC 793 ms 133528 KB
1
in38.txt AC 804 ms 134332 KB
1
in39.txt AC 623 ms 111960 KB
1
in40.txt AC 788 ms 129416 KB
1
in41.txt AC 121 ms 31204 KB
1
in42.txt AC 123 ms 32304 KB
1
in43.txt AC 782 ms 143212 KB
1
in44.txt AC 1258 ms 145416 KB
1
sample.txt AC 23 ms 38068 KB
1