-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7511.cpp
More file actions
63 lines (54 loc) · 1.17 KB
/
7511.cpp
File metadata and controls
63 lines (54 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
int arr[1000001];
int find(int n) {
if (arr[n] < 0)return n;
else return arr[n] = find(arr[n]);
}
void merge(int a, int b) {
a = find(a);
b = find(b);
if (a == b)return;
if (arr[a] < arr[b]) {
arr[a] += arr[b];
arr[b] = a;
}
else {
arr[b] += arr[a];
arr[a] = b;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int T;
cin >> T;
int count = 1;
while (T--) {
cout << "Scenario " << count << ":\n";
int N, K;
fill(arr, arr + 1000001, -1);
cin >> N >> K;
for (int i = 0; i < K; i++) {
int a, b;
cin >> a >> b;
merge(a, b);
}
int m;
cin >> m;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
u = find(u);
v = find(v);
if (u == v)cout << 1 << '\n';
else cout << 0 << '\n';
}
count++;
cout << "\n";
}
}