Submission #7129306


Source Code Expand

#include <iostream>
#include <cstdlib>
#include <vector>
#include <stack>
#include <string>
#include <climits>
#include <algorithm>
#include <functional>
#include <string.h>
#include <numeric>
#include <math.h>

#define LOOP(N) for(int i=0; i<(N); ++i)
#define REP(i, N) for(int i=0; i<(N); ++i)
#define FOR(i, start, end) for(int i=(start); i<(end); ++i)
#define ALL(a) (a).begin(),(a).end()

using namespace std;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using Graph = vector< vector<int> >;

void sayYes() {cout << "Yes" << endl;}
void sayNo() {cout << "No" << endl;}


class UnionFind {
    vector<int> parent;
    
    int rank(int v) {
        return -parent[v];
    }
public:
    UnionFind(int N) : parent(N, -1) {}
    int getRoot(int v) {
        return parent[v] < 0 ? v : parent[v] = getRoot(parent[v]);
    }
    void unite(int a, int b) {
        a = getRoot(a);
        b = getRoot(b);
        if (a == b) return;
        
        if (rank(a) < rank(b)) swap(a, b);
        parent[a] += parent[b];
        parent[b] = a;
    }
    bool isSameGroup(int a, int b) {
        return getRoot(a) == getRoot(b);
    }
    void print(int a, int b) {
        cout << a << " " << parent[a] << " " << b << " " << parent[b] << endl;
    }
};


int main() {
    int N, Q; cin >> N >> Q;
    UnionFind unf(N);

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

        // conbine
        if (p == 0) {
            unf.unite(a, b);
        }
        // judge
        if (p == 1) {
            if (unf.isSameGroup(a, b)) sayYes();
            else sayNo();
            // unf.print(a, b);
        }
    }
}

Submission Info

Submission Time
Task B - Union Find
User scientistb
Language C++14 (GCC 5.4.1)
Score 100
Code Size 1734 Byte
Status AC
Exec Time 458 ms
Memory 1280 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 265 ms 640 KB
subtask_01_02.txt AC 1 ms 640 KB
subtask_01_03.txt AC 406 ms 1024 KB
subtask_01_04.txt AC 456 ms 1280 KB
subtask_01_05.txt AC 24 ms 256 KB
subtask_01_06.txt AC 26 ms 640 KB
subtask_01_07.txt AC 423 ms 768 KB
subtask_01_08.txt AC 453 ms 1280 KB
subtask_01_09.txt AC 1 ms 256 KB
subtask_01_10.txt AC 1 ms 640 KB
subtask_01_11.txt AC 407 ms 896 KB
subtask_01_12.txt AC 457 ms 1280 KB
subtask_01_13.txt AC 344 ms 768 KB
subtask_01_14.txt AC 2 ms 640 KB
subtask_01_15.txt AC 416 ms 768 KB
subtask_01_16.txt AC 458 ms 1280 KB
subtask_01_17.txt AC 303 ms 1024 KB
subtask_01_18.txt AC 306 ms 1024 KB