-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUva11504.java
More file actions
106 lines (95 loc) · 2.94 KB
/
Copy pathUva11504.java
File metadata and controls
106 lines (95 loc) · 2.94 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
101
102
103
104
105
106
import java.util.*;
import java.io.*;
public class Uva11504
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
StringBuilder sb = new StringBuilder();
int tc = Integer.parseInt(br.readLine());
while( tc-->0 ){
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken()) , m = Integer.parseInt(st.nextToken());
Graph g = new Graph(n);
for (int i = 0; i < m; i++) {
st = new StringTokenizer(br.readLine());
int u = Integer.parseInt(st.nextToken()) , v = Integer.parseInt(st.nextToken());
g.addEdge(u-1,v-1);
}
g.SCC();
int ans = g.count();
sb.append(ans).append('\n');
}
pw.print(sb.toString());
pw.close();
br.close();
}
static class Graph
{
int V ;
ArrayList<Integer> [] adjList ;
int time ;
int [] disc ;
int [] low ;
boolean [] stackMember ;
Stack<Integer> stack ;
int count ;
public Graph(int V)
{
this.V = V ;
adjList = new ArrayList[V];
for (int i = 0; i < V; i++) {
adjList[i] = new ArrayList<Integer>();
}
time = 0 ;
disc = new int[V] ;
low = new int[V];
stackMember = new boolean[V];
stack = new Stack<>();
Arrays.fill(disc,-1);
Arrays.fill(low,-1);
count = 0 ;
}
public int count()
{
return this.count ;
}
public void addEdge(int v , int w)
{
adjList[v].add(w);
}
public void SCC()
{
for (int i = 0; i < V; i++) {
if( disc[i] == -1 ){
count++ ;
SCCUtil(i,disc , low , stack , stackMember);
}
}
}
public void SCCUtil(int u , int[] disc , int [] low , Stack<Integer> stack , boolean [] stackMember)
{
disc[u] = low[u] = ++time ;
stack.push(u);
stackMember[u] = true ;
for(int v : adjList[u]){
if(disc[v]==-1){
SCCUtil(v,disc,low,stack,stackMember);
low[u] = Integer.min(low[u],low[v]);
} else if( stackMember[v] ){
low[u] = Integer.min(low[u],disc[v]);
}
}
int w ;
if( low[u] == disc[u] ){
while( stack.peek() != u ){
w = stack.pop();
stackMember[w] = false ;
}
w = stack.pop();
stackMember[w] = false ;
}
}
}
}