Submission #11229037


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000;
int par[MAX_N];
int rank_of_tree[MAX_N] = {0};

void init(int n)
{

    for (int i = 0; i < n; i++)
    {
        par[i] = i;
        rank_of_tree[i] = 0;
    }
}

int find(int x)
{
    if (par[x] == x)
    {
        return x;
    }
    else
    {
        return par[x] = find(par[x]);
    }
}

void unite(int x, int y)
{
    x = find(x);
    y = find(y);
    if (x == y)
        return;

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

bool same(int x, int y)
{
    return find(x) == find(y);
}

int main()
{
    int n, q;
    cin >> n >> q;
    init(n);
    for (int i = 0; i < q; i++)
    {
        int p, a, b;
        cin >> p >> a >> b;
        if (p == 0)
            unite(a, b);
        if (p == 1)
            cout << (same(a, b) ? "Yes" : "No") << endl;
    }
}

Submission Info

Submission Time
Task B - Union Find
User chicken1925
Language C++14 (GCC 5.4.1)
Score 100
Code Size 1060 Byte
Status AC
Exec Time 499 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 2 ms 1024 KB
subtask_01_03.txt AC 431 ms 1024 KB
subtask_01_04.txt AC 492 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 455 ms 896 KB
subtask_01_08.txt AC 496 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 448 ms 896 KB
subtask_01_12.txt AC 499 ms 1664 KB
subtask_01_13.txt AC 379 ms 768 KB
subtask_01_14.txt AC 3 ms 1024 KB
subtask_01_15.txt AC 451 ms 896 KB
subtask_01_16.txt AC 493 ms 1664 KB
subtask_01_17.txt AC 336 ms 1408 KB
subtask_01_18.txt AC 344 ms 1408 KB