Advertisement
splash365

bipartite

Jan 18th, 2021
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define     fast            ios_base::sync_with_stdio(false);cin.tie(NULL)
  5. #define     read            freopen("input.txt","r",stdin)
  6. #define     write           freopen("output.txt","w",stdout)
  7.  
  8. const int MAXN = 107;
  9.  
  10.  
  11. map<string, vector<string>> adj;
  12. map<string, bool> vis;
  13. map<string, int> color;
  14. map<string, string> parent;
  15. int N, E;
  16. bool isSame = false;
  17. bool isCycle = false;
  18.  
  19.  
  20. void dfs(string src)
  21. {
  22.     cout << src << endl;
  23.     vis[src] = true;
  24.     for(auto v : adj[src])
  25.     {
  26.         if(!vis[v])
  27.         {
  28.             color[v] = !color[src];
  29.             parent[v] = src;
  30.             dfs(v);
  31.         }
  32.         else
  33.         {
  34.             if(color[v] == color[src]) isSame = true;
  35.             if(parent[src] != v)
  36.                 isCycle = true;
  37.         }
  38.     }
  39. }
  40.  
  41.  
  42.  
  43. int main() {
  44. #ifndef ONLINE_JUDGE
  45.     read;
  46.     write;
  47. #endif
  48.     fast;
  49.     cin >> N >> E;
  50.     for (int i = 0; i < E; i++)
  51.     {
  52.         string u, v;
  53.         cin >> u >> v;
  54.         adj[u].push_back(v);
  55.         adj[v].push_back(u);
  56.     }
  57.     dfs("em");
  58.     if(isSame) cout << "Not Possible\n" << endl;
  59.     else cout << "Possible\n" << endl;
  60.     if(isCycle) cout << "Cycle\n" << endl;
  61.     else cout << "No Cycle\n" << endl;
  62.     return 0;
  63. }
  64. /*
  65. 6 6
  66. em fa
  67. fa ra
  68. ra sh
  69. ko ra
  70. ko far
  71. far sh
  72. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement