Submission #1021497


Source Code Expand

#include <iostream>
#include <vector>
#include <string>
using namespace std;
class DisjointSet {
public:
	vector<int> rank,p;
	DisjointSet() {}
	DisjointSet(int size) {
		rank.resize(size, 0);
		p.resize(size, 0);
		for (int i = 0; i < size; ++i) {
			makeSet(i);
		}
	}

	void makeSet(int x) {
		p[x] = x;
		rank[x] = 0;
	}

	void link(int x, int y) {
		if (rank[x] > rank[y]) {
			p[y] = x;
		}
		else {
			p[x] = y;
			if (rank[x] == rank[y]) {
				++rank[x];
			}
		}
	}

	bool same(int x, int y) {
		return findSet(x) == findSet(y);
	}

	void unite(int x, int y) {
		link(findSet(x), findSet(y));
	}

	int findSet(int x) {
		if (p[x] != x) {
			p[x] = findSet(p[x]);
		}
		return p[x];
	}
};

int main() {
	int n, q, com, a, b;
	cin >> n >> q;
	DisjointSet* ds = new DisjointSet(n);
	for (int i = 0; i < q; ++i) {
		cin >> com >> a >> b;
		if (com == 0) {
			ds->unite(a, b);
		}
		else {
			if (ds->same(a, b)) {
				cout << "Yes" << endl;
			}
			else {
				cout << "No" << endl;
			}
		}
	}
	return 0;
}

Submission Info

Submission Time
Task B - Union Find
User iroha168
Language C++ (GCC 4.9.2)
Score 100
Code Size 1079 Byte
Status AC
Exec Time 835 ms
Memory 1692 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 19 ms 892 KB
subtask_01_01.txt AC 499 ms 928 KB
subtask_01_02.txt AC 18 ms 1568 KB
subtask_01_03.txt AC 665 ms 920 KB
subtask_01_04.txt AC 829 ms 1568 KB
subtask_01_05.txt AC 62 ms 800 KB
subtask_01_06.txt AC 62 ms 1692 KB
subtask_01_07.txt AC 748 ms 928 KB
subtask_01_08.txt AC 832 ms 1608 KB
subtask_01_09.txt AC 19 ms 796 KB
subtask_01_10.txt AC 18 ms 1568 KB
subtask_01_11.txt AC 638 ms 916 KB
subtask_01_12.txt AC 810 ms 1692 KB
subtask_01_13.txt AC 622 ms 924 KB
subtask_01_14.txt AC 20 ms 1568 KB
subtask_01_15.txt AC 685 ms 928 KB
subtask_01_16.txt AC 835 ms 1692 KB
subtask_01_17.txt AC 602 ms 1692 KB
subtask_01_18.txt AC 602 ms 1568 KB