summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-03-30 20:09:44 +0800
committercrupest <crupest@outlook.com>2021-03-30 20:09:44 +0800
commit9c5636025aa91333baaa16e81a43ce3f488ef785 (patch)
tree79277f959cdc2d70c8ff420e5d2ad9a082d0e74f
parentb08e829ab8797381d72a0b83050b55b24cbffc07 (diff)
downloadsolutions-9c5636025aa91333baaa16e81a43ce3f488ef785.tar.gz
solutions-9c5636025aa91333baaa16e81a43ce3f488ef785.tar.bz2
solutions-9c5636025aa91333baaa16e81a43ce3f488ef785.zip
Add acwing 2069.
-rw-r--r--.gitignore3
-rw-r--r--acwing/2069.cpp82
2 files changed, 85 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index fd9075a..951dd99 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,6 @@
*.exe
*.pdb
*.ilk
+*.obj
+*.exp
+*.lib
diff --git a/acwing/2069.cpp b/acwing/2069.cpp
new file mode 100644
index 0000000..fbfa6d2
--- /dev/null
+++ b/acwing/2069.cpp
@@ -0,0 +1,82 @@
+#include <iostream>
+#include <vector>
+
+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;
+}