Skip to content

Commit 0692077

Browse files
authored
[20250805] BOJ / G5 / 소수 화폐 / 이종환
1 parent 34d7c44 commit 0692077

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
```java
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.util.*;
6+
7+
8+
public class Main {
9+
10+
static final int WAR = 2;
11+
static final int UNION = 1;
12+
13+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
static StringBuilder sb = new StringBuilder();
15+
static int[] countryPower,parent;
16+
static boolean[] isAlive;
17+
static int countryCnt, recordCnt,ans;
18+
static List<Integer> ansList = new ArrayList<>();
19+
20+
public static void main(String[] args) throws IOException {
21+
init();
22+
process();
23+
print();
24+
25+
}
26+
27+
private static void init() throws IOException {
28+
StringTokenizer st = new StringTokenizer(br.readLine());
29+
countryCnt = Integer.parseInt(st.nextToken());
30+
recordCnt = Integer.parseInt(st.nextToken());
31+
countryPower = new int[countryCnt];
32+
parent = new int[countryCnt];
33+
isAlive = new boolean[countryCnt];
34+
Arrays.fill(isAlive, true);
35+
36+
for (int i = 0; i < countryCnt; i++) {
37+
countryPower[i] = Integer.parseInt(br.readLine());
38+
parent[i] = i;
39+
}
40+
}
41+
42+
private static void process() throws IOException {
43+
for (int i = 0; i < recordCnt; i++) {
44+
StringTokenizer st = new StringTokenizer(br.readLine());
45+
int order = Integer.parseInt(st.nextToken());
46+
int a = Integer.parseInt(st.nextToken())-1;
47+
int b = Integer.parseInt(st.nextToken())-1;
48+
49+
if (order == UNION) {
50+
union(a, b);
51+
} else{
52+
war(a,b);
53+
}
54+
}
55+
56+
for (int i = 0; i < countryCnt; i++) {
57+
if (!isAlive[i]) continue;
58+
int iRoot = find(i);
59+
if (iRoot != i) continue;
60+
if (isAlive[iRoot]) {
61+
ans++;
62+
ansList.add(countryPower[iRoot]);
63+
}
64+
}
65+
66+
67+
68+
}
69+
70+
private static void war(int a, int b) {
71+
int aRoot = find(a);
72+
int bRoot = find(b);
73+
74+
if (aRoot == bRoot) return;
75+
76+
int aPower = countryPower[aRoot];
77+
int bPower = countryPower[bRoot];
78+
if (aPower > bPower) {
79+
countryPower[aRoot] -= bPower;
80+
countryPower[bRoot] = 0;
81+
parent[bRoot] = aRoot;
82+
isAlive[bRoot] = false;
83+
} else if (aPower < bPower) {
84+
countryPower[bRoot] -= aPower;
85+
countryPower[aRoot] = 0;
86+
parent[aRoot] = bRoot;
87+
isAlive[aRoot] = false;
88+
} else{
89+
countryPower[aRoot] = 0;
90+
countryPower[bRoot] = 0;
91+
isAlive[aRoot] = false;
92+
isAlive[bRoot] = false;
93+
}
94+
95+
96+
}
97+
98+
private static void union(int a, int b) {
99+
int aRoot = find(a);
100+
int bRoot = find(b);
101+
102+
if (aRoot == bRoot) return;
103+
104+
int aPower = countryPower[aRoot];
105+
int bPower = countryPower[bRoot];
106+
107+
if (aPower >= bPower) {
108+
countryPower[aRoot] += bPower;
109+
countryPower[bRoot] = 0;
110+
parent[bRoot] = aRoot;
111+
isAlive[bRoot] = false;
112+
} else {
113+
countryPower[bRoot] += aPower;
114+
countryPower[aRoot] = 0;
115+
parent[aRoot] = bRoot;
116+
isAlive[aRoot] = false;
117+
}
118+
119+
}
120+
121+
private static int find(int x){
122+
if ( parent[x] == x ) return x;
123+
return parent[x] = find(parent[x]);
124+
}
125+
126+
private static void print() {
127+
Collections.sort(ansList);
128+
System.out.println(ansList.size());
129+
if (!ansList.isEmpty()) {
130+
for (int i = 0; i < ansList.size(); i++) {
131+
System.out.print(ansList.get(i));
132+
if (i < ansList.size() - 1) System.out.print(" ");
133+
}
134+
System.out.println();
135+
}
136+
}
137+
}
138+
```

0 commit comments

Comments
 (0)