Fr.java
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;
public class Fr {
public static void main
(String[] args
) { Scanner sc
= new Scanner
(System.
in);
while(sc.hasNextInt()){
int n = sc.nextInt();
int c = sc.nextInt();
Node[] node = new Node[n + 1];
ArrayList<Edge> edge = new ArrayList<Edge>();
for(int i = 0; i < n - 1; i++){
int u = sc.nextInt();
int v = sc.nextInt();
int w = sc.nextInt();
Edge e = new Edge();
if(node[u] == null){
node[u] = new Node();
}
if(node[v] == null){
node[v] = new Node();
}
e.a = node[v];
e.b = node[u];
e.cena = w;
node[u].hrany.add(e);
node[v].hrany.add(e);
}
System.
out.
println(node
[c
].
initiate()); }
}
}
class Node {
LinkedList<Edge> hrany;
public Node()
{
hrany = new LinkedList<Edge>();
}
int initiate(){
int res = 0;
for(Edge e: hrany){
if(e.a == this){
res += e.b.compute(e);
} else {
res += e.a.compute(e);
}
}
return res;
}
int compute(Edge edge){
if (hrany.size() == 1)
return edge.cena;
int res = 0;
for(Edge e: hrany){
if(e != edge)
{
if (this == e.a){
res += e.b.compute(e);
} else {
res += e.a.compute(e);
}
}
}
return Math.
min(res, edge.
cena); }
}
class Edge {
int cena;
Node a, b;
}