Submission #4357635


Source Code Expand

#include <cstdio>

#define MAX_N 100000

struct uftree {
    int par[MAX_N], rank[MAX_N];
    uftree(int N) {
        for (int i=0; i<N; i++) {
            par[i] = i;
            rank[i] = 0;
        }
    }
    int root(int x) {
        return par[x] == x ? x : par[x] = root(par[x]);
    }
    void unite(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return;
        if (rank[x] > rank[y]) par[y] = x;
        else {
            par[x] = y;
            if (rank[x] == rank[y]) rank[y]++;
        }
    }
    bool same(int x, int y) {
        return root(x) == root(y);
    }
};

int main() {
    int N, Q, P, A, B;
    scanf("%d %d", &N, &Q);
    uftree T(N);
    for (int i=0; i<Q; i++) {
        scanf("%d %d %d", &P, &A, &B);
        if (P) printf("%s\n", T.same(A, B) ? "Yes" : "No");
        else T.unite(A, B);
    }
    return 0;
}

Submission Info

Submission Time
Task B - Union Find
User shugo256
Language C++14 (GCC 5.4.1)
Score 100
Code Size 913 Byte
Status AC
Exec Time 50 ms
Memory 1536 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:33:27: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &N, &Q);
                           ^
./Main.cpp:36:38: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %d", &P, &A, &B);
                                      ^

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 128 KB
subtask_01_01.txt AC 29 ms 640 KB
subtask_01_02.txt AC 1 ms 896 KB
subtask_01_03.txt AC 40 ms 896 KB
subtask_01_04.txt AC 49 ms 1536 KB
subtask_01_05.txt AC 4 ms 256 KB
subtask_01_06.txt AC 4 ms 896 KB
subtask_01_07.txt AC 45 ms 768 KB
subtask_01_08.txt AC 49 ms 1536 KB
subtask_01_09.txt AC 1 ms 128 KB
subtask_01_10.txt AC 1 ms 896 KB
subtask_01_11.txt AC 41 ms 768 KB
subtask_01_12.txt AC 49 ms 1536 KB
subtask_01_13.txt AC 37 ms 640 KB
subtask_01_14.txt AC 1 ms 896 KB
subtask_01_15.txt AC 42 ms 768 KB
subtask_01_16.txt AC 50 ms 1536 KB
subtask_01_17.txt AC 49 ms 1280 KB
subtask_01_18.txt AC 48 ms 1280 KB