-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay 183.java
More file actions
73 lines (52 loc) · 1.91 KB
/
Day 183.java
File metadata and controls
73 lines (52 loc) · 1.91 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
import java.util.*;
class Solution {
static int time;
static void dfs(int u, int parent, ArrayList<ArrayList<Integer>> adj,
boolean[] visited, int[] disc, int[] low, boolean[] isAP) {
visited[u] = true;
disc[u] = low[u] = ++time;
int children = 0;
for (int v : adj.get(u)) {
if (v == parent) continue;
if (!visited[v]) {
children++;
dfs(v, u, adj, visited, disc, low, isAP);
low[u] = Math.min(low[u], low[v]);
if (parent == -1 && children > 1) {
isAP[u] = true;
}
if (parent != -1 && low[v] >= disc[u]) {
isAP[u] = true;
}
} else {
low[u] = Math.min(low[u], disc[v]);
}
}
}
static ArrayList<Integer> articulationPoints(int V, int[][] edges) {
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
for (int i = 0; i < V; i++) adj.add(new ArrayList<>());
for (int[] e : edges) {
adj.get(e[0]).add(e[1]);
adj.get(e[1]).add(e[0]);
}
boolean[] visited = new boolean[V];
int[] disc = new int[V];
int[] low = new int[V];
boolean[] isAP = new boolean[V];
time = 0;
for (int i = 0; i < V; i++) {
if (!visited[i]) {
dfs(i, -1, adj, visited, disc, low, isAP);
}
}
ArrayList<Integer> result = new ArrayList<>();
for (int i = 0; i < V; i++) {
if (isAP[i]) result.add(i);
}
if (result.size() == 0) {
result.add(-1);
}
return result;
}
}