Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- int t;
- cin >> t;
- for(int _ = 0; _ < t; _++) {
- int n, m, q;
- cin >> n >> m >> q;
- set<int> s;
- while(m--) {
- int x;
- cin >> x;
- s.insert(x);
- }
- vector<vector<int>> adj(n * n);
- int fl = 0, xr = 0, cnt = 0, id = 0;
- for(int i = 0; i < 2 * (n * n - n); i++) {
- if(s.find(i) == s.end()) {
- cnt++;
- id++;
- if(!xr and cnt == n - 1) {
- xr ^= 1;
- cnt = 0;
- id -= n - 1;
- } else if(xr and cnt == n) {
- xr ^= 1;
- cnt = 0;
- fl++;
- }
- continue;
- }
- if(!xr) {
- adj[id].push_back(id + 1);
- adj[id + 1].push_back(id);
- cnt++;
- id++;
- if(!xr and cnt == n - 1) {
- xr ^= 1;
- cnt = 0;
- id -= n - 1;
- } else if(xr and cnt == n) {
- xr ^= 1;
- cnt = 0;
- fl++;
- }
- } else {
- adj[id].push_back(id + n);
- adj[id + n].push_back(id);
- cnt++;
- id++;
- if(!xr and cnt == n - 1) {
- xr ^= 1;
- cnt = 0;
- id -= n - 1;
- } else if(xr and cnt == n) {
- xr ^= 1;
- cnt = 0;
- fl++;
- }
- }
- }
- vector<int> vis(n * n);
- int cur = 1;
- for(int i = 0; i < n * n; i++) {
- if(!vis[i]) {
- queue<int> q;
- q.push(i);
- vis[i] = cur;
- while(!q.empty()) {
- int x = q.front(); q.pop();
- for(auto e : adj[x]) {
- if(!vis[e]) {
- vis[e] = cur;
- q.push(e);
- }
- }
- }
- cur++;
- }
- }
- for(int i = 0; i < q; i++) {
- int a, b;
- cin >> a >> b;
- cout << _ << "." << i << " " << (vis[a] == vis[b]) << "\n";
- }
- cout << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement