Submission #3819643


Source Code Expand

#include<algorithm>
#include<complex>
#include<ctype.h>
#include<iomanip>
#include<iostream>
#include<map>
#include<math.h>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<stdio.h>
#include<string>
#include<string>
#include<vector>

using namespace std;
typedef long long ll;

#define REP(i,n) for(int i=0;i<n;++i)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define pn(s) cout << (#s) << " " << (s) << endl

const ll mod = 1e9 + 7;
const ll inf = 1e18;

void pV(vector<ll> A){
    cout << "[vector]";
    for(int i=0; i<A.size(); i++){
        cout << A[i] << " ";
    }
    cout << endl;
}

// ref
// https://www.slideshare.net/iwiwi/ss-3578491

const int N_MAX = 100010;
int parent[N_MAX];

int findRoot(int x){
    if(parent[x] == x){
        return x;
    }else{
        auto root = findRoot(parent[x]);
        parent[x] = root; // path compression
        return root;
    }
}

// union is reserved word...
void _union(int a, int b){
    auto rootA = findRoot(a);
    auto rootB = findRoot(b);

    if(rootA == rootB){
        return;
    }else{
        parent[a] = b;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    // input
    int N, Q;
    cin >> N >> Q;

    // init
    FOR(i, 0, N_MAX){
        parent[i] = i;
    }

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

        // union
        if(p==0){
            _union(a, b);
        }
        // find
        else{
            // a & b is same group?
            bool isSameGroup;
            if(findRoot(a) == findRoot(b)){
                isSameGroup = true;
                p("Yes");
            }else{
                isSameGroup = false;
                p("No");
            }
        }
    }
    
    return 0;
}

Submission Info

Submission Time
Task B - Union Find
User peroon
Language C++14 (GCC 5.4.1)
Score 100
Code Size 1873 Byte
Status AC
Exec Time 348 ms
Memory 1408 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 640 KB
subtask_01_01.txt AC 212 ms 1024 KB
subtask_01_02.txt AC 1 ms 640 KB
subtask_01_03.txt AC 338 ms 1408 KB
subtask_01_04.txt AC 347 ms 1280 KB
subtask_01_05.txt AC 19 ms 640 KB
subtask_01_06.txt AC 18 ms 640 KB
subtask_01_07.txt AC 343 ms 1280 KB
subtask_01_08.txt AC 345 ms 1280 KB
subtask_01_09.txt AC 1 ms 640 KB
subtask_01_10.txt AC 1 ms 640 KB
subtask_01_11.txt AC 348 ms 1280 KB
subtask_01_12.txt AC 342 ms 1280 KB
subtask_01_13.txt AC 272 ms 1152 KB
subtask_01_14.txt AC 2 ms 640 KB
subtask_01_15.txt AC 334 ms 1280 KB
subtask_01_16.txt AC 341 ms 1280 KB
subtask_01_17.txt AC 203 ms 1024 KB
subtask_01_18.txt AC 197 ms 1024 KB