From b85894c1ec17d798560c7c32ed855f9e02fd381b Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Sun, 12 Sep 2021 16:07:25 +0800 Subject: [PATCH] =?UTF-8?q?836.=20=E5=90=88=E5=B9=B6=E9=9B=86=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acwing.com/problem/content/submission/code_detail/7683086/ --- AcWing/836/836.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 AcWing/836/836.cpp diff --git a/AcWing/836/836.cpp b/AcWing/836/836.cpp new file mode 100644 index 00000000..708d9f62 --- /dev/null +++ b/AcWing/836/836.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +int n, m, a, b, fa[100005]; +char op; + +int getfa(int x) { + return fa[x] == x ? fa[x] : fa[x] = getfa(fa[x]); +} + +int main() { + cin >> n >> m; + for (int i = 1; i <= n; i++) { + fa[i] = i; + } + for (int i = 0; i < m; i++) { + cin >> op >> a >> b; + if (op == 'M') { + fa[getfa(a)] = getfa(b); + } else { + cout << (getfa(a) == getfa(b) ? "Yes" : "No") << endl; + } + } + return 0; +}