-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUva315.java
More file actions
100 lines (93 loc) · 2.93 KB
/
Uva315.java
File metadata and controls
100 lines (93 loc) · 2.93 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
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
import java.util.*;
import java.io.*;
/*
* Uva 315 - Network
* @ author Mostafa Kamel
* Description : John Edward Hopcroft and Robert Endre Tarjan to compute Articulation point
*/
public class Uva315
{
static ArrayList<Integer> []adjList ;
static boolean [] marked ;
static int [] parent ;
static boolean [] isArticulationPoint ;
static int [] dfs_low ;
static int [] dfs_num ;
static int counter ;
static int root ;
static int rootChildren ;
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pr = new PrintWriter(System.out);
StringBuilder sb = new StringBuilder();
int n = Integer.parseInt(br.readLine());
while (n!=0)
{
marked = new boolean[n];
parent = new int[n];
isArticulationPoint = new boolean[n];
dfs_low = new int[n];
dfs_num = new int[n];
adjList = (ArrayList<Integer>[]) new ArrayList[n];
for (int i = 0; i < n; i++)
adjList[i] = new ArrayList<Integer>();
StringTokenizer st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken())-1;
while(x!=-1)
{
while (st.hasMoreTokens())
{
int y = Integer.parseInt(st.nextToken())-1;
adjList[x].add(y);
adjList[y].add(x);
}
st = new StringTokenizer(br.readLine());
x = Integer.parseInt(st.nextToken())-1;
}
for (int i = 0; i < n; i++) {
if(!marked[i])
{
root = i ;
rootChildren = 0 ;
marked[i] = true ;
dfs(i);
if(rootChildren <= 1)
isArticulationPoint[root] =false ;
}
}
int sum = 0 ;
for (boolean b : isArticulationPoint ) {
if(b) sum++ ;
}
sb.append(sum).append("\n");
n = Integer.parseInt(br.readLine());
}
pr.print(sb.toString());
br.close();
pr.close();
}
static void dfs(int u)
{
marked[u] = true ;
dfs_num[u] = dfs_low[u] = counter++ ;
for (int w :adjList[u])
{
if(!marked[w])
{
parent[w] = u ;
if(u==root)
rootChildren++ ;
dfs(w);
if(dfs_low[w] >= dfs_num[u])
isArticulationPoint[u] = true ;
dfs_low[u] = Integer.min(dfs_low[w],dfs_low[u]);
}
else
{
if(parent[u] != w)
dfs_low[u] = Integer.min(dfs_low[u] , dfs_num[w]);
}
}
}
}