Skip to content

Commit 3e8cc67

Browse files
authored
Merge pull request #75 from AlgorithmWithGod/Seol-JY
[20250211] BOJ / 골드5 / 파괴된 도시 / 설진영
2 parents 0af3013 + 50229ed commit 3e8cc67

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
```java
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
import java.util.StringTokenizer;
9+
10+
public class Main {
11+
private static int N;
12+
private static int[][] map;
13+
private static int[] burn;
14+
private static int[] mark;
15+
16+
public static void main(String[] args) throws IOException {
17+
18+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
19+
StringTokenizer st = new StringTokenizer(br.readLine());
20+
N = nextInt(st);
21+
map = new int[N+1][N+1];
22+
burn = new int[N+1];
23+
mark = new int[N+1];
24+
25+
int M = nextInt(st);
26+
27+
for (int i = 0; i < M; i++) {
28+
st = new StringTokenizer(br.readLine());
29+
int x = nextInt(st);
30+
int y = nextInt(st);
31+
32+
map[x][y] = 1;
33+
map[y][x] = 1;
34+
}
35+
36+
int T = Integer.parseInt(br.readLine());
37+
st = new StringTokenizer(br.readLine());
38+
for (int i = 0; i < T; i++) {
39+
int value = nextInt(st);
40+
burn[value] = 1;
41+
mark[value] = 1;
42+
}
43+
44+
List<Integer> answers = new ArrayList<>();
45+
for (int i = 1; i <= N; i++) {
46+
if (burn[i] == 1) {
47+
if (extinguish(i)) {
48+
answers.add(i);
49+
}
50+
}
51+
}
52+
53+
if (Arrays.stream(mark).sum() == 0) {
54+
System.out.println(answers.size());
55+
for (Integer a : answers) {
56+
System.out.print(a + " ");
57+
}
58+
59+
return;
60+
}
61+
62+
System.out.println(-1);
63+
}
64+
65+
private static boolean extinguish(int value) {
66+
for (int i = 1; i < N+1; i++) {
67+
if (i == value) continue;
68+
if (map[value][i] == 1 && burn[i] != 1) {
69+
return false;
70+
}
71+
}
72+
mark[value] = 0;
73+
for (int i =1; i < N+1; i++) {
74+
if (map[value][i] == 1) {
75+
mark[i] = 0;
76+
}
77+
}
78+
79+
return true;
80+
}
81+
82+
private static int nextInt(StringTokenizer st) {
83+
return Integer.parseInt(st.nextToken());
84+
}
85+
}
86+
```

0 commit comments

Comments
 (0)