4eyes4u

Untitled

Nov 22nd, 2016
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. /*
  2. Hierholzerov algoritam za pronazal Ojlerovog ciklusa u O(m)
  3. Podrazumevano su svi cvorovi sa pozitivnim stepenom jedna komponenta
  4. */
  5.  
  6. #include <bits/stdc++.h>
  7. using namespace std;
  8.  
  9. const int N=1e5+10;
  10.  
  11. struct Edge
  12. {
  13.     int u, v, next;
  14. } grana[N];
  15.  
  16. int n, m, k, E;
  17. int head[N];
  18. int deg[N];
  19. bool mark[N];
  20.  
  21. void addEdge (int u, int v)
  22. {
  23.     grana[E++]=(Edge){u, v, head[u]};
  24.     head[u]=E-1;
  25. }
  26.  
  27. bool EulerianCycle ()
  28. {
  29.     for (int i=1;i<=n;i++) if (deg[i]&1) return false;
  30.     return true;
  31. }
  32.  
  33. void dfs (int v)
  34. {
  35.     for (int e=head[v];e!=-1;e=grana[e].next)
  36.     {
  37.         if (mark[e]) continue;
  38.         int xt=grana[e].v;
  39.         printf ("%d %d\n", v, xt);
  40.         mark[e]=mark[e^1]=true;
  41.         dfs (xt);
  42.     }
  43. }
  44.  
  45. int main()
  46. {
  47.     scanf ("%d%d", &n, &m);
  48.     fill (head+1, head+n+1, -1);
  49.     for (int i=0;i<m;i++)
  50.     {
  51.         int a, b;
  52.         scanf ("%d%d", &a, &b);
  53.         addEdge(a, b);
  54.         addEdge(b, a);
  55.         deg[a]++;
  56.         deg[b]++;
  57.     }
  58.  
  59.     if (EulerianCycle()) dfs (1);
  60.  
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment