Skip to content

Commit bc8a2ce

Browse files
authored
[20250321] BOJ / G4 / Bi-coloring / 권혁준
1 parent 9bf0a52 commit bc8a2ce

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
class Main {
7+
8+
// IO field
9+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
11+
static StringTokenizer st = new StringTokenizer("");
12+
13+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
14+
static String nextToken() throws Exception {
15+
while(!st.hasMoreTokens()) nextLine();
16+
return st.nextToken();
17+
}
18+
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
19+
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
20+
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
21+
static void bwEnd() throws Exception {bw.flush();bw.close();}
22+
23+
// Additional field
24+
25+
static int N, M;
26+
static boolean[][] E;
27+
static int[] C;
28+
29+
public static void main(String[] args) throws Exception {
30+
for(int T=nextInt();T-->0;) {
31+
32+
ready();
33+
solve();
34+
}
35+
36+
bwEnd();
37+
38+
}
39+
40+
static void ready() throws Exception{
41+
42+
N = nextInt();
43+
M = nextInt();
44+
E = new boolean[N][N];
45+
C = new int[N];
46+
for(int i=0;i<M;i++) {
47+
int a = nextInt(), b = nextInt();
48+
E[a][b] = true;
49+
E[b][a] = true;
50+
}
51+
52+
}
53+
54+
static void solve() throws Exception{
55+
56+
int ans = 1;
57+
for(int i=0;i<N;i++) if(C[i] == 0) {
58+
C[i] = 1;
59+
if(dfs(i,1)) ans<<=1;
60+
else {
61+
bw.write("-1\n");
62+
return;
63+
}
64+
}
65+
bw.write(ans + "\n");
66+
67+
}
68+
69+
static boolean dfs(int n, int c) {
70+
boolean res = true;
71+
for(int i=0;i<N;i++) if(E[n][i]) {
72+
if(C[i] == 0) {
73+
C[i] = c^2;
74+
res &= dfs(i,c^2);
75+
}
76+
else if(C[i] == c) res = false;
77+
}
78+
return res;
79+
}
80+
81+
}
82+
83+
```

0 commit comments

Comments
 (0)