Submission #420176


Source Code Expand

Copy
#define _CRT_SECURE_NO_WARNINGS
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//#define int ll
//#define endl "\n"
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i,a,b) for(ll i=a; i<ll(b); i++)
#define rep(i,b) loop(i,0,b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template<class T> ostream & operator<<(ostream & os, vector<T> const &);
template<int n, class...T> typename enable_if<(n>=sizeof...(T))>::type _ot(ostream &, tuple<T...> const &){}
template<int n, class...T> typename enable_if<(n< sizeof...(T))>::type _ot(ostream & os, tuple<T...> const & t){ os << (n==0?"":" ") << get<n>(t); _ot<n+1>(os, t); }
template<class...T> ostream & operator<<(ostream & os, tuple<T...> const & t){ _ot<0>(os, t); return os; }
template<class T, class U> ostream & operator<<(ostream & os, pair<T,U> const & p){ return os << "(" << p.first << ", " << p.second << ") "; }
template<class T> ostream & operator<<(ostream & os, vector<T> const & v){ rep(i,v.size()) os << v[i] << (i+1==(int)v.size()?"":" "); return os; }
template<class T> inline bool chmax(T & x, T const & y){ return x<y ? x=y,true : false; }
template<class T> inline bool chmin(T & x, T const & y){ return x>y ? x=y,true : false; }
#ifdef DEBUG
#define dump(...) (cerr<<#__VA_ARGS__<<" = "<<mt(__VA_ARGS__)<<" ["<<__LINE__<<"]"<<endl)
#else
#define dump(...)
#endif
// ll const mod = 1000000007;
// ll const inf = 1LL<<60;

struct UnionFind {
    vector<int> par;
    int cnt;
    UnionFind(int size_) : par(size_, -1), cnt(size_) { }
    bool set(int x, int y) {
        x = find(x); y = find(y);
        if (x != y) {
            if (par[y] < par[x]) swap(x, y);
            par[x] += par[y]; par[y] = x;
            cnt--;
        }
        return x != y;
    }
    bool get(int x, int y) {
        return find(x) == find(y);
    }
    int find(int x) {
        return par[x] < 0 ? x : par[x] = find(par[x]);
    }
    int size(int x) {
        return -par[find(x)];
    }
    int size() {
        return cnt;
    }
};


signed main(){
    int N,Q;
    cin >> N >> Q;
    UnionFind t(N);
    rep(i,Q){
        int p,a,b;
        cin >> p >> a >> b;
        if(p){
            puts(t.get(a,b) ? "Yes" : "No");
        } else {
            t.set(a,b);
        }
    }
}

Submission Info

Submission Time
Task B - Union Find
User tubo28
Language C++11 (GCC 4.9.2)
Score 100
Code Size 2477 Byte
Status
Exec Time 837 ms
Memory 1312 KB

Judge Result

Set Name Score / Max Score Test Cases
Sample 0 / 0 00_sample_01.txt
All 100 / 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 25 ms 928 KB
subtask_01_01.txt 479 ms 736 KB
subtask_01_02.txt 25 ms 1312 KB
subtask_01_03.txt 618 ms 800 KB
subtask_01_04.txt 814 ms 1304 KB
subtask_01_05.txt 67 ms 932 KB
subtask_01_06.txt 71 ms 1304 KB
subtask_01_07.txt 744 ms 804 KB
subtask_01_08.txt 837 ms 1104 KB
subtask_01_09.txt 25 ms 764 KB
subtask_01_10.txt 26 ms 1308 KB
subtask_01_11.txt 646 ms 800 KB
subtask_01_12.txt 816 ms 1188 KB
subtask_01_13.txt 636 ms 796 KB
subtask_01_14.txt 32 ms 1304 KB
subtask_01_15.txt 722 ms 928 KB
subtask_01_16.txt 816 ms 1308 KB
subtask_01_17.txt 612 ms 1308 KB
subtask_01_18.txt 605 ms 1184 KB