Advertisement
hpnq

solved B7 task

Jun 2nd, 2024
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <random>
  4.  
  5. #define mp make_pair
  6. #define cve(tpy) for (int i = 0; i < sizeof(tpy)/sizeof(tpy[0]); i++) {for(int j = 0; j < sizeof(tpy[i])/sizeof(tpy[i][0]); j++){cout << tpy[i][j] << " ";  }cout << "\n";} ;
  7. #define f first
  8. #define s second
  9. #define loop(i, x, n) for (int i = x; i < n; i++)
  10. #define joop(x, n) for (ll j = x; j < n; j++)
  11. #define lp(n) for (ll i = 0; i < n; i++)
  12. #define err cout << "ERROR" << endl;
  13. #define all(x) x.begin(), x.end()
  14. #define pb push_back
  15. #define sz(x) x.size()
  16. #define rndm rng()
  17.  
  18. using namespace std;
  19.  
  20. // types
  21. typedef long long ll;
  22. typedef long double ld;
  23.  
  24. // types of data
  25. #define inf 1000000000
  26. #define infll 1000000000000000000
  27. #define INF ll(1e9)
  28.  
  29. #define md 998244353
  30. #define mod 1000000009
  31. //#define K 239017
  32.  
  33. #define DEBUG 1
  34.  
  35. const int N = 20;
  36.  
  37. void bfs(int adjList[N][N], int startNode, bool visited[N]) {
  38.     // Create a queue for BFS
  39.     int queue[N];
  40.     int front = 0, rear = 0;
  41.  
  42.     // Mark the current node as visited and enqueue it
  43.     visited[startNode] = true;
  44.     queue[rear++] = startNode;
  45.  
  46.     // Iterate over the queue
  47.     while (front != rear) {
  48.         int currentNode = queue[front++];
  49.         for (int neighbor = 0; neighbor < N; neighbor++) {
  50.             if (adjList[currentNode][neighbor] && !visited[neighbor]) {
  51.                 visited[neighbor] = true;
  52.                 queue[rear++] = neighbor;
  53.             }
  54.         }
  55.     }
  56. }
  57.  
  58. void solve() {
  59.     int n = 20;
  60.     int g[N][N] = {0};
  61.     loop(i, 0, n) {
  62.         loop(j, 0, n) {
  63.             int a;
  64.             cin >> a;
  65.             if (a == 1) {
  66.                 g[i][j] = 1;
  67.                 g[j][i] = 1;
  68.             }
  69.         }
  70.     }
  71.     int start = 2;
  72.     bool vis[N] = {0};
  73.     cout << "first group:\n";
  74.     bfs(g, start, vis);
  75.     loop(i, 0, n) {
  76.         if (vis[i]) {
  77.             cout << i + 1 << " ";
  78.         }
  79.     }
  80.     cout << "\n";
  81.     cout << "second group:\n";
  82.     bool vis2[N] = {0};
  83.     bfs(g, start, vis2);
  84.     loop(i, 0, n) {
  85.         if (!vis2[i]) {
  86.             cout << i + 1 << " ";
  87.         }
  88.     }
  89. }
  90.  
  91. int main() {
  92.     freopen("text.txt", "r", stdin);
  93.     solve();
  94.     return 1;
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement