Submission #1178054
Source Code Expand
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <cstddef>
#include <cstring>
#include <cctype>
#include <numeric>
#include <vector>
template <size_t E, bool T=1>
class FastScanner {
char e[E],*t,*a;
const double A=strtod("NaN",nullptr);
FILE*o;
bool i(){size_t n=fread(e,1,E,o);e[n]=0;a=e+n;t=e;return n;}
bool i(ptrdiff_t n){size_t s=fread(e,1,E,o);e[n+=s]=0;a=e+n;t=e;return s;}
public:
FastScanner(FILE*o=stdin):e{},t(e),a(e+E),o(o)
{i();}
template<class O,bool I=T>
bool scan(O&n,typename std::enable_if<!std::is_signed<O>::value>::type*_1=0,typename std::enable_if<I>::type*_2=0){bool s=0;n=0;for(;;++t){char h=*t;if(!h){if(!i())return s;h=*t;}if(isdigit(h)){s=1;n=n*10+h-'0';}else if(s){++t;return 1;}}}
template<class O,bool I=T>
bool scan(O&n,typename std::enable_if<!std::is_signed<O>::value>::type*_1=0,typename std::enable_if<!I>::type*_2=0){bool s=0;n=0;for(;;++t){char h=*t;if(!h){if(!i())return s;h=*t;}if(isdigit(h)){s=1;n=n*10+h-'0';}else if(s){return 1;}}}
template<class O,class...I>
bool scan(O&&n,I&&...s){return scan(n)&&scan(s...);}
template<class O>
O next(){O n;scan(n);return n;}
};
template <size_t E>
class FastPrinter {
char e[E|1],*t,*a;
const char T[6]="%.16f";
const size_t A=20,O=330;
FILE*o;
void i(){fwrite(e,1,t-e,o);t=e;}
public:
FastPrinter(FILE*o):e{},t(e),a(t+E),o(o)
{}
void print(char*n){size_t s=strlen(n);char*h=t+s;if(h>=a){i();h=e+s;}if(s>=E){fwrite(n,1,s,o);return;}memcpy(t,n,s);t=h;}
void print(const char*n){size_t s=strlen(n);char*h=t+s;if(h>=a){i();h=e+s;}if(s>=E){fwrite(n,1,s,o);return;}memcpy(t,n,s);t=h;}
template<class I,class...N>
void print(I n,N&&...s){print(n),print(s...);}
template<class I>
void println(I n){print(n,'\n');}
template<class I,class...N>
void println(I n,N&&...s){print(n,' '),println(s...);}
template<class I>
void printlns(I n){print(n,'\n');}
template<class I,class...N>
void printlns(I n,N&&...s){print(n,'\n'),printlns(s...);}
void flush(){fwrite(e,1,t-e,o);fflush(o);t=e;}
template<class I>
FastPrinter&operator<<(I n){print(n);return*this;}
~FastPrinter(){flush();}
};
constexpr size_t BUF_SIZE=(1<<18);
FastScanner<BUF_SIZE> cin(stdin);
FastPrinter<BUF_SIZE> cout(stdout);
class UnionFindTree {
std::vector<int> tree;
public:
UnionFindTree(size_t n): tree(n, -1) {}
size_t find_root(size_t v) {
if (tree[v] < 0) return v;
return tree[v] = find_root(tree[v]);
}
bool unite(size_t x, size_t y) {
x = find_root(x);
y = find_root(y);
if (x == y) return false;
if (tree[x] > tree[y]) std::swap(x, y);
tree[x] += tree[y];
tree[y] = x;
return true;
}
bool connected(size_t x, size_t y) {
return find_root(x) == find_root(y);
}
size_t rank(size_t v) {
return -tree[find_root(v)];
}
};
int main() {
size_t N;
unsigned Q;
cin.scan(N, Q);
UnionFindTree tree(N);
for (; Q--;) {
unsigned P;
size_t A, B;
cin.scan(P, A, B);
if (P) {
cout.print(tree.connected(A, B)? "Yes\n":"No\n");
} else {
tree.unite(A, B);
}
}
return 0;
}
Submission Info
Submission Time |
|
Task |
B - Union Find |
User |
rsk0315 |
Language |
C++14 (GCC 5.4.1) |
Score |
100 |
Code Size |
3301 Byte |
Status |
AC |
Exec Time |
7 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 |
768 KB |
subtask_01_01.txt |
AC |
4 ms |
1152 KB |
subtask_01_02.txt |
AC |
1 ms |
1152 KB |
subtask_01_03.txt |
AC |
5 ms |
1536 KB |
subtask_01_04.txt |
AC |
7 ms |
1664 KB |
subtask_01_05.txt |
AC |
2 ms |
768 KB |
subtask_01_06.txt |
AC |
2 ms |
1152 KB |
subtask_01_07.txt |
AC |
7 ms |
1280 KB |
subtask_01_08.txt |
AC |
7 ms |
1664 KB |
subtask_01_09.txt |
AC |
1 ms |
768 KB |
subtask_01_10.txt |
AC |
1 ms |
1152 KB |
subtask_01_11.txt |
AC |
5 ms |
1408 KB |
subtask_01_12.txt |
AC |
7 ms |
1664 KB |
subtask_01_13.txt |
AC |
6 ms |
1152 KB |
subtask_01_14.txt |
AC |
2 ms |
1152 KB |
subtask_01_15.txt |
AC |
5 ms |
1280 KB |
subtask_01_16.txt |
AC |
7 ms |
1664 KB |
subtask_01_17.txt |
AC |
7 ms |
1408 KB |
subtask_01_18.txt |
AC |
7 ms |
1408 KB |