Advertisement
Guest User

Adjacency Matrix

a guest
Oct 19th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.text.*;
  4. import java.math.*;
  5. import static java.lang.System.*;
  6. import static java.lang.Integer.*;
  7. import static java.lang.Double.*;
  8. import static java.lang.Math.*;
  9.  
  10. public class AdjMatrix2
  11. {
  12.     int[][] adj = new int[10000][10000];
  13.     int[] sarr = new int[10000];
  14.     public void run() throws Exception
  15.     {
  16.         Scanner file = new Scanner(new File("maze2.dat"));
  17.  
  18.         int times = file.nextInt();
  19.         file.nextLine();
  20.        
  21.         for (int asdf = 0; asdf < times; asdf++)
  22.         {
  23.             int conn = file.nextInt();
  24.             file.nextLine();
  25.             ArrayList<Character> names = new ArrayList<Character>();
  26.             ArrayList<Character> names1 = new ArrayList<Character>();
  27.             for(int i = 0; i < conn; i++)
  28.             {
  29.                 char[] ca = file.nextLine().replaceAll(" ", "").toCharArray();
  30.                 names.add(ca[0]);
  31.                 names1.add(ca[1]);
  32.             }
  33.             for(int i = 0; i < names.size(); i++)
  34.             {
  35.                 adj[names.indexOf(names.get(i))][names1.indexOf(names1.get(i))] = 1;
  36.                 adj[names1.indexOf(names1.get(i))][names.indexOf(names.get(i))] = 1;
  37.             }
  38.             recur(0,0);
  39.             int test = file.nextInt();
  40.             file.nextLine();
  41.             for(int i = 0; i < test; i++)
  42.             {
  43.                 char[] c = file.nextLine().replaceAll(" " , "").toCharArray();
  44.                 System.out.println(
  45.                     adj[names.indexOf(c[0]) != -1? names.indexOf((char) c[0]) : 9999]
  46.                     [names1.indexOf(c[1]) != -1? names1.indexOf((char) c[1]) : 9999]  == 1 ? "YES" : "NO"
  47.                         );
  48.             }
  49.         }
  50.     }
  51.     public void recur(int i, int tot)
  52.     {
  53.         for(int j = 0; j < adj[i].length; j++)
  54.         {
  55.             if(adj[i][j] == 1 && sarr[i] > tot)
  56.             {
  57.                 sarr[i] = tot;
  58.                 recur(j,tot+1);
  59.             }
  60.         }
  61.     }
  62.     public static void main(String[] args) throws Exception
  63.     {
  64.  
  65.         new AdjMatrix2().run();
  66. //      data:
  67. //      1
  68. //      6
  69. //      ral steven
  70. //      steven govind
  71. //      tristan aayush
  72. //      sammy ral
  73. //      sammy mr.armstrong
  74. //      ral bad
  75. //      4
  76. //      steven ral
  77. //      aayush tristan
  78. //      govind steven
  79. //      sammy mr.armstrong
  80.        
  81. //      output
  82. //      YES
  83. //      NO
  84. //      NO
  85. //      YES
  86.  
  87. //      output should be
  88. //      YES
  89. //      YES
  90. //      YES
  91. //      YES    
  92.     }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement