Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- import java.text.*;
- import java.math.*;
- import static java.lang.System.*;
- import static java.lang.Integer.*;
- import static java.lang.Double.*;
- import static java.lang.Math.*;
- public class AdjMatrix2
- {
- int[][] adj = new int[10000][10000];
- int[] sarr = new int[10000];
- public void run() throws Exception
- {
- Scanner file = new Scanner(new File("maze2.dat"));
- int times = file.nextInt();
- file.nextLine();
- for (int asdf = 0; asdf < times; asdf++)
- {
- int conn = file.nextInt();
- file.nextLine();
- ArrayList<Character> names = new ArrayList<Character>();
- ArrayList<Character> names1 = new ArrayList<Character>();
- for(int i = 0; i < conn; i++)
- {
- char[] ca = file.nextLine().replaceAll(" ", "").toCharArray();
- names.add(ca[0]);
- names1.add(ca[1]);
- }
- for(int i = 0; i < names.size(); i++)
- {
- adj[names.indexOf(names.get(i))][names1.indexOf(names1.get(i))] = 1;
- adj[names1.indexOf(names1.get(i))][names.indexOf(names.get(i))] = 1;
- }
- recur(0,0);
- int test = file.nextInt();
- file.nextLine();
- for(int i = 0; i < test; i++)
- {
- char[] c = file.nextLine().replaceAll(" " , "").toCharArray();
- System.out.println(
- adj[names.indexOf(c[0]) != -1? names.indexOf((char) c[0]) : 9999]
- [names1.indexOf(c[1]) != -1? names1.indexOf((char) c[1]) : 9999] == 1 ? "YES" : "NO"
- );
- }
- }
- }
- public void recur(int i, int tot)
- {
- for(int j = 0; j < adj[i].length; j++)
- {
- if(adj[i][j] == 1 && sarr[i] > tot)
- {
- sarr[i] = tot;
- recur(j,tot+1);
- }
- }
- }
- public static void main(String[] args) throws Exception
- {
- new AdjMatrix2().run();
- // data:
- // 1
- // 6
- // ral steven
- // steven govind
- // tristan aayush
- // sammy ral
- // sammy mr.armstrong
- // ral bad
- // 4
- // steven ral
- // aayush tristan
- // govind steven
- // sammy mr.armstrong
- // output
- // YES
- // NO
- // NO
- // YES
- // output should be
- // YES
- // YES
- // YES
- // YES
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement