Submission #420174


Source Code Expand

#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii; typedef long long ll;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }

struct UnionFind {
	vector<int> data;
	void init(int n) { data.assign(n, -1); }
	bool unionSet(int x, int y) {
		x = root(x); y = root(y);
		if(x != y) {
			if(data[y] < data[x]) swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
		return x != y;
	}
	bool findSet(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;
	while(~scanf("%d%d", &N,&Q)) {
		UnionFind uf; uf.init(N);
		rep(i, Q) {
			int P, A, B;
			scanf("%d%d%d", &P, &A, &B);
			if(P == 0) {
				uf.unionSet(A, B);
			}else {
				bool ans = uf.findSet(A, B);
				puts(ans ? "Yes" : "No");
			}
		}
	}
	return 0;
}

Submission Info

Submission Time
Task B - Union Find
User anta
Language C++ (GCC 4.9.2)
Score 100
Code Size 1954 Byte
Status AC
Exec Time 149 ms
Memory 1316 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:60:31: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d%d%d", &P, &A, &B);
                               ^

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 808 KB
subtask_01_01.txt AC 86 ms 808 KB
subtask_01_02.txt AC 24 ms 1308 KB
subtask_01_03.txt AC 149 ms 804 KB
subtask_01_04.txt AC 130 ms 1184 KB
subtask_01_05.txt AC 31 ms 804 KB
subtask_01_06.txt AC 33 ms 1188 KB
subtask_01_07.txt AC 130 ms 804 KB
subtask_01_08.txt AC 145 ms 1316 KB
subtask_01_09.txt AC 24 ms 920 KB
subtask_01_10.txt AC 26 ms 1176 KB
subtask_01_11.txt AC 136 ms 916 KB
subtask_01_12.txt AC 128 ms 1180 KB
subtask_01_13.txt AC 106 ms 800 KB
subtask_01_14.txt AC 26 ms 1308 KB
subtask_01_15.txt AC 133 ms 808 KB
subtask_01_16.txt AC 138 ms 1316 KB
subtask_01_17.txt AC 110 ms 1316 KB
subtask_01_18.txt AC 110 ms 1304 KB