Submission #3402357


Source Code Expand

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <iomanip>
using namespace std;
typedef long long unsigned int ll;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define REP(i,n) for(int i=0;i<n;i++)
#define REPS(i,f,n) for(int i=(f);i<(n);i++)
#define EACH(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)

#define DEBUG

#ifdef DEBUG
#define var_dump(...) fprintf(stdout, __VA_ARGS__)
#define dump(a) cout << a << "\n";
#else
#define var_dump(...) 42
#define dump(a) 42
#endif

typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

int dy4[]={0, 0, 1, -1};
int dx4[]={1, -1, 0, 0};
int dx8[]={0, 0, 1, -1, 1, 1, -1, -1};
int dy8[]={1, -1, 0, 0, 1, -1, -1, 1};

// https://beta.atcoder.jp/contests/abc049/tasks/arc065_b

int par[100005];
int ranks[100005];

// n要素で初期化
void init(int n) {
    REP(i, n) {
        par[i] = i;
        ranks[i] = 0;
    }
}

// 木の根を求める
int root(int x) {
    if (par[x] == x) {
        return x;
    } else {
        return par[x] = root(par[x]);
    }
}

// xとyの属する集合を併合
void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) return;

    if (ranks[x] < ranks[y]) {
        par[x] = y;
    } else {
        par[y] = x;
        if (ranks[x] == ranks[y]) ranks[x]++;
    }
}

// xとyが同じ集合に属するか
bool same(int x, int y) {
    return root(x) == root(y);
}


int main() {
    int N, Q; cin >> N >> Q;
    init(N);

    int p, a, b;
    REP(i, Q) {
        cin >> p;
        cin >> a >> b;

        if (p == 0)
            unite(a, b);
        else
            cout << (same(a, b) ? "Yes" : "No") << "\n";
    }

    return 0;
}

Submission Info

Submission Time
Task B - Union Find
User wakamenod
Language C++14 (GCC 5.4.1)
Score 100
Code Size 2146 Byte
Status AC
Exec Time 497 ms
Memory 1664 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 1
AC × 19
Set Name Test Cases
Sample 00_sample_01.txt
All 00_sample_01.txt, subtask_01_01.txt, subtask_01_02.txt, subtask_01_03.txt, subtask_01_04.txt, subtask_01_05.txt, subtask_01_06.txt, subtask_01_07.txt, subtask_01_08.txt, subtask_01_09.txt, subtask_01_10.txt, subtask_01_11.txt, subtask_01_12.txt, subtask_01_13.txt, subtask_01_14.txt, subtask_01_15.txt, subtask_01_16.txt, subtask_01_17.txt, subtask_01_18.txt
Case Name Status Exec Time Memory
00_sample_01.txt AC 1 ms 256 KB
subtask_01_01.txt AC 287 ms 640 KB
subtask_01_02.txt AC 1 ms 1024 KB
subtask_01_03.txt AC 428 ms 1024 KB
subtask_01_04.txt AC 491 ms 1664 KB
subtask_01_05.txt AC 25 ms 256 KB
subtask_01_06.txt AC 28 ms 1024 KB
subtask_01_07.txt AC 450 ms 896 KB
subtask_01_08.txt AC 492 ms 1664 KB
subtask_01_09.txt AC 1 ms 256 KB
subtask_01_10.txt AC 2 ms 1024 KB
subtask_01_11.txt AC 422 ms 1024 KB
subtask_01_12.txt AC 497 ms 1664 KB
subtask_01_13.txt AC 377 ms 768 KB
subtask_01_14.txt AC 2 ms 1024 KB
subtask_01_15.txt AC 440 ms 896 KB
subtask_01_16.txt AC 487 ms 1664 KB
subtask_01_17.txt AC 339 ms 1408 KB
subtask_01_18.txt AC 335 ms 1408 KB