Submission #7129294


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 0
Code Size 1731 Byte
Status WA
Exec Time 839 ms
Memory 4736 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 100
Status
WA × 1
WA × 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 WA 1 ms 256 KB
subtask_01_01.txt WA 501 ms 2560 KB
subtask_01_02.txt WA 1 ms 640 KB
subtask_01_03.txt WA 784 ms 3072 KB
subtask_01_04.txt WA 824 ms 4736 KB
subtask_01_05.txt WA 43 ms 384 KB
subtask_01_06.txt WA 42 ms 768 KB
subtask_01_07.txt WA 805 ms 3328 KB
subtask_01_08.txt WA 839 ms 4736 KB
subtask_01_09.txt WA 1 ms 256 KB
subtask_01_10.txt WA 1 ms 640 KB
subtask_01_11.txt WA 787 ms 2816 KB
subtask_01_12.txt WA 826 ms 4736 KB
subtask_01_13.txt WA 649 ms 3072 KB
subtask_01_14.txt WA 3 ms 640 KB
subtask_01_15.txt WA 794 ms 3072 KB
subtask_01_16.txt WA 826 ms 4736 KB
subtask_01_17.txt WA 496 ms 2560 KB
subtask_01_18.txt WA 503 ms 2944 KB