# Not a smart DFS solution, but it is clear

• ``````class Edge{
String from,to;
double weight;
public Edge(String a,String b, double w){
from = a;
to = b;
weight = w;
}
public String toString(){
return from+"->"+to+":"+weight;
}
public int hashCode(){
}
public boolean equals(Object b){
if(b instanceof Edge && toString().equals(b.toString())){
return true;
}
return false;
}
}
class EdgeWeightedGraph{

public void addEdge(String a,String b, double weight){
Edge one = new Edge(a,b,weight);
Edge two = new Edge(b,a,1.0/weight);
}else{
Set<Edge> s = new HashSet<Edge>();
}
}else{
Set<Edge> s = new HashSet<Edge>();
}
}
public boolean contains(String a){
}
public List<Edge> getPath(String a, String b){
Set<Edge> visited = new HashSet<Edge>();
List<Edge> path = new ArrayList<Edge>();
path = dfsGetPath(a,b,path,visited);
return path;
}
private List<Edge> dfsGetPath(String start,String end,List<Edge> path,Set<Edge> visited){
if(start.equals(end)){
//List<Edge> result = new ArrayList<Edge>();
return path;
}
List<Edge> r = dfsGetPath(e.to,end,path,visited);
if(r.size()>0){
return r;
//stop further processing
}
path.remove(e);//next loop starting from same path path
}
}
}
return new ArrayList<Edge>();
}
}
public class Solution {
public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
//prepare
double[] result = new double[queries.length];
EdgeWeightedGraph g = new EdgeWeightedGraph();
for(int i=0;i<values.length;i++){
}
for(int i=0;i<queries.length;i++){
if(queries[i][0].equals(queries[i][1])&&g.contains(queries[i][0])){
result[i]=1.0;
continue;
}
List<Edge> r = g.getPath(queries[i][0],queries[i][1]);
if(r.size()==0){
result[i]=-1.0;
}else{
result[i]=1;
for(Edge e:r){
result[i]*=e.weight;
}
}
}
return result;
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.