Skip to content

Commit 34d7c44

Browse files
authored
[20250804] BOJ / G3 / 전국시대 / 이종환
1 parent b335980 commit 34d7c44

File tree

1 file changed

+137
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)