-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUva459.java
More file actions
61 lines (56 loc) · 1.66 KB
/
Uva459.java
File metadata and controls
61 lines (56 loc) · 1.66 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
import java.util.* ;
import java.io.* ;
/**
* uva 459 Graph Connectivity
* @author mostafa
*/
public class Uva459
{
static ArrayList<Integer> [] adjList ;
static boolean [] marked ;
public static void main(String[] args) throws IOException
{
// BufferedReader br = new BufferedReader(new FileReader("in.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pr = new PrintWriter(System.out);
StringBuilder sb = new StringBuilder();
int tc = Integer.parseInt(br.readLine());
br.readLine();
while(tc-->0)
{
int N = br.readLine().charAt(0)- 'A' + 1 ;
marked = new boolean [N];
adjList = new ArrayList[N];
for (int i = 0; i < N ; i++)
adjList[i] = new ArrayList<Integer>();
String str ;
while( ( str = br.readLine() ) != null && !str.equals("") )
addEdge( str.charAt(0)-'A' , str.charAt(1)-'A' );
int count = 0 ;
for (int i = 0; i < N ; i++)
if(!marked[i])
{
count++ ;
dfs(i);
}
if(tc==0)
sb.append(count+"\n");
else
sb.append(count+"\n\n");
}
pr.print(sb);
br.close();
pr.close();
}
public static void dfs(int s)
{
marked[s] = true ;
for(int w : adjList[s])
if(!marked[w]) dfs(w);
}
public static void addEdge(int u , int v)
{
adjList[u].add(v);
adjList[v].add(u);
}
}