Maruf_Hasan

BiColoring

Feb 20th, 2021
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1.  
  2. //In the Name of Allah Most Gracious, Most Merciful//
  3. /*If you want something you've never had, you have to do something you never did.*/
  4.  
  5. #include<bits/stdc++.h>
  6. using namespace std;
  7.  
  8.  
  9. #define pb push_back
  10. #define ll long long
  11. #define pii pair<int,int>
  12. #define pll pair<ll,ll>
  13. #define M 101007
  14. #define INF 1e9
  15. #define INFL 1e18
  16. #define PI acos(-1)
  17. #define mp make_pair
  18.  
  19. //const int fx[]= {+1,-1,+0,+0};
  20. //const int fy[]= {+0,+0,+1,-1};
  21. //const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; // Kings Move
  22. //const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; // Kings Move
  23. //const int fx[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move
  24. //const int fy[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move
  25.  
  26.  
  27. int gcd(int a, int b)
  28. {
  29. return b ? gcd(b, a%b) : a;
  30. }
  31.  
  32. int color[2*M+100];
  33. bool visited[2*M+100];
  34. vector<int>adj[2*M];
  35.  
  36.  
  37. bool dfs(int node)
  38. {
  39. for(int i=0;i<adj[node].size();i++)
  40. {
  41. int x=adj[node][i];
  42. if(visited[x]==false)
  43. {
  44. visited[x]=true;
  45. color[x]=!color[node];
  46. if(!dfs(x))
  47. return false;
  48. }
  49. else if(color[x]==color[node])
  50. return false;
  51. }
  52. return true;
  53. }
  54.  
  55.  
  56. int main()
  57. {
  58.  
  59. // #ifndef ONLINE_JUDGE
  60. // freopen("input.txt","r",stdin);
  61. // freopen("output.txt","w",stdout);
  62. // #endif
  63. int n,m;
  64. cin>>n>>m;
  65. vector<pii>v;
  66. for(int i=0;i<m;i++)
  67. {
  68. int x,y;
  69. cin>>x>>y;
  70. adj[x].pb(y);
  71. adj[y].pb(x);
  72. v.pb(mp(x,y));
  73. }
  74. memset(color,0,sizeof color);
  75. memset(visited,false,sizeof visited);
  76. visited[1]=true;
  77. bool ans=dfs(1);
  78. if(ans)
  79. {
  80. cout<<"YES"<<endl;
  81. for(int i=0;i<m;i++)
  82. {
  83. int x=v[i].first;
  84. int y=v[i].second;
  85. if(color[x]==0)
  86. {
  87. cout<<1;
  88. }
  89. else
  90. {
  91. cout<<0;
  92. }
  93. }
  94. cout<<endl;
  95. //cout<<endl;
  96. }
  97. else
  98. {
  99. cout<<"NO"<<endl;
  100.  
  101. }
  102. //#ifdef LOCAL_DEFINE
  103. // cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
  104. // #endif
  105. ///Before submit=>
  106. /// *check for integer overflow,array bounds
  107. /// *check for n=1
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment