@icr0/

QuickHandsomeMacro

Java

No description

fork
loading
Files
  • Main.java
Main.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
import java.util.*;
class Main{
    public static ArrayList<Integer> path = new ArrayList<Integer>();
    public static HashSet<Integer> visited = new HashSet<Integer>();
    public static HashMap<Integer,String> intstr = new HashMap();
    public static HashMap<String,Integer> strint = new HashMap();
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        
        String to,from;
        int counter = 0;
        ArrayList[] graph = new ArrayList[n];
        for(int i = 0;i<n;i++){
            graph[i] = new ArrayList<Integer>();
        }
        for(int i = 0;i<n;i++){
            from = in.next();
            to = in.next();
            if(!intstr.containsKey(to)){
                intstr.put(counter,to);
                strint.put(to,counter);
                counter++;
            }
            if(!intstr.containsKey(from)){
                intstr.put(counter,from);
                strint.put(from,counter);
                counter++;
            }
            graph[strint.get(from)].add(strint.get(to));
            graph[strint.get(to)].add(strint.get(from));
        }
        dfs(strint.get(in.next()),strint.get(in.next()),graph);
    }
    public static void dfs(int s,int e,ArrayList[] graph){
        if(visited.contains(s)){
            path.remove(path.size()-1); //might cause problems
            return;
        }
        visited.add(s);
        if(s==e){
          for(int i = 0;i<path.size();i++){
            System.out.println(path.get(i)+" ");
          }
        }
        for(Integer next:(ArrayList<Integer>)graph[s]){
          path.add(next);
          dfs(next,e,graph);
        }
        System.out.println("no route");
    }
}