Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
- #define ll long long
- #define ld long double
- #define pb push_back
- #define fe first
- #define se second
- #define nl "\n"
- #define pp pair < ll , ll >
- #define sz(x) (ll)x.size()
- #define st(x) sort(x.begin(),x.end())
- #define rst(x) sort(x.rbegin(), x.rend())
- #define all(x) x.begin(),x.end()
- long double pi = 3.14159265358979323;
- const double EPS = 1e-12;
- const int N = 1e6 + 5;
- //Just A Gentle Reminder about MOD
- const int mod = 1e9 + 7;
- //Articulation point
- //Two Condn
- //1 - if u is root node
- //it must have two children components
- //2 - not a root
- //there should not a back edge from subgraph of v to ancestor of u
- vector < ll > v[N];
- //vector < ll > ans;
- int tin[N];
- int low[N]; //earliest node which is back edge
- int vis[N];
- bool Arti[N];
- ll par[N];
- ll timer;
- void dfs(ll s , ll timer){
- int cnt = 0 ;
- timer++;
- tin[s] = timer;
- low[s] = timer;
- vis[s] = 1;
- for(auto it : v[s]){
- if(!vis[it]){
- par[it] = s;
- cnt++;
- dfs(it,timer);
- low[s] = min(low[it] , low[s]);
- //For Root Node
- if(s == 1 && cnt > 1){
- //cout << s << " ";
- Arti[s] = 1;
- }
- //For Any Other Node
- //if low[it] < tin[s] back edge is present!!
- //condn for no back edge
- else if(s != 1 && low[it] >= tin[s]){
- Arti[s] = 1;
- }
- }
- //FOR BACK EDGE
- else if(it != par[s]){
- low[s] = min(tin[it] , low[s]);
- }
- }
- //vis[s] = 2;
- }
- int main()
- {
- fast;
- // ll t;
- // cin >> t ;
- ll n , m;
- while(cin >> n >> m && m != 0 && n != 0 ){
- for(int i = 0 ; i <= n ;i++ ){
- v[i].clear();
- vis[i] = tin[i] = low[i] = Arti[i] = par[i] = 0;
- }
- timer = 0;
- for(int i = 0 ; i < m ;i++ ){
- ll a , b;
- cin >> a >> b ;
- //cout << a << " " << b << "\n";
- v[a].pb(b);
- v[b].pb(a);
- }
- for(int i = 1 ; i <= n; i++ ){
- if(!vis[i]){
- dfs(i,0);
- }
- }
- ll ans = 0 ;
- for(int i = 1 ; i <= n; i++ ){
- if(Arti[i])ans++ ;
- }
- cout << ans << "\n";
- }
- return 0;
- //It's not end it yet
- }
Advertisement
Add Comment
Please, Sign In to add comment