Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. const int mx = 200001;
  10. bool color[mx];
  11. int N, M, timer, in[mx], up[mx], count, head, clr[mx], maxcolor;
  12. vector<int> rebr[mx];
  13. vector<int> rebr_number[mx];
  14. vector<int> stash;
  15.  
  16. void dfs(int v, int p)
  17.     {
  18.         color[v] = true;
  19.         timer++;
  20.         in[v] = timer - 1;
  21.         up[v] = timer - 1;
  22.         for (int i = 0; i < rebr[v].size(); i++)
  23.             {
  24.                 if (clr[rebr_number[v][i]] == 0)
  25.                     stash.push_back(rebr_number[v][i]);
  26.                 int to = rebr[v][i];
  27.                 if (to == p)
  28.                     continue;
  29.                 if (color[to])
  30.                     {
  31.                         if (up[v] > in[to])
  32.                             up[v] = in[to];
  33.                     }
  34.                 else
  35.                     {
  36.                         if (v == head)
  37.                             count++;
  38.                         dfs(to, v);
  39.                         up[v] = min(up[v], up[to]);
  40.                         if (up[to] >= in[v])
  41.                             {
  42.                                 maxcolor++;
  43.                                 int temp = 0;
  44.                                 while ( (stash.back() != rebr_number[v][i]) || (temp < 1) )
  45.                                     {
  46.                                         if (stash.back() == rebr_number[v][i])
  47.                                             temp++;
  48.                                         clr[stash.back()] = maxcolor;
  49.                                         stash.pop_back();
  50.                                     }
  51.                                 clr[rebr_number[v][i]] = maxcolor;
  52.                                 stash.pop_back();
  53.                             }
  54.                         else if (up[to] < up[v])
  55.                             up[v] = up[to];
  56.                     }
  57.             }
  58.     }
  59.  
  60. int main()
  61.     {
  62.         freopen("biconv.in","r",stdin);
  63.         freopen("biconv.out","w",stdout);
  64.         int M;
  65.         scanf("%d %d\n", &N, &M);
  66.         for(int i = 0; i < mx; i++)
  67.             {
  68.                 color[i] = false;
  69.                 clr[i] = 0;
  70.             }
  71.         for(int i = 0; i < M; i++)
  72.             {
  73.                 int a, b;
  74.                 scanf("%d %d\n", &a, &b);
  75.                 rebr[a].push_back( b);
  76.                 rebr[b].push_back( a);
  77.                 rebr_number[a].push_back(i + 1);
  78.                 rebr_number[b].push_back(i + 1);
  79.             }
  80.         maxcolor = 0;
  81.         for(int i = 1; i <= N; i++)
  82.             {
  83.                 timer = 0;
  84.                 head = i;
  85.                 count = 0;
  86.                 if (!color[i])
  87.                     dfs(i, -1);
  88.             }
  89.         printf("%d\n", maxcolor);
  90.         for(int i = 1; i <= M; i++)
  91.             printf("%d ", clr[i]);
  92.         fclose(stdin);
  93.         fclose(stdout);
  94.         return 0;
  95.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement