Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<algorithm>
- #include<vector>
- #include <iostream>
- #include <cstdio>
- using namespace std;
- int wynik =0;
- typedef vector<int > VI;
- const int INF = 1000000000;
- int n,m, cnt;
- VI in, pi;
- vector<VI> adj;
- void read()
- {
- int i, a,b;
- scanf("%d %d", &n, &m);
- adj.clear(); adj.resize(n);
- for (i=0; i<m; i++)
- {
- scanf("%d %d", &a, &b);
- if (a==b) continue;
- a--; b--;
- adj[a].push_back(b);
- adj[b].push_back(a);
- }
- }
- void init()
- {
- in.clear (); in.resize (n,INF);
- pi.resize(n);
- cnt = 1;
- }
- int dfs(int u)
- {
- int ans = u;
- in[u] = cnt++;
- for (int i=0; i<adj[u].size(); i++)
- {
- int v = adj[u][i];
- if (v==u) continue;
- if (v==pi[u]) continue;
- if (in[v]==INF)
- {
- pi[v] = u;
- v = dfs(v);
- }
- if (in[ans] > in[v]) ans=v;
- }
- if (ans==u && pi[u]!=u) wynik++;
- return ans;
- }
- int main(void)
- {
- read();
- init();
- for (int u=0; u<n; u++)
- if (in[u]==INF)
- {
- pi[u] = u; dfs(u);
- }
- cout << wynik;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement