Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. // Bipartit1 pbinfo.ro/probleme/472
  2.  
  3. // All rights reserved to Claudiu Ursescu ©
  4.  
  5. #include <bits/stdc++.h>
  6.  
  7. using namespace std;
  8.  
  9. ifstream fin("bipartit1.in");
  10. ofstream fout("bipartit1.out");
  11.  
  12. vector< vector < int > > mat(16, vector< int >(16));
  13. vector< int > color(16, -1);
  14. // 1 = rosu // 0 = albastru
  15.  
  16. int n, m;
  17. int k;
  18.  
  19. void read()
  20. {
  21. fin >> n >> m;
  22. int x, y;
  23. for(int i = 1; i <=m; ++i)
  24. {
  25. fin >> x >> y;
  26. mat[x][y] = mat[y][x] = 1;
  27. }
  28. }
  29.  
  30. bool isBipartite(int pos, int c)
  31. {
  32. if(color[pos] != -1 && color[pos] != c)
  33. return false;
  34.  
  35. color[pos] = c;
  36. bool ok = true;
  37. for(int i = 1; i <= n; ++i)
  38. {
  39. if(mat[i][pos] == 1)
  40. {
  41. if(color[i] == -1)
  42. ok &= isBipartite(i, 1-c);
  43. if(color[i] != -1 && color[i] != 1-c)
  44. return false;
  45. }
  46. if(!ok)
  47. return false;
  48. }
  49. return true;
  50. }
  51.  
  52. void print()
  53. {
  54. int clr = color[1];
  55. fout << 1 << " ";
  56.  
  57. for(int i = 2; i <= n; ++i)
  58. if(color[i] == clr)
  59. fout << i << " ";
  60. fout << endl;
  61. for(int i = 2; i <= n; ++i)
  62. if(color[i] != clr)
  63. fout << i << " ";
  64. }
  65.  
  66. int main()
  67. {
  68. read();
  69. int pos = 1;
  70. if(isBipartite(pos,1) == true)
  71. fout << "DA" << endl;
  72. else
  73. fout << "NU" << endl;
  74. print();
  75. return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement