Advertisement
cosenza987

Untitled

Mar 9th, 2024
685
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     ios_base::sync_with_stdio(false);
  7.     cin.tie(nullptr);
  8.     int t;
  9.     cin >> t;
  10.     for(int _ = 0; _ < t; _++) {
  11.         int n, m, q;
  12.         cin >> n >> m >> q;
  13.         set<int> s;
  14.         while(m--) {
  15.             int x;
  16.             cin >> x;
  17.             s.insert(x);
  18.         }
  19.         vector<vector<int>> adj(n * n);
  20.         int fl = 0, xr = 0, cnt = 0, id = 0;
  21.         for(int i = 0; i < 2 * (n * n - n); i++) {
  22.             if(s.find(i) == s.end()) {
  23.                 cnt++;
  24.                 id++;
  25.                 if(!xr and cnt == n - 1) {
  26.                     xr ^= 1;
  27.                     cnt = 0;
  28.                     id -= n - 1;
  29.                 } else if(xr and cnt == n) {
  30.                     xr ^= 1;
  31.                     cnt = 0;
  32.                     fl++;
  33.                 }
  34.                 continue;
  35.             }
  36.             if(!xr) {
  37.                 adj[id].push_back(id + 1);
  38.                 adj[id + 1].push_back(id);
  39.                 cnt++;
  40.                 id++;
  41.                 if(!xr and cnt == n - 1) {
  42.                     xr ^= 1;
  43.                     cnt = 0;
  44.                     id -= n - 1;
  45.                 } else if(xr and cnt == n) {
  46.                     xr ^= 1;
  47.                     cnt = 0;
  48.                     fl++;
  49.                 }
  50.             } else {
  51.                 adj[id].push_back(id + n);
  52.                 adj[id + n].push_back(id);
  53.                 cnt++;
  54.                 id++;
  55.                 if(!xr and cnt == n - 1) {
  56.                     xr ^= 1;
  57.                     cnt = 0;
  58.                     id -= n - 1;
  59.                 } else if(xr and cnt == n) {
  60.                     xr ^= 1;
  61.                     cnt = 0;
  62.                     fl++;
  63.                 }
  64.             }
  65.         }
  66.         vector<int> vis(n * n);
  67.         int cur = 1;
  68.         for(int i = 0; i < n * n; i++) {
  69.             if(!vis[i]) {
  70.                 queue<int> q;
  71.                 q.push(i);
  72.                 vis[i] = cur;
  73.                 while(!q.empty()) {
  74.                     int x = q.front(); q.pop();
  75.                     for(auto e : adj[x]) {
  76.                         if(!vis[e]) {
  77.                             vis[e] = cur;
  78.                             q.push(e);
  79.                         }
  80.                     }
  81.                 }
  82.                 cur++;
  83.             }
  84.         }
  85.         for(int i = 0; i < q; i++) {
  86.             int a, b;
  87.             cin >> a >> b;
  88.             cout << _ << "." << i << " " << (vis[a] == vis[b]) << "\n";
  89.         }
  90.         cout << "\n";
  91.     }
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement