Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solved 이분 그래프 - 836ms 271424KB #243

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions Baekjoon/이분그래프/이분그래프_강정훈.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import java.io.*;
import java.util.*;

class Main {

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int K = Integer.parseInt(br.readLine());
for (int T = 0; T < K; T++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int V = Integer.parseInt(st.nextToken());
int E = Integer.parseInt(st.nextToken());
List<Integer>[] graph = new ArrayList[V + 1];
for (int index = 0; index < V + 1; index++) {
graph[index] = new ArrayList<>();
}
for (int index = 0; index < E; index++) {
st = new StringTokenizer(br.readLine());
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());
graph[start].add(end);
graph[end].add(start);
}

if (isBinaryGraph(graph, V)) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}

private static boolean isBinaryGraph(List<Integer>[] graph, int V) {
int[] groups = new int[V + 1];
for (int initialNode = 1; initialNode <= V; initialNode++) {
if (groups[initialNode] != 0) {
continue;
}
Queue<Integer> q = new ArrayDeque<>();
groups[initialNode] = 1;
q.add(initialNode);
while (!q.isEmpty()) {
int current = q.poll();
for (int nextIndex : graph[current]) {
if (groups[current] == groups[nextIndex]) {
return false;
}
if (groups[nextIndex] == 0) {
groups[nextIndex] = groups[current] * (-1);
q.add(nextIndex);
}
}
}
}
return true;
}
}