Advertisement
splash365

MCC Class - Graph

Jul 15th, 2021
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define MAXN 100
  5.  
  6. vector<int> adj[MAXN];
  7. bool vis[MAXN];
  8. int parent[MAXN];
  9. int dist[MAXN];
  10. int N, E;
  11.  
  12. int color[MAXN];
  13.  
  14. bool hasCycle = false;
  15. bool isBipartite = true;
  16.  
  17. void init()
  18. {
  19.     for (int i = 0; i < MAXN; i++)
  20.         adj[i].clear();
  21.     memset(vis, false, sizeof(vis));
  22.     memset(parent, -1, sizeof(parent));
  23.     memset(dist, 0, sizeof(dist));
  24.     memset(color, 0, sizeof(color));
  25.     hasCycle = false;
  26.     isBipartite = true;
  27. }
  28.  
  29. void dfs(int u)
  30. {
  31.     vis[u] = true;
  32.     cout << u << ' ';
  33.     for (int i = 0; i < adj[u].size(); i++)
  34.     {
  35.         int v = adj[u][i];
  36.         if(vis[v] == false)
  37.         {
  38.             parent[v] = u;
  39.             color[v] = !color[u];
  40.             dfs(v);
  41.         }
  42.         else
  43.         {
  44.             if(parent[u] != v)
  45.                 hasCycle = true;
  46.             if(color[u] == color[v])
  47.                 isBipartite = false;
  48.         }
  49.     }
  50. }
  51.  
  52. void bfs(int src)
  53. {
  54.     queue<int> Q;
  55.  
  56.     dist[src] = 0;
  57.     vis[src] = true;
  58.     parent[src] = -1;
  59.     Q.push(src);
  60.  
  61.     while(Q.empty() == false)
  62.     {
  63.         int u = Q.front();
  64.         Q.pop();
  65.  
  66.         for (int i = 0; i < adj[u].size(); i++)
  67.         {
  68.             int v = adj[u][i];
  69.             if(vis[v] == false)
  70.             {
  71.                 dist[v] = dist[u] + 1;
  72.                 vis[v] = true;
  73.                 parent[v] = u;
  74.                 Q.push(v);
  75.             }
  76.         }
  77.     }
  78. }
  79.  
  80. int main()
  81. {
  82.     freopen("input.txt", "r", stdin);
  83.     freopen("output.txt", "w", stdout);
  84.  
  85.     init();
  86.  
  87.     cin >> N >> E;
  88.     for (int i = 0; i < E; i++)
  89.     {
  90.         int u, v;
  91.         cin >> u >> v;
  92.         adj[u].push_back(v);
  93.         adj[v].push_back(u);
  94.     }
  95.  
  96.     for (int i = 0; i < N; i++)
  97.     {
  98.         cout << i << ": ";
  99.         for (int j = 0; j < adj[i].size(); j++)
  100.         {
  101.             cout << adj[i][j] << " ";
  102.         }
  103.         cout << endl;
  104.     }
  105.  
  106.     int src = 0;
  107.  
  108.     // bfs(src);
  109.  
  110.  
  111.     // cout << "Distance: \n";
  112.     // for (int i = 0; i < N; i++)
  113.     // {
  114.     //     cout << i << ": " << dist[i] << endl;
  115.     // }
  116.     color[src] = 0;
  117.  
  118.     dfs(src);
  119.  
  120.     if(isBipartite == true)
  121.         cout << "\nBipartite\n";
  122.     if(hasCycle)
  123.         cout << "Cycle\n";
  124.  
  125.     return 0;
  126. }
  127.  
  128. /*
  129.  
  130. adj[0] -> vector {...}
  131. adj[1] -> vector {...}
  132. .
  133. .
  134. .
  135. adj[MAXN-1] -> vector {...}
  136.  
  137.  
  138. vis {0 0 0 0 0 0 0}
  139.  
  140. memset -> 0, 1, -1
  141.  
  142.  
  143.  
  144. INPUT:
  145. 6 7
  146. 0 1
  147. 1 2
  148. 3 5
  149. 2 3
  150. 3 4
  151. 0 3
  152. 2 1
  153.  
  154.  
  155. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement