Skip to content

Commit 7dc3aa7

Browse files
authored
Merge pull request #568 from AlgorithmWithGod/khj20006
[20250729] BOJ / P5 / 별자리 / 권혁준
2 parents 3407739 + 192de62 commit 7dc3aa7

File tree

1 file changed

+215
-0
lines changed

1 file changed

+215
-0
lines changed
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
enum Status {
51+
BEFORE, LINE, CIRCLE, AFTER
52+
}
53+
54+
public class Main {
55+
56+
static IOController io;
57+
58+
//
59+
60+
static int N, M;
61+
static int[][] edges;
62+
static int[] r;
63+
static Status[] s;
64+
static List<Integer>[] graph;
65+
66+
public static void main(String[] args) throws Exception {
67+
68+
io = new IOController();
69+
70+
init();
71+
solve();
72+
73+
io.close();
74+
75+
}
76+
77+
static int f(int x) {return x==r[x] ? x : (r[x]=f(r[x]));}
78+
79+
static void init() throws Exception {
80+
81+
N = io.nextInt();
82+
M = io.nextInt();
83+
edges = new int[M][];
84+
for(int i=0;i<M;i++) edges[i] = new int[]{io.nextInt(),io.nextInt(),io.nextInt()};
85+
r = new int[N+1];
86+
s = new Status[N+1];
87+
graph = new List[N+1];
88+
for(int i=1;i<=N;i++) {
89+
r[i] = i;
90+
s[i] = Status.BEFORE;
91+
graph[i] = new ArrayList<>();
92+
}
93+
94+
}
95+
96+
static void solve() throws Exception {
97+
98+
Arrays.sort(edges, (a,b) -> b[2]-a[2]);
99+
100+
int ans = edges[0][2], val = Integer.MAX_VALUE, idx = 0;
101+
int lines = 0, circles = 0;
102+
// weight sweeping
103+
while(idx < M) {
104+
int currentWeight = edges[idx][2];
105+
while(idx < M && edges[idx][2] == currentWeight) {
106+
int a = edges[idx][0], b = edges[idx][1];
107+
int x = f(a), y = f(b);
108+
if(x == y) {
109+
if(s[x] == Status.CIRCLE) {
110+
circles--;
111+
s[x] = Status.AFTER;
112+
}
113+
else if(s[x] == Status.LINE) {
114+
lines--;
115+
if(graph[a].size() > 1 || graph[b].size() > 1) {
116+
s[x] = Status.AFTER;
117+
}
118+
else {
119+
circles++;
120+
s[x] = Status.CIRCLE;
121+
}
122+
}
123+
}
124+
else {
125+
if(s[x] == Status.BEFORE) {
126+
r[x] = y;
127+
if(s[y] == Status.BEFORE) {
128+
lines++;
129+
s[y] = Status.LINE;
130+
}
131+
else if(s[y] == Status.LINE) {
132+
if(graph[b].size() > 1) {
133+
lines--;
134+
s[y] = Status.AFTER;
135+
}
136+
}
137+
else if(s[y] == Status.CIRCLE) {
138+
circles--;
139+
s[y] = Status.AFTER;
140+
}
141+
}
142+
else if(s[x] == Status.LINE) {
143+
r[y] = x;
144+
if(s[y] == Status.BEFORE) {
145+
if(graph[a].size() > 1) {
146+
lines--;
147+
s[x] = Status.AFTER;
148+
}
149+
}
150+
else if(s[y] == Status.LINE) {
151+
lines--;
152+
if(graph[a].size() > 1 || graph[b].size() > 1) {
153+
lines--;
154+
s[x] = Status.AFTER;
155+
}
156+
}
157+
else if(s[y] == Status.CIRCLE) {
158+
circles--;
159+
lines--;
160+
s[x] = Status.AFTER;
161+
}
162+
else {
163+
lines--;
164+
s[x] = Status.AFTER;
165+
}
166+
}
167+
else if(s[x] == Status.CIRCLE) {
168+
r[y] = x;
169+
if(s[y] == Status.BEFORE) {
170+
circles--;
171+
s[x] = Status.AFTER;
172+
}
173+
else if(s[y] == Status.LINE) {
174+
lines--;
175+
circles--;
176+
s[x] = Status.AFTER;
177+
}
178+
else if(s[y] == Status.CIRCLE) {
179+
circles-=2;
180+
s[x] = Status.AFTER;
181+
}
182+
else {
183+
circles--;
184+
s[x] = Status.AFTER;
185+
}
186+
}
187+
else {
188+
r[y] = x;
189+
if(s[y] == Status.LINE) {
190+
lines--;
191+
}
192+
else if(s[y] == Status.CIRCLE) {
193+
circles--;
194+
}
195+
}
196+
graph[a].add(b);
197+
graph[b].add(a);
198+
}
199+
idx++;
200+
}
201+
202+
int res = Math.abs(lines-circles);
203+
if(res <= val) {
204+
ans = currentWeight;
205+
val = res;
206+
}
207+
208+
}
209+
210+
io.write(ans + " " + val);
211+
212+
}
213+
214+
}
215+
```

0 commit comments

Comments
 (0)