Go to diff to previous submission
import java.util.ArrayList; import java.util.Scanner; public class Fr { static Node [] strom; while (sc.hasNextInt()) { int n = sc.nextInt(); int c = sc.nextInt() - 1; sc.nextLine(); strom = new Node[n]; for(int i = 0; i < strom.length; i++){ strom[i] = new Node(i); } for (int i = 0; i < strom.length - 1; i++) { if(cil == c){ int temp = od; od = cil; cil = temp; } if(strom[cil].pred != null){ int temp = od; od = cil; cil = temp; } strom[cil].pred = strom[od]; strom[od].potomci.add(new Hrana(vaha, strom[cil])); } } } public static int projdiNasl(Node od, int vaha){ int celkovaVaha = 0; for (int i = 0; i < od.potomci.size(); i++) { celkovaVaha += Math.min(od.potomci.get(i).vaha, projdiNasl(od.potomci.get(i).cil, od.potomci.get(i).vaha)); } if(od.potomci.size()>0) return celkovaVaha; else } } class Node{ ArrayList<Hrana> potomci; int cislo; Node pred = null; public Node(int cislo){ this.cislo = cislo; potomci = new ArrayList<Hrana>(); } } class Hrana{ int vaha; Node cil; public Hrana(int vaha, Node cil){ this.vaha = vaha; this.cil = cil; } }
--- c5.s1045.cteam058.fr.java.0.Fr.java +++ c5.s1095.cteam058.fr.java.0.Fr.java @@ -27,9 +27,16 @@ String vstup = sc.nextLine(); String[] cesta = vstup.split(" "); - int od = Integer.parseInt(cesta[0]); - int cil = Integer.parseInt(cesta[1]); + int od = Integer.parseInt(cesta[0]) - 1; + int cil = Integer.parseInt(cesta[1]) - 1; int vaha = Integer.parseInt(cesta[2]); - if(cil - 1 == c){ + + if(cil == c){ + int temp = od; + od = cil; + cil = temp; + } + + if(strom[cil].pred != null){ int temp = od; od = cil; @@ -37,6 +44,8 @@ } + + strom[cil].pred = strom[od]; + strom[od].potomci.add(new Hrana(vaha, strom[cil])); - strom[od - 1].potomci.add(new Hrana(vaha, strom[cil - 1])); } @@ -66,4 +75,5 @@ ArrayList<Hrana> potomci; int cislo; + Node pred = null; public Node(int cislo){ @@ -78,4 +88,5 @@ Node cil; + public Hrana(int vaha, Node cil){ this.vaha = vaha;