AlexandruT

[pbInfo] bipartit1

Dec 4th, 2016
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #define NMax 101
  4. using namespace std;
  5.  
  6. ifstream fin("bipartit1.in");
  7. ofstream fout("bipartit1.out");
  8.  
  9.  
  10. vector <int> a[NMax];
  11. int uz[NMax];
  12. int n;
  13.  
  14. void Citire()
  15. {
  16.     int m, x, y;
  17.     fin >> n >> m;
  18.     while(fin >> x >> y)
  19.     {
  20.         a[x].push_back(y);
  21.         a[y].push_back(x);
  22.     }
  23. }
  24.  
  25. void DFS(int x)
  26. {
  27.     for(int i = 0; i < a[x].size(); i++)
  28.         if(uz[a[x][i]] == 0)
  29.         {
  30.             uz[a[x][i]] = 3 - uz[x];
  31.             DFS(a[x][i]);
  32.         }
  33. }
  34.  
  35. int main()
  36. {
  37.     Citire();
  38.     uz[1] = 1;
  39.     DFS(1);
  40.     for(int i = 1; i <= n; i++)
  41.         for(int j = 0; j < a[i].size(); j++)
  42.             if(uz[i] == uz[a[i][j]])
  43.             {
  44.                 fout << "NU";
  45.                 return 0;
  46.             }
  47.     fout << "DA\n";
  48.     for(int i = 1; i <= n; i++)
  49.         if(uz[i] == 1)
  50.             fout << i << " ";
  51.     fout << "\n";
  52.     for(int i = 1; i <= n; i++)
  53.         if(uz[i] == 2)
  54.             fout << i << " ";
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment