Submission #420238


Source Code Expand

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

struct union_find {
	vector<int> v;
	union_find(int n) : v(n, -1) {}
	int find(int x) { return (v[x] < 0) ? x : (v[x] = find(v[x])); }
	void unite(int x, int y) {
		x = find(x); y = find(y);
		if (x != y) {
			if (-v[x] < -v[y]) swap(x, y);
			v[x] += v[y]; v[y] = x;
		}
	}
	bool same(int x, int y) { return find(x) == find(y); }
	int size(int x) { return -v[find(x)]; }
};

int main() {
	int N, Q; cin >> N >> Q;
	union_find uf(N);
	while (Q--) {
		int P, A, B; cin >> P >> A >> B;
		if (P == 0) uf.unite(A, B);
		else cout << (uf.same(A, B) ? "Yes" : "No") << endl;
	}
}

Submission Info

Submission Time
Task B - Union Find
User sugim48
Language C++11 (GCC 4.9.2)
Score 100
Code Size 1268 Byte
Status AC
Exec Time 873 ms
Memory 1312 KB

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 25 ms 800 KB
subtask_01_01.txt AC 502 ms 800 KB
subtask_01_02.txt AC 25 ms 1308 KB
subtask_01_03.txt AC 709 ms 804 KB
subtask_01_04.txt AC 839 ms 1312 KB
subtask_01_05.txt AC 70 ms 928 KB
subtask_01_06.txt AC 74 ms 1308 KB
subtask_01_07.txt AC 757 ms 792 KB
subtask_01_08.txt AC 861 ms 1180 KB
subtask_01_09.txt AC 25 ms 800 KB
subtask_01_10.txt AC 25 ms 1188 KB
subtask_01_11.txt AC 701 ms 796 KB
subtask_01_12.txt AC 873 ms 1228 KB
subtask_01_13.txt AC 643 ms 804 KB
subtask_01_14.txt AC 27 ms 1312 KB
subtask_01_15.txt AC 721 ms 924 KB
subtask_01_16.txt AC 837 ms 1312 KB
subtask_01_17.txt AC 615 ms 1308 KB
subtask_01_18.txt AC 622 ms 1124 KB