fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. using namespace std;
  5.  
  6.  
  7. map<int, int> getColor;
  8. string RES = "YES";
  9.  
  10. class Fonar {
  11. public:
  12. int num;
  13. int color = 0;
  14. bool visited = false;
  15. vector<Fonar*> sosedi;
  16.  
  17. void add(Fonar *sosed)
  18. {
  19. sosedi.push_back(sosed);
  20. }
  21.  
  22. };
  23.  
  24. void onecolor(Fonar *ff) {
  25. Fonar f = *ff;
  26. if (!f.visited & RES != "NO")
  27. for (auto & i: f.sosedi) {
  28. if ((*i).color == f.color) {
  29. RES = "NO";
  30. }
  31.  
  32. (*i).color = getColor[f.color];
  33. (*i).visited = true;
  34. onecolor(i);
  35. }
  36. }
  37.  
  38. void coloring(map<int, Fonar> *fonarii) {
  39. map<int, Fonar> fonari = *fonarii;
  40. for (int j = 1; j <= fonari.size(); j++) {
  41. cout << fonari[j].color << " " << endl;
  42. if (fonari[j].color == 0) fonari[j].color = 1;
  43. onecolor(&fonari[j]);
  44. }
  45. }
  46.  
  47. int main() {
  48. getColor[1] = 2;
  49. getColor[2] = 1;
  50. int test;
  51. cin >> test;
  52. while(test--) {
  53. int n, m;
  54. cin >> n >> m;
  55. map<int, Fonar> fonari;
  56. for (int i = 0; i < m; i++) {
  57. int t, tt;
  58. cin >> t >> tt;
  59. if (fonari.count(t) == 0) {
  60. Fonar tmp;
  61. tmp.num = t;
  62. fonari[t] = tmp;
  63. }
  64. if (fonari.count(tt) == 0) {
  65. Fonar ttmp;
  66. ttmp.num = tt;
  67. fonari[tt] = ttmp;
  68. }
  69. fonari[t].add(&fonari[tt]);
  70. fonari[tt].add(&fonari[t]);
  71. }
  72.  
  73. fonari[1].color = 1;
  74. coloring(&fonari);
  75. cout << RES << endl;
  76. RES = "YES";
  77. }
  78. }
Success #stdin #stdout 0.01s 5440KB
stdin
3
7 3 1 2 1 3 6 1
4 4 2 1 3 2 1 4 3 1
4 4 1 3 4 2 4 1 2 3
stdout
1 
0 
0 
0 
0 
0 
NO
1 
0 
0 
0 
NO
1 
0 
0 
0 
NO