Submission #7125573


Source Code Expand

Copy
#include <iostream>
#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() {cout << "Yes" << endl;}
void sayNo() {cout << "No" << endl;}


int getRoot(vector<int> &lines, int v) {
    if (lines[v] == v) {
        return v;
    }
    else {
        return lines[v] = getRoot(lines, lines[v]);
    }
}


int main() {
    int N, Q; cin >> N >> Q;
    vector<int> lines(N);
    REP(i, N) lines[i] = i;

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

        // conbine
        if (p == 0) {
            lines[max(a, b)] = getRoot(lines, min(a, b));
        }
        // judge
        if (p == 1) {
            if (getRoot(lines, a) == getRoot(lines, b)) sayYes();
            else sayNo();
            // cout << lines[a] << " " << lines[b] << endl;
        }
    }
}

Submission Info

Submission Time
Task B - Union Find
User scientistb
Language C++14 (GCC 5.4.1)
Score 0
Code Size 1312 Byte
Status
Exec Time 486 ms
Memory 1280 KB

Judge Result

Set Name Score / Max Score Test Cases
Sample 0 / 0 00_sample_01.txt
All 0 / 100 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 1 ms 256 KB
subtask_01_01.txt 277 ms 640 KB
subtask_01_02.txt 2 ms 640 KB
subtask_01_03.txt 427 ms 1024 KB
subtask_01_04.txt 483 ms 1280 KB
subtask_01_05.txt 25 ms 256 KB
subtask_01_06.txt 26 ms 640 KB
subtask_01_07.txt 439 ms 768 KB
subtask_01_08.txt 469 ms 1280 KB
subtask_01_09.txt 1 ms 256 KB
subtask_01_10.txt 2 ms 640 KB
subtask_01_11.txt 436 ms 896 KB
subtask_01_12.txt 486 ms 1280 KB
subtask_01_13.txt 364 ms 768 KB
subtask_01_14.txt 3 ms 640 KB
subtask_01_15.txt 437 ms 896 KB
subtask_01_16.txt 485 ms 1280 KB
subtask_01_17.txt 322 ms 896 KB
subtask_01_18.txt 320 ms 1024 KB