File tree 1 file changed +51
-0
lines changed
1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .io .*;
2
+ import java .util .*;
3
+
4
+ public class Main {
5
+
6
+ public static int n ;
7
+ public static boolean [][] graph ;
8
+
9
+ public static void main (String [] args ) throws IOException {
10
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
11
+ n = Integer .parseInt (br .readLine ());
12
+ graph = new boolean [27 ][27 ];
13
+ for (int i =0 ; i <n ; i ++) {
14
+ StringTokenizer st = new StringTokenizer (br .readLine ());
15
+ int a = st .nextToken ().charAt (0 ) - 'a' ;
16
+ st .nextToken ();
17
+ int b = st .nextToken ().charAt (0 ) - 'a' ;
18
+ graph [a ][b ] = true ;
19
+ }
20
+ int m = Integer .parseInt (br .readLine ());
21
+ StringBuffer sb = new StringBuffer ();
22
+ for (int i =0 ; i <m ; i ++) {
23
+ StringTokenizer st = new StringTokenizer (br .readLine ());
24
+ int a = st .nextToken ().charAt (0 ) - 'a' ;
25
+ st .nextToken ();
26
+ int b = st .nextToken ().charAt (0 ) - 'a' ;
27
+ sb .append (bfs (a , b )).append ('\n' );
28
+ }
29
+ System .out .print (sb );
30
+ }
31
+
32
+ public static char bfs (int start , int end ) {
33
+ boolean [] visited = new boolean [27 ];
34
+ Queue <Integer > q = new LinkedList <>();
35
+ q .offer (start );
36
+ while (!q .isEmpty ()) {
37
+ int v = q .poll ();
38
+ if (v == end ) {
39
+ return 'T' ;
40
+ }
41
+
42
+ for (int i =0 ; i <27 ; i ++) {
43
+ if (graph [v ][i ] && !visited [i ]) {
44
+ visited [i ] = true ;
45
+ q .offer (i );
46
+ }
47
+ }
48
+ }
49
+ return 'F' ;
50
+ }
51
+ }
You can’t perform that action at this time.
0 commit comments