forked from shivammaniharsahu/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharticulation points.cpp
More file actions
73 lines (73 loc) · 1.14 KB
/
articulation points.cpp
File metadata and controls
73 lines (73 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<bits/stdc++.h>
using namespace std;
vector<int> ar[100001];
int vis[100001], low[100001], in[100001];
int timer;
set<int> AP;
void dfs(int node,int par)
{
vis[node]=1;
in[node]=low[node]=timer++;
int child_cnt=0;
for(int i=0;i<ar[node].size();i++)
{
if(ar[node][i]==par)
{
continue;
}
if(vis[ar[node][i]]==1)
{
//edge node-child is a back edge
low[node]= min(low[node], in[ar[node][i]]);
}
else
{
//edge node-child is a forward edge
dfs(ar[node][i],node);
child_cnt++;
low[node]= min(low[node], low[ar[node][i]]);
if(low[ar[node][i]]>=in[node] && par!=-1)
{
AP.insert(node);
}
}
}
if(par == -1 && child_cnt>1)
{
AP.insert(node);
}
}
int main()
{
int n,m,a,b;
while(1)
{
cin>>n>>m;
if(n==0 && m==0)
{
break;
}
for(int i=1;i<=n;i++)
{
ar[i].clear();
vis[i]=0;
AP.clear();
timer=1;
}
for(int i=1;i<=m;i++)
{
cin>>a>>b;
ar[a].push_back(b);
ar[b].push_back(a);
}
for(int i=1;i<=n;i++)
{
if(vis[i]==0)
{
dfs(i,-1);
}
}
cout<<AP.size()<<"\n";
}
return 0;
}