fork download
  1. #include <iostream>
  2. #include<bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. int n,m;
  7.  
  8. int main() {
  9.  
  10. cin>>n>>m;
  11. int flag=0;
  12. vector<vector<int>>arr(m,vector<int>(2));
  13. vector<vector<int>>adj(n+1);
  14.  
  15. for(int i=0;i<m;i++)
  16. {
  17. cin>>arr[i][0]>>arr[i][1];
  18. }
  19.  
  20. for(int i=0;i<m;i++)
  21. {
  22. adj[arr[i][0]].push_back(arr[i][1]);
  23. adj[arr[i][1]].push_back(arr[i][0]);
  24. }
  25.  
  26. vector<int>set(n+1,0);
  27.  
  28. for(int i=1;i<=n;i++)
  29. {
  30. vector<int>temp=adj[i];
  31. int team1=0,team2=0;
  32.  
  33. if(set[i]==0)
  34. {
  35. for(auto x: temp)
  36. {
  37. if(set[x]==1)
  38. {
  39. team1=2; team2=1; break;
  40. }
  41.  
  42. else if(set[x]==2)
  43. {
  44. team1=1; team2=2; break;
  45. }
  46.  
  47. else
  48. {
  49. team1=1; team2=2;
  50. }
  51.  
  52. }
  53.  
  54. cout<<flag<<endl;
  55. }
  56.  
  57.  
  58. else if(set[i]==1)
  59. {
  60. team1=1;
  61.  
  62. for(auto x: temp)
  63. {
  64. if(set[x]==1)
  65. {
  66. flag=1; break;
  67. }
  68.  
  69. }
  70.  
  71. team2=2;
  72.  
  73. if(flag)
  74. {
  75. break;
  76. }
  77. cout<<flag<<endl;
  78. }
  79.  
  80.  
  81.  
  82. else if(set[i]==2)
  83. {
  84. team1=2;
  85.  
  86. for(auto x: temp)
  87. {
  88. if(set[x]==2)
  89. {
  90. flag=1; break;
  91. }
  92. }
  93.  
  94. team2=1;
  95.  
  96. if(flag)
  97. {
  98. break;
  99. }
  100. cout<<flag<<endl;
  101. }
  102.  
  103. set[i]=team1;
  104. for(auto x:temp)
  105. {
  106. set[x]=team2;
  107. }
  108. }
  109. cout<<flag<<endl;
  110. if(flag==0) for(int i=1;i<=n;i++)cout<<set[i]<<" ";
  111. else cout<<"IMPOSSIBLE";
  112.  
  113. }
Success #stdin #stdout 0.01s 5288KB
stdin
10 20
7 10
3 10
9 10
2 10
1 7
4 7
2 8
4 9
5 9
1 9
7 8
2 6
8 9
5 7
3 4
2 5
6 9
3 5
3 6
6 7
stdout
0
0
0
1
IMPOSSIBLE