Advertisement
pablo7890

Drzewka FINAL VERSION :D

Apr 26th, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include<algorithm>
  2. #include<vector>
  3. #include <iostream>
  4. #include <cstdio>
  5. using namespace std;
  6. int wynik =0;
  7. typedef vector<int >   VI;
  8. const int INF = 1000000000;
  9.  
  10. int n,m, cnt;
  11. VI in, pi;
  12. vector<VI> adj;
  13.  
  14.  
  15. void read()
  16. {
  17.     int i, a,b;
  18.     scanf("%d %d", &n, &m);
  19.     adj.clear(); adj.resize(n);
  20.     for (i=0; i<m; i++)
  21.     {
  22.         scanf("%d %d", &a, &b);
  23.         if (a==b) continue;
  24.         a--; b--;
  25.         adj[a].push_back(b);
  26.         adj[b].push_back(a);
  27.     }
  28. }
  29.  
  30.  
  31. void init()
  32. {
  33.     in.clear (); in.resize (n,INF);
  34.     pi.resize(n);
  35.     cnt = 1;
  36. }
  37.  
  38. int dfs(int u)
  39. {
  40.     int ans = u;
  41.     in[u] = cnt++;
  42.     for (int i=0; i<adj[u].size(); i++)
  43.     {
  44.         int v = adj[u][i];
  45.  
  46.         if (v==u) continue;
  47.  
  48.         if (v==pi[u]) continue;
  49.  
  50.         if (in[v]==INF)
  51.         {
  52.             pi[v] = u;
  53.             v = dfs(v);
  54.         }
  55.         if (in[ans] > in[v]) ans=v;
  56.     }
  57.     if (ans==u && pi[u]!=u) wynik++;
  58.  
  59.     return ans;
  60. }
  61.  
  62. int main(void)
  63. {
  64.     read();
  65.     init();
  66.     for (int u=0; u<n; u++)
  67.         if (in[u]==INF)
  68.         {
  69.             pi[u] = u; dfs(u);
  70.         }
  71.     cout << wynik;
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement