Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. /*
  2. __Allahu Akbar__
  3. __All_praises_to_Allah__
  4. __Bismillahir_Rahmanir_Rahim__
  5.  
  6. Author: Abdullah Al Nayem
  7. Studying B.Sc in CSE at Leading University.
  8.  
  9. Practice, like you've never won. Perform, like you've never lost.
  10. Practice hard, play hard. Be hard to beat.
  11.  
  12. */
  13.  
  14. #include <bits/stdc++.h>
  15. #include <iostream>
  16. #include <cstdio>
  17. #include <algorithm>
  18. #include <math.h>
  19. #include <bitset>
  20. using namespace std;
  21.  
  22. #define MAXN 100007
  23. #define ll long long
  24. #define pb push_back
  25. #define mkp make_pair
  26. #define F first
  27. #define S second
  28. #define Sort_arr(arr,x) sort(arr,arr+x)
  29. #define Sortr_arr(arr,x) sort(arr, arr+n,greater<int>())
  30. #define SortS(str) sort(str.begin(), str.end())
  31. #define SortRS(str) sort(str.rbegin(), str.rend())
  32. #define arr_rev(n) sort(n.begin(), n.end(), greater<int>())
  33. #define gin(a) getline(cin,a)
  34. #define Sz(a) a.size()
  35. #define Ignore cin.ignore()
  36. #define sq(n) (n*n)
  37. #define cube(n) (n*n*n)
  38. #define min3(a, b, c) min(a, min(b, c))
  39. #define max3(a, b, c) max(a, max(b, c))
  40. #define YES cout << "YES" << endl
  41. #define NO cout << "NO" << endl
  42. #define pYES printf("YES\n")
  43. #define pNO printf("NO\n")
  44. #define bs binary_search
  45. #define lb lower_bound
  46. #define ub upper_bound
  47. #define all(a) a.begin(),a.end()
  48. #define Fast_read ios_base::sync_with_stdio(false);
  49. #define Test_case int test_case = 1;cin>>test_case;for (int Case = 1; Case<=test_case; Case++)
  50.  
  51. vector <int> vec[MAXN], cnt(MAXN), parent(MAXN), visited(MAXN);
  52. int totalCount = 1;
  53.  
  54. void bfs(int node)
  55. {
  56. int edgeCount = 0, marker = 0;
  57. queue <int> q;
  58. q.push(node);
  59.  
  60. while(!q.empty())
  61. {
  62. int t = q.front();
  63. q.pop();
  64.  
  65. if (visited[t]) continue;
  66. visited[t] = true;
  67.  
  68. if (cnt[t]%2 == 1) marker = t;
  69. parent[t] = 1;
  70.  
  71. for (int i = 0; i<vec[t].size(); i++)
  72. {
  73. if (!visited[vec[t][i]]) q.push(vec[t][i]), edgeCount++;
  74. }
  75. }
  76.  
  77. if ((edgeCount & 1) && marker) totalCount = max(totalCount, 2), parent[marker] = 2;
  78. else if (edgeCount & 1)
  79. {
  80. totalCount = 3;
  81. parent[node] = 3;
  82. parent[vec[node][0]] = 2;
  83. }
  84. }
  85.  
  86. int main()
  87. {
  88. Fast_read
  89.  
  90. Test_case
  91. {
  92. totalCount = 1;
  93. for (int i = 0; i<MAXN; i++) {vec[i].clear(); cnt[i] = 0; visited[i] = 0; parent[i] = 0;}
  94.  
  95. int n, m, x, y;
  96. cin >> n >> m;
  97.  
  98. for (int i = 0; i<m; i++)
  99. {
  100. cin >> x >> y;
  101.  
  102. vec[x].push_back(y);
  103. vec[y].push_back(x);
  104.  
  105. cnt[x]++;
  106. cnt[y]++;
  107. }
  108.  
  109. if (m%2 == 0)
  110. {
  111. cout << 1 << endl;
  112. for (int i = 0; i<n; i++)
  113. {
  114. cout << 1 << " ";
  115. }
  116. cout << endl;
  117. continue;
  118. }
  119.  
  120. for (int i = 1; i<=n; i++)
  121. {
  122. if (!visited[i]) bfs(i);
  123. }
  124. cout << totalCount << endl;
  125.  
  126. for (int i = 1; i<=n; i++) cout << parent[i] << " ";
  127. cout << endl;
  128. }
  129.  
  130. return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement