File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change 1+ package 백준
2+ private val arr= arrayListOf<Triple <Int ,Int ,Int >>()
3+ private lateinit var parent: IntArray
4+ private lateinit var size: LongArray
5+ fun main () {
6+ val br= System .`in `.bufferedReader()
7+ val (n,m)= br.readLine().split(" " ).map { it.toInt() }
8+ parent= IntArray (n+ 1 ){it}
9+ size= LongArray (n+ 1 ){1 }
10+ var costsum: Long = 0
11+ repeat(m) {
12+ val (a,b,c)= br.readLine().split(" " ).map { it.toInt() }
13+ arr.add(Triple (a,b,c))
14+ costsum+ = c
15+ }
16+ arr.sortByDescending{ it.third }
17+ var ans: Long = 0
18+ for (i in arr){
19+ if (find(i.first)!= find(i.second)){
20+ ans+ = ((((size[find(i.first)]* size[find(i.second)])% 1000000000 )* costsum))
21+ ans% = 1000000000
22+ union(i.first,i.second)
23+ }
24+ costsum- = i.third
25+ }
26+ println (ans)
27+ }
28+ private fun find (x : Int ):Int {
29+ return when (x) {
30+ parent[x] -> {
31+ x
32+ }
33+ else -> {
34+ parent[x] = find(parent[x])
35+ parent[x]
36+ }
37+ }
38+ }
39+ private fun union (x : Int ,y : Int ){
40+ val (px,py)= arrayOf(find(x), find(y))
41+ when {
42+ px> py -> {
43+ parent[px]= py
44+ size[py]+ = size[px]
45+ size[px]= 1
46+ }
47+ else -> {
48+ parent[py]= px
49+ size[px]+ = size[py]
50+ size[py]= 1
51+ }
52+ }
53+ }
You can’t perform that action at this time.
0 commit comments