Submission #1680939


Source Code Expand

#include "bits/stdc++.h"
using namespace std;

struct QuickFind {
        int n;
        vector<int> group;
        vector<vector<int>> item;
        QuickFind(int n_) {
                n = n_;
                group.resize(n_);
                item.resize(n_);
                for (int i = 0; i < n_; i ++) {
                        group[i] = i;
                        item[i].assign(1, i);
                }
        }
        bool same(int x, int y) { return group[x] == group[y]; }
        void merge(int x, int y) {
                if (same(x, y)) return;
                if (item[group[x]].size() < item[group[y]].size()) swap(x, y);
                int gx = group[x];
                int gy = group[y];
                for (int i : item[gy]) group[i] = gx;
                item[gx].insert(item[gx].end(), item[gy].begin(), item[gy].end());
                item[gy].clear();
                n --;
        }
        int get() { return n; }
        int get(int x) { return item[group[x]].size(); }
};

int main() {
        int n, q;
        scanf("%d%d", &n, &q);
        QuickFind qf(n);
        while (q --) {
                int p, a, b;
                scanf("%d%d%d", &p, &a, &b);
                if (p) puts((qf.same(a, b) ? "Yes" : "No"));
                else qf.merge(a, b);
        }
        return 0;
}

Submission Info

Submission Time
Task B - Union Find
User KokiYmgch
Language C++14 (GCC 5.4.1)
Score 100
Code Size 1363 Byte
Status AC
Exec Time 57 ms
Memory 6908 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:34:30: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &n, &q);
                              ^
./Main.cpp:38:44: 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 256 KB
subtask_01_01.txt AC 29 ms 1152 KB
subtask_01_02.txt AC 8 ms 6144 KB
subtask_01_03.txt AC 40 ms 1024 KB
subtask_01_04.txt AC 56 ms 6656 KB
subtask_01_05.txt AC 4 ms 256 KB
subtask_01_06.txt AC 12 ms 6144 KB
subtask_01_07.txt AC 45 ms 896 KB
subtask_01_08.txt AC 56 ms 6656 KB
subtask_01_09.txt AC 1 ms 256 KB
subtask_01_10.txt AC 8 ms 6144 KB
subtask_01_11.txt AC 41 ms 896 KB
subtask_01_12.txt AC 56 ms 6656 KB
subtask_01_13.txt AC 37 ms 896 KB
subtask_01_14.txt AC 8 ms 6144 KB
subtask_01_15.txt AC 42 ms 896 KB
subtask_01_16.txt AC 56 ms 6656 KB
subtask_01_17.txt AC 57 ms 6908 KB
subtask_01_18.txt AC 57 ms 6908 KB