forked from improper4/uva
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUVa00247_CallingCircles.java
116 lines (94 loc) · 2.38 KB
/
UVa00247_CallingCircles.java
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
package uva;
/* USER: 46724 (sfmunera) */
/* PROBLEM: 183 (247 - Calling Circles) */
/* SUBMISSION: 09368237 */
/* SUBMISSION TIME: 2011-10-13 23:00:04 */
/* LANGUAGE: 2 */
import java.util.*;
import java.io.*;
public class UVa00247_CallingCircles {
static int N;
static boolean[][] adj;
static boolean[] visited;
static List<Integer> sort;
static Map<Integer, String> map;
static void dfs(int s) {
visited[s] = true;
for (int i = 0; i < N; ++i)
if (adj[s][i] && !visited[i])
dfs(i);
sort.add(0, s);
}
static void topsort() {
visited = new boolean[N];
sort = new ArrayList<Integer>();
for (int i = 0; i < N; ++i)
if (!visited[i])
dfs(i);
}
static void dfs2(int s, boolean first) {
visited[s] = true;
if (!first)
System.out.print(", ");
System.out.print(map.get(s));
for (int i = 0; i < N; ++i)
if (adj[i][s] && !visited[i])
dfs2(i, false);
}
static void scc() {
topsort();
visited = new boolean[N];
for (int x : sort)
if (!visited[x]) {
dfs2(x, true);
System.out.println();
}
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer stk;
int t = 1;
boolean first = true;
while (true) {
stk = new StringTokenizer(in.readLine());
N = Integer.parseInt(stk.nextToken());
int M = Integer.parseInt(stk.nextToken());
if (N == 0 && M == 0)
break;
adj = new boolean[N][N];
map = new HashMap<Integer, String>();
int k = 0;
for (int i = 0; i < M; ++i) {
stk = new StringTokenizer(in.readLine());
String from = stk.nextToken();
String to = stk.nextToken();
if (!map.containsValue(from))
map.put(k++, from);
if (!map.containsValue(to))
map.put(k++, to);
int v = 0;
int w = 0;
for (int x : map.keySet()) {
if (map.get(x).equals(from)) {
v = x;
//System.out.println(v + ": " + from);
}
if (map.get(x).equals(to)) {
w = x;
//System.out.println(w + ": " + to);
}
}
adj[v][w] = true;
}
if (first)
first = false;
else
System.out.println();
System.out.println("Calling circles for data set " + t + ":");
scc();
++t;
}
in.close();
System.exit(0);
}
}