-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution3124.java
More file actions
105 lines (85 loc) · 2.12 KB
/
Solution3124.java
File metadata and controls
105 lines (85 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
class Edge implements Comparable<Edge>{
int a;
int b;
int w;
public Edge(int a, int b, int w) {
super();
this.a = a;
this.b = b;
this.w = w;
}
@Override
public int compareTo(Edge o) {
return this.w-o.w;
}
}
public class Solution3124 {
static int[]p;
static int[]rank;
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(bf.readLine());
for(int tc=1;tc<=T;tc++) {
StringTokenizer st = new StringTokenizer(bf.readLine()," ");
int V = Integer.parseInt(st.nextToken());//정점
int E = Integer.parseInt(st.nextToken()); //간선
p = new int[V+1];
rank = new int[V+1];
Edge[] ed = new Edge[E];
for(int i=0;i<E;i++) {
st = new StringTokenizer(bf.readLine()," ");
ed[i]=new Edge(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()));
}
Arrays.sort(ed);
for(int i=0;i<p.length;i++) {
makeSet(i);
}
long ans = 0;
//가중치 낮은 간선부터 선택하기
for(int i=0;i<ed.length;i++) {
Edge e = ed[i];
if(findSet(e.a)!=findSet(e.b)) {
union(e.a,e.b);
ans += e.w;
}
}
System.out.println("#"+tc+" "+ans);
}//end of tc
}
//새로운 집합 생성
public static void makeSet(int x) {
p[x]=x;
}
//x가 포함된 그룹의 대표자 리턴
public static int findSet(int x) {
if(x==p[x]) return x;
else {
p[x]=findSet(p[x]);
return p[x];
}
}
public static void union(int x, int y) {
int px=findSet(x);
int py=findSet(y);
//서로 다른 집합일 경우만 합친다.
if(px!=py) {
link(px,py);
}
}
//px, py가 대표로 있는 집합을 합치고 rank 맞추기
public static void link(int px, int py) {
if(rank[px]>rank[py]) {
p[py] = px;
} else {
p[px] = py;
if(rank[px]==rank[py]) {
rank[py]++;
}
}
}
}