-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1986D.cpp
More file actions
68 lines (62 loc) · 1.69 KB
/
1986D.cpp
File metadata and controls
68 lines (62 loc) · 1.69 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
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
void connectedVertex(int **arr, int n) {
vector<vector<int>> adj(n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (arr[i][j]) adj[i].push_back(j);
vector<bool> visited(n), ap(n);
vector<int> disc(n), low(n), parent(n, -1);
int t = 0;
function<void(int)> dfs = [&](int u) {
visited[u] = true;
disc[u] = low[u] = ++t;
int children = 0;
for (int v : adj[u]) {
if (!visited[v]) {
children++;
parent[v] = u;
dfs(v);
low[u] = min(low[u], low[v]);
if (parent[u] == -1 && children > 1) ap[u] = true;
if (parent[u] != -1 && low[v] >= disc[u]) ap[u] = true;
} else if (v != parent[u]) {
low[u] = min(low[u], disc[v]);
}
}
};
for (int i = 0; i < n; i++)
if (!visited[i]) dfs(i);
vector<bool> isB(n);
for (int u = 0; u < n; u++)
if (ap[u])
for (int v : adj[u])
if (!ap[v])
isB[v] = true;
vector<int> res;
for (int i = 0; i < n; i++)
if (isB[i])
res.push_back(i);
sort(res.begin(), res.end());
for (int i = 0; i < (int)res.size(); i++) {
if (i) cout << ' ';
cout << res[i];
}
cout << '\n';
}
int main() {
int n;
cin >> n;
int **arr = new int*[n];
for (int i = 0; i < n; i++) {
arr[i] = new int[n];
for (int j = 0; j < n; j++) {
cin >> arr[i][j];
}
}
connectedVertex(arr, n);
return 0;
}