-
Notifications
You must be signed in to change notification settings - Fork 0
/
articulation_points(DAA).cpp
90 lines (87 loc) · 1.43 KB
/
articulation_points(DAA).cpp
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include<iostream>
#include<vector>
#include<fstream>
#include<stack>
using namespace std;
int M, N;
vector<vector<int> > graph;
int dfsAP()
{
int v, tv, sv = 0, num = 1, ap = 0, rAPC = 0;
bool allVis;
stack<int> s;
vector<int> DFN(N), level(N), parent(N);
vector<bool> visited(N), isAp(N);
visited[sv] = true;
s.push(sv);
DFN[sv] = num++;
parent[sv] = -1;
level[sv] = DFN[sv];
while(!s.empty())
{
int v = s.top();
allVis = true;
for(size_t i = 0;i<graph[v].size();++i)
{
tv = graph[v][i];
if(!visited[tv])
{
if(v==sv)
++rAPC;
visited[tv] = true;
s.push(tv);
DFN[tv] = num++;
parent[tv] = v;
level[tv] = DFN[tv];
allVis = false;
break;
}
}
if(allVis)
{
s.pop();
for(size_t i = 0;i<graph[v].size();++i)
{
tv = graph[v][i];
if(tv!=parent[v] && DFN[tv]<DFN[v])
level[v] = (level[v]<DFN[tv])?level[v]:DFN[tv];
else
{
if(v==parent[tv])
{
level[v] = (level[v]<level[tv])?level[v]:level[tv];
if(level[tv]>=DFN[v] && v != sv &&!isAp[v])
{
++ap;
isAp[v] = true;
//cout<<v<<" ";
}
}
}
}
}
}
if(rAPC>=2)
{
//cout<<sv<<endl;
++ap;
}
return ap;
}
int main()
{
//ifstream file("Test.in");
//file>>N>>M;
cin>>N>>M;
graph.resize(N);
while(M--)
{
int v1, v2;
//file>>v1>>v2;
cin>>v1>>v2;
graph[v1].push_back(v2);
graph[v2].push_back(v1);
}
cout<<dfsAP()<<endl;
return 0;
}