From b0cb34e532c7b94d04cda246d2080bafd86aad38 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 30 Mar 2021 20:09:44 +0800 Subject: import(solutions): Add acwing 2069. --- works/solutions/.gitignore | 3 ++ works/solutions/acwing/2069.cpp | 82 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 works/solutions/acwing/2069.cpp (limited to 'works') diff --git a/works/solutions/.gitignore b/works/solutions/.gitignore index fd9075a..951dd99 100644 --- a/works/solutions/.gitignore +++ b/works/solutions/.gitignore @@ -2,3 +2,6 @@ *.exe *.pdb *.ilk +*.obj +*.exp +*.lib diff --git a/works/solutions/acwing/2069.cpp b/works/solutions/acwing/2069.cpp new file mode 100644 index 0000000..fbfa6d2 --- /dev/null +++ b/works/solutions/acwing/2069.cpp @@ -0,0 +1,82 @@ +#include +#include + +struct Node { + int m = 0; + Node *set; + Node *left = nullptr; + Node *right = nullptr; + + Node() : set(this) {} + + void SetParent(Node *parent, bool l) { + set = parent; + l ? parent->left = this : parent->right = this; + } + + Node *GetSetRepresentative() { + Node *r = this; + while (r->set != r) { + r = r->set; + } + set = r; + return set; + } + + bool has_dfs = false; + + void Dfs() { + if (has_dfs) + return; + has_dfs = true; + if (left != nullptr) + left->Dfs(m); + if (right != nullptr) + right->Dfs(m); + } + + void Dfs(int c) { + m += c; + if (left != nullptr) + left->Dfs(m); + if (right != nullptr) + right->Dfs(m); + } +}; + +Node nodes[10010]; + +int main() { + std::ios_base::sync_with_stdio(false); + std::cin.tie(nullptr); + + int n, m; + + std::cin >> n >> m; + + for (int i = 0; i < m; i++) { + int operation, a, b; + std::cin >> operation >> a >> b; + + if (operation == 1) { + auto ra = nodes[a].GetSetRepresentative(), + rb = nodes[b].GetSetRepresentative(); + + if (ra != rb) { + auto node = new Node; + ra->SetParent(node, true); + rb->SetParent(node, false); + } + } else { + nodes[a].GetSetRepresentative()->m += b; + } + } + + for (int i = 1; i <= n; i++) { + auto &node = nodes[i]; + node.GetSetRepresentative()->Dfs(); + std::cout << node.m << ' '; + } + + return 0; +} -- cgit v1.2.3