Submission #420216
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
#define repu(i, a, b) for (int i = (a); i < (b); ++i)
#define repd(i, a, b) for (int i = (a); i > (b); --i)
#define mem(a, x) memset(a, x, sizeof(a))
#define all(a) a.begin(), a.end()
#define uni(a) a.erase(unique(all(a)), a.end())
#define count_bits(x) __builtin_popcount(x)
#define count_bitsll(x) __builtin_popcountll(x)
#define least_bits(x) __builtin_ffs(x)
#define least_bitsll(x) __builtin_ffsll(x)
#define most_bits(x) 32 - __builtin_clz(x)
#define most_bitsll(x) 64 - __builtin_clz(x)
vector<string> split(const string &s, char c) {
vector<string> v;
stringstream ss(s);
string x;
while (getline(ss, x, c)) v.push_back(x);
return v;
}
#define error(args...) { vector<string> _v = split(#args, ','); err(_v.begin(), args); }
void err(vector<string>::iterator it) {}
template<typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
cerr << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << '\n';
err(++it, args...);
}
typedef long long ll;
const int MOD = 1000000007;
template<class T> inline T tmin(T a, T b) {return (a < b) ? a : b;}
template<class T> inline T tmax(T a, T b) {return (a > b) ? a : b;}
template<class T> inline void amax(T &a, T b) {if (b > a) a = b;}
template<class T> inline void amin(T &a, T b) {if (b < a) a = b;}
template<class T> inline T tabs(T a) {return (a > 0) ? a : -a;}
template<class T> T gcd(T a, T b) {while (b != 0) {T c = a; a = b; b = c % b;} return a;}
struct UF {
vector<int> par, rk;
int sz;
UF(int _n) {
sz = _n;
par.resize(sz + 1), rk.assign(sz + 1, 0);
for (int i = 0; i <= sz; ++i) {
par[i] = i;
}
}
int find(int x) {
if (x != par[x]) par[x] = find(par[x]);
return par[x];
}
bool same(int x, int y) {
return find(x) == find(y);
}
void unite(int x, int y) {
x = find(x), y = find(y);
if (x == y) return;
if (rk[x] < rk[y]) par[x] = y;
else {
par[y] = x;
if (rk[x] == rk[y]) rk[x]++;
}
}
};
int main(int argc, char *argv[]) {
ios_base::sync_with_stdio(false);
int n, q, a, b, p;
cin >> n >> q;
UF uf(n + 1);
repu(i, 0, q) {
cin >> p >> a >> b;
if (p == 0) uf.unite(a, b);
else printf("%s\n", uf.same(a, b) ? "Yes" : "No");
}
return 0;
}
Submission Info
Submission Time
2015-06-06 21:06:14+0900
Task
B - Union Find
User
rantd
Language
C++ (GCC 4.9.2)
Score
100
Code Size
2500 Byte
Status
AC
Exec Time
140 ms
Memory
1696 KB
Compile Error
./Main.cpp:28:30: warning: variadic templates only available with -std=c++11 or -std=gnu++11
template<typename T, typename... Args>
^
./Main.cpp:29:52: warning: variadic templates only available with -std=c++11 or -std=gnu++11
void err(vector<string>::iterator it, T a, Args... args) {
^
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
23 ms
928 KB
subtask_01_01.txt
AC
85 ms
928 KB
subtask_01_02.txt
AC
27 ms
1568 KB
subtask_01_03.txt
AC
130 ms
800 KB
subtask_01_04.txt
AC
133 ms
1568 KB
subtask_01_05.txt
AC
34 ms
932 KB
subtask_01_06.txt
AC
34 ms
1516 KB
subtask_01_07.txt
AC
135 ms
924 KB
subtask_01_08.txt
AC
138 ms
1692 KB
subtask_01_09.txt
AC
23 ms
800 KB
subtask_01_10.txt
AC
25 ms
1568 KB
subtask_01_11.txt
AC
130 ms
924 KB
subtask_01_12.txt
AC
140 ms
1572 KB
subtask_01_13.txt
AC
109 ms
924 KB
subtask_01_14.txt
AC
25 ms
1696 KB
subtask_01_15.txt
AC
140 ms
796 KB
subtask_01_16.txt
AC
127 ms
1564 KB
subtask_01_17.txt
AC
117 ms
1696 KB
subtask_01_18.txt
AC
115 ms
1696 KB