https://www.acmicpc.net/problem/30797
Edge
클래스 정의n-1
개 간선 채택import static java.lang.Integer.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main {
static int[] parent;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = parseInt(st.nextToken());
int Q = parseInt(st.nextToken());
parent = new int[n + 1];
Arrays.fill(parent, -1);
PriorityQueue<Edge> pq = new PriorityQueue<>((p1, p2) -> {
if (p1.w == p2.w) {
return Integer.compare(p1.t, p2.t);
}
return Integer.compare(p1.w, p2.w);
});
while (Q-- > 0) {
st = new StringTokenizer(br.readLine());
int u = parseInt(st.nextToken());
int v = parseInt(st.nextToken());
int w = parseInt(st.nextToken());
int t = parseInt(st.nextToken());
pq.add(new Edge(u, v, w, t));
}
int count = 0;
long sum = 0;
int endTime = -1;
while (count < n - 1 && !pq.isEmpty()) {
Edge e = pq.poll();
int p1 = find(e.u);
int p2 = find(e.v);
if (p1 == p2) {
continue;
}
if (parent[p1] < parent[p2]) {
parent[p1] += parent[p2];
parent[p2] = p1;
} else {
parent[p2] += parent[p1];
parent[p1] = p2;
}
count++;
sum += e.w;
endTime = Math.max(endTime, e.t);
}
String answer = endTime + " " + sum;
System.out.println(count != n - 1 ? -1 : answer);
br.close();
}
static int find(int u) {
if (parent[u] < 0) {
return u;
}
return parent[u] = find(parent[u]);
}
static class Edge {
int u, v, w, t;
public Edge(int u, int v, int w, int t) {
this.u = u;
this.v = v;
this.w = w;
this.t = t;
}
}
}