Skip to content

Commit c701860

Browse files
beberichetony9402
andauthored
[ADD] baekjoon 13265 java solution (#83)
* [ADD] baekjoon 13265 java solution * Refactoring --------- Co-authored-by: Minsang Kim <[email protected]>
1 parent 34553b6 commit c701860

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

solutions/baekjoon/13265/Main.java

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// Authored by: beberiche
2+
// Co-authored by: -
3+
// Link: http://boj.kr/a548c2ea425541dd82f89145600468f1
4+
5+
import java.util.*;
6+
import java.io.*;
7+
8+
public class Main {
9+
public static void main(String[] args) {
10+
FastReader rd = new FastReader();
11+
int T = rd.nextInt();
12+
13+
StringBuffer sb = new StringBuffer();
14+
while (--T >= 0) {
15+
int N = rd.nextInt();
16+
int M = rd.nextInt();
17+
18+
List<Integer> list[] = new ArrayList[N + 1];
19+
for (int i = 1; i <= N; i++) {
20+
list[i] = new ArrayList<>();
21+
}
22+
23+
for (int i = 0; i < M; i++) {
24+
int n1 = rd.nextInt();
25+
int n2 = rd.nextInt();
26+
27+
list[n1].add(n2);
28+
list[n2].add(n1);
29+
}
30+
31+
32+
int[] g = new int[N + 1];
33+
boolean check = true;
34+
35+
for (int i = 1; i <= N; i++) {
36+
if (g[i] == 0) {
37+
check = bfs(i, g, list);
38+
}
39+
if (!check) break;
40+
}
41+
42+
if (!check) sb.append("im");
43+
sb.append("possible").append("\n");
44+
}
45+
System.out.print(sb.toString());
46+
}
47+
48+
private static boolean bfs(int st, int[] g, List<Integer> list[]) {
49+
Queue<Integer> q = new LinkedList<>();
50+
g[st] = 1;
51+
q.add(st);
52+
53+
while (!q.isEmpty()) {
54+
int curr = q.poll();
55+
56+
for (int next : list[curr]) {
57+
if (g[next] == g[curr]) return false;
58+
59+
if (g[next] == 0) {
60+
g[next] = g[curr] * -1;
61+
q.add(next);
62+
}
63+
}
64+
}
65+
66+
return true;
67+
}
68+
69+
static class FastReader {
70+
BufferedReader br;
71+
StringTokenizer st;
72+
73+
public FastReader() {
74+
br = new BufferedReader(new InputStreamReader(System.in));
75+
}
76+
77+
String next() {
78+
while (st == null || !st.hasMoreElements()) {
79+
try {
80+
st = new StringTokenizer(br.readLine());
81+
} catch (IOException e) {
82+
e.printStackTrace();
83+
}
84+
}
85+
return st.nextToken();
86+
}
87+
88+
int nextInt() {
89+
return Integer.parseInt(next());
90+
}
91+
92+
long nextLong() {
93+
return Long.parseLong(next());
94+
}
95+
96+
double nextDouble() {
97+
return Double.parseDouble(next());
98+
}
99+
100+
String nextLine() {
101+
String str = "";
102+
try {
103+
str = br.readLine();
104+
} catch (IOException e) {
105+
e.printStackTrace();
106+
}
107+
return str;
108+
}
109+
}
110+
}
111+
112+
/* Solution Description
113+
114+
1. 그래프 응용 문제. 주어진 입력값을 통해 그래프를 형성하여 탐색했을 때,
115+
인접 노드끼리 색이 다른지 확인하는 방식으로 문제를 풀 수 있다.
116+
117+
2. 해당 풀이의 경우 색의 상태값을 `0,1,-1` 로 정했다.
118+
`0` 은 색이 칠해지지 않은 경우, `1,-1` 은 각각 색을 칠한 경우이며 색이 서로 다르다고 가정했다.
119+
만약 임의의 노드의 색을 `c` 라고 한다면, 다음 이동해야할 노드의 색은 `c*-1` 이어야 한다.
120+
121+
3. 그래프 탐색 중, 현재 노드와 다음 노드의 색이 동일한 경우가 발생한다면 `impossible` 을,
122+
탐색이 끝날때 까지 발생하지 않는다면 `possible` 을 출력한다.
123+
124+
*/

0 commit comments

Comments
 (0)