-
Notifications
You must be signed in to change notification settings - Fork 1
/
315 - Network.cpp
141 lines (116 loc) · 2.5 KB
/
315 - Network.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*input
4
1 4 2 3
2 4
0
3
2 1 3
0
0
*/
#include <stdio.h>
#include <cmath>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string.h>
#include <stack>
#include <map>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <queue>
#include <functional>
using namespace std;
#define r(input) freopen("input.txt","r",stdin)
#define w(output) freopen("output.txt","w",stdout)
#define FOR(i, a, b) for ( int i = 0 ; i < a ; i+=b )
#define printArr(array, n) for(int i = 0 ; i < n ; i++) cout << array[i]<<" ";
#define FOREACH(l) for (auto it = l.begin(); it != l.end(); it++)
#define BitCount(i) __builtin_popcount(i)
#define Sort(v) sort(v.begin(),v.end())
#define cover(a, d) memset(a,d,sizeof(a))
#define sz() size()
#define pb push_back
#define VISITED 1
#define UNVISITED 0
#define INF 1000000000
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
inline int toInt(string s){int v; istringstream sin(s);sin>>v;return v;}
inline ll toll(string s){ll v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}
vi adj[105];
vi dfs_num4;
vi dfs_low;
vi dfs_parent2;
int dfsRoot;
int rootChildren;
int dfsCount ;
vector<bool> articulation_vertex;
void articulation_point(int u )
{
dfs_num4[u] = dfs_low[u] = ++dfsCount;
for ( int i = 0 ; i < adj[u].size(); i++)
{
int v = adj[u][i];
if ( dfs_num4[v] == UNVISITED)
{
dfs_parent2[v] = u;
if ( u == dfsRoot) rootChildren++;
articulation_point(v);
if (dfs_low[v] >= dfs_num4[u])
articulation_vertex[u] = true;
dfs_low[u] = min(dfs_low[v], dfs_low[u]);
}
else if(v != dfs_parent2[u] )
dfs_low[u] = min(dfs_low[u], dfs_num4[v]);
}
}
int main()
{
int N;
while (cin >> N)
{
if(!N) break;
cin.ignore();
FOR(i, N, 1)
adj[i].clear();
string line;
while(getline(cin, line))
{
stringstream ln (line);
int x;
ln >> x;
if ( x == 0) break;
int y;
while(ln>>y)
{
adj[x - 1].pb(y - 1);
adj[y - 1].pb(x - 1);
}
}
dfsCount = 0 ;
dfs_num4.assign(N, UNVISITED);
dfs_parent2.assign(N, 0);
dfs_low.assign(N, 0);
articulation_vertex.assign(N, 0);
for ( int i = 0 ; i < N ; i++)
{
if (dfs_num4[i] == UNVISITED)
{
dfsRoot = i;
rootChildren = 0;
articulation_point(i);
articulation_vertex[dfsRoot] = (rootChildren > 1);
}
}
int res = 0;
FOR(i, N, 1)
if(articulation_vertex[i]) res++;
cout << res << endl;
}
}