a

import java.util.*;
import java.io.*;

public class WUGraph {
int numOfNodes;
ArrayList<Edge> list = new ArrayList<Edge>(); //隣接ノード、重さ//

class Edge implements Comparable<Edge> {
private int to; // 辺番号//
private int weight; //重さ//
private int nodeU, nodeV; //両端ノード//
Edge(int to,int nodeU, int nodeV ,int weight) {this.to = to; this.weight = weight; this.nodeU = nodeU; this.nodeV = nodeV;}
int To() { return to;}
int NodeU() { return nodeU;}
int NodeV() { return nodeV;}
int Weight() { return weight;}
@Override
public int compareTo(Edge e) {
return this.weight - e.weight;
}
}

ArrayList<Edge> getSortedEdges(){
ArrayList<Edge> al = new ArrayList<>(list);
Collections.sort(al);
return al;
}


void LoadGraph(String filename){
try{
Scanner scan = new Scanner(new File(filename));

String Line = scan.nextLine();
String r = Line.split(",",2); //頂点数、 辺の数//

numOfNodes = Integer.parseInt(r[0]);

for(int i = 0; i < Integer.parseInt(r[1]); i++){
String line = scan.nextLine();
String s = line.split(":",0);
String[] t = s[1].split(",",3);

Edge e = new Edge(Integer.parseInt(s[0]),Integer.parseInt(t[0]), Integer.parseInt(t[1]),Integer.parseInt(t[2]));
list.add(e);

}
} catch (java.io.FileNotFoundException e){
System.out.println(e);
System.exit(0);
}
}

ArrayList<Edge> getMST() {
ArrayList<Edge> mst = new ArrayList<Edge>();
ArrayList<Edge> selst = getSortedEdges();
UnionFind uf = new UnionFind(numOfNodes);
for (Edge e : selst) {
if (uf.unite(e.nodeU, e.nodeV)) {
mst.add(e);
}
}
return mst;
}

int getMSTcost() {
ArrayList<Edge> edges= getMST();
int cost = 0;
for (int i = 0; i < edges.size(); i++)
cost = cost + edges.get(i).Weight();
return cost;
}

void printGraph(){
ArrayList<Edge> edges= getMST();
System.out.println(getMSTcost());
System.out.println(numOfNodes + "," + edges.size());
for (int j = 0; j < edges.size() ; j++) {
System.out.println(edges.get(j).To() + ":" + edges.get(j).NodeU() + "," + edges.get(j).NodeV() + "," + edges.get(j).Weight());
}
}
}