Submission #420247


Source Code Expand

// tsukasa_diary's programing contest code template
#include <bits/stdc++.h>
using namespace std;
// define
#define for_(i,a,b) for(int i=(a);i<(b);++i)
#define for_rev(i,a,b) for(int i=(a);i>=(b);--i)
#define allof(a) (a).begin(),(a).end()
#define minit(a,b) memset(a,b,sizeof(a))
#define size_of(a) int((a).size())
// typedef
typedef long long lint;
typedef double Double;
typedef pair< int, int > pii;
template< typename T >
using Matrix = vector< vector< T > >;
// popcount
inline int POPCNT(int x) { return __builtin_popcount(x); }
inline int POPCNT(lint x) { return __builtin_popcount(x); }
// inf
const int iINF = 1L << 30;
const lint lINF = 1LL << 60;
// eps
const Double EPS = 1e-9;
const Double PI = acos(-1);
// inrange
template< typename T >
inline bool in_range(T v, T mi, T mx) { return mi <= v && v < mx; }
template< typename T >
inline bool in_range(T x, T y, T W, T H) { return in_range(x,0,W) && in_range(y,0,H); }
// neighbor clockwise
const int DX[4] = {0,1,0,-1}, DY[4] = {-1,0,1,0};
const int DX_[8] = {0,1,1,1,0,-1,-1,-1}, DY_[8] = {-1,-1,0,1,1,1,0,-1};
// variable update
template< typename T > inline void modAdd(T& a, T b, T mod) { a = (a + b) % mod; }
template< typename T > inline void minUpdate(T& a, T b) { a = min(a, b); }
template< typename T > inline void maxUpdate(T& a, T b) { a = max(a, b); }
// converter
template< typename F, typename T >
inline void convert(F& from, T& to) {
	stringstream ss;
	ss << from; ss >> to;
}

class UnionFind {
private:
	vector< int > data;
	int group_nums;
	
public:
	UnionFind(int n) : data(n, -1), group_nums(n) {}
	
	bool unionSet(int x, int y) {
		x = root(x); y = root(y);
		
		if (x == y) return 0;
		
		if (data[y] < data[x]) swap(x, y);
		data[x] += data[y]; data[y] = x;
		--group_nums;
		
		return 1;
	}
	
	bool sameSet(int x, int y) {
		return root(x) == root(y);
	}
	
	int root(int x) {
		return data[x] < 0 ? x : data[x] = root(data[x]);
	}
	
	int size(int x) {
		return -data[root(x)];
	}
};

int main() {
	int N, Q;
	cin >> N >> Q;
	
	UnionFind uf(N);
	
	for_(i,0,Q) {
		int p, a, b;
		cin >> p >> a >> b;
		
		if (p) {
			cout << (uf.sameSet(a, b) ? "Yes" : "No") << endl;
		} else {
			uf.unionSet(a, b);
		}
	}
}

Submission Info

Submission Time
Task B - Union Find
User tsukasa_diary
Language C++11 (GCC 4.9.2)
Score 100
Code Size 2289 Byte
Status AC
Exec Time 972 ms
Memory 1308 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 24 ms 804 KB
subtask_01_01.txt AC 497 ms 924 KB
subtask_01_02.txt AC 27 ms 1192 KB
subtask_01_03.txt AC 735 ms 920 KB
subtask_01_04.txt AC 841 ms 1184 KB
subtask_01_05.txt AC 65 ms 932 KB
subtask_01_06.txt AC 72 ms 1188 KB
subtask_01_07.txt AC 753 ms 800 KB
subtask_01_08.txt AC 972 ms 1192 KB
subtask_01_09.txt AC 26 ms 796 KB
subtask_01_10.txt AC 26 ms 1116 KB
subtask_01_11.txt AC 868 ms 800 KB
subtask_01_12.txt AC 842 ms 1308 KB
subtask_01_13.txt AC 687 ms 796 KB
subtask_01_14.txt AC 29 ms 1120 KB
subtask_01_15.txt AC 821 ms 796 KB
subtask_01_16.txt AC 906 ms 1308 KB
subtask_01_17.txt AC 621 ms 1188 KB
subtask_01_18.txt AC 637 ms 1304 KB