Submission #7129503


Source Code Expand

#include <iostream>
#include <stdio.h>
#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() {puts("Yes");}
void sayNo() {puts("No");}


class UnionFind {
    vector<int> parent;

    int rank(int v) {
        // 根でしか呼び出していないのでこの実装
        // 根以外でも呼び出す場合は、-parent[getRoot(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() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N, Q; scanf("%d", &N); scanf("%d", &Q);
    UnionFind unf(N);

    int p, a, b;
    REP(i, Q) {
        scanf("%d", &p); scanf("%d", &a); scanf("%d", &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 1985 Byte
Status AC
Exec Time 58 ms
Memory 1280 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:66:30: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     int N, Q; scanf("%d", &N); scanf("%d", &Q);
                              ^
./Main.cpp:66:47: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     int N, Q; scanf("%d", &N); scanf("%d", &Q);
                                               ^
./Main.cpp:71:24: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &p); scanf("%d", &a); scanf("%d", &b);
                        ^
./Main.cpp:71:41: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &p); scanf("%d", &a); scanf("%d", &b);
                                         ^
./Main.cpp:71:58: warning: ignoring re...

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 34 ms 640 KB
subtask_01_02.txt AC 1 ms 640 KB
subtask_01_03.txt AC 50 ms 1024 KB
subtask_01_04.txt AC 58 ms 1280 KB
subtask_01_05.txt AC 5 ms 256 KB
subtask_01_06.txt AC 5 ms 640 KB
subtask_01_07.txt AC 54 ms 896 KB
subtask_01_08.txt AC 58 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 50 ms 896 KB
subtask_01_12.txt AC 58 ms 1280 KB
subtask_01_13.txt AC 44 ms 768 KB
subtask_01_14.txt AC 1 ms 640 KB
subtask_01_15.txt AC 53 ms 896 KB
subtask_01_16.txt AC 58 ms 1280 KB
subtask_01_17.txt AC 57 ms 1024 KB
subtask_01_18.txt AC 57 ms 1024 KB