-
Notifications
You must be signed in to change notification settings - Fork 12
/
SUBMERGE - Submerging Islands.cpp
105 lines (89 loc) · 1.49 KB
/
SUBMERGE - Submerging Islands.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define int long long
#define fast ios::sync_with_stdio(false)
const int N = 1e5+5;
vector<int>g[N];
vector<bool>visit(N , false);
int ans = 0;
int tmin[N];
int low[N];
int t = 0;
int v,e;
int arr[N];
void dfs(int v,int p=-1)
{
visit[v] = true;
tmin[v] = low[v] = ++t;
int child = 0;
for(auto i:g[v])
{
if(p == i)continue;
if(visit[i])
{
low[v] = min(low[v] , tmin[i]);
}
else
{
dfs(i,v);
low[v] = min(low[i],low[v]);
if(low[i] >= tmin[v] && p!=-1 )
arr[v]=1;
child++;
}
if(p == -1 && child > 1)arr[v]=1;
}
}
void reset()
{
for(int i=0;i<N;i++)
{
g[i].clear();
visit[i]=false;
ans = 0;
t = 0;
arr[i]=0;
tmin[i] = -1;
low[i] = -1;
}
}
void make_graph()
{
for(int i=1;i<=e;i++)
{
int x,y;
cin >> x >> y;
g[x].pb(y);
g[y].pb(x);
}
}
void solve()
{
reset();
make_graph();
for(int i=1;i<=v;i++)
{
if(!visit[i])
{
dfs(i);
}
}
for(int i=1;i<=v;i++)
{
if(arr[i])ans++;
}
cout<<ans<<"\n";
}
int32_t main()
{
while(1)
{
cin >> v >> e;
if(v == 0 && e == 0)return 0;
solve();
}
return 0;
}