Submission #3049404
Source Code Expand
#include <iostream> #include <string> #include <vector> #include <cstdlib> #include <cstdio> #include <cmath> #include <algorithm> #include <map> #include <stack> #include <queue> #include <set> #include <cstring> using namespace std; // ascending order #define vsort(v) sort(v.begin(), v.end()) // descending order #define vsort_r(v) sort(v.begin(), v.end(), greater<int>()) #define vunique(v) unique(v.begin(), v.end()) #define mp make_pair #define ts(x) to_string(x) #define rep(i, a, b) for(int i = (int)a; i < (int)b; i++) #define repm(i, a, b) for(int i = (int)a; i > (int)b; i--) #define bit(a) bitset<8>(a) #define des_priority_queue priority_queue<int, vector<int>, greater<int> > typedef long long ll; typedef pair<int, int> P; const ll INF = 1e18; #define MAX_N 100001 int par[MAX_N]; // 親 int depth[MAX_N]; // 木の深さ // n要素で初期化 void init(int n) { rep(i, 0, n) { par[i] = i; depth[i] = 0; } } // 木の根を求める int find(int x) { if(par[x] == x) return x; else return par[x] = find(par[x]); } // xとyの属する集合を併合 void unite(int x, int y) { x = find(x); y = find(y); if(x == y) return; if(depth[x] < depth[y]) { par[x] = y; } else { par[y] = x; if(depth[x] == depth[y]) depth[x]++; } } // xとyが同じ集合に属するか否か bool same(int x, int y) { return find(x) == find(y); } int main(){ cin.tie(0); ios::sync_with_stdio(false); int N, Q; cin >> N >> Q; init(N); rep(q, 0, Q) { int p, a, b; cin >> p >> a >> b; if(p == 0) unite(a, b); else { if(same(a, b)) cout << "Yes" << endl; else cout << "No" << endl; } } }
Submission Info
Submission Time | |
---|---|
Task | B - Union Find |
User | tsutarou10 |
Language | C++14 (GCC 5.4.1) |
Score | 100 |
Code Size | 1715 Byte |
Status | AC |
Exec Time | 353 ms |
Memory | 1664 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 100 / 100 | ||||
Status |
|
|
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 | 221 ms | 640 KB |
subtask_01_02.txt | AC | 2 ms | 1024 KB |
subtask_01_03.txt | AC | 338 ms | 1024 KB |
subtask_01_04.txt | AC | 346 ms | 1664 KB |
subtask_01_05.txt | AC | 20 ms | 256 KB |
subtask_01_06.txt | AC | 19 ms | 1024 KB |
subtask_01_07.txt | AC | 338 ms | 896 KB |
subtask_01_08.txt | AC | 341 ms | 1664 KB |
subtask_01_09.txt | AC | 1 ms | 256 KB |
subtask_01_10.txt | AC | 2 ms | 1024 KB |
subtask_01_11.txt | AC | 337 ms | 896 KB |
subtask_01_12.txt | AC | 343 ms | 1664 KB |
subtask_01_13.txt | AC | 270 ms | 768 KB |
subtask_01_14.txt | AC | 2 ms | 1024 KB |
subtask_01_15.txt | AC | 343 ms | 896 KB |
subtask_01_16.txt | AC | 353 ms | 1664 KB |
subtask_01_17.txt | AC | 197 ms | 1408 KB |
subtask_01_18.txt | AC | 201 ms | 1408 KB |