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 이분 그래프 - 2436ms 317.536mb #244

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
60 changes: 60 additions & 0 deletions Baekjoon/이분그래프/이분그래프_옥수빈.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import java.util.*;

public class Main {

static List<Integer>[] connect;
static int[] group;

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int K = sc.nextInt();
for (int i = 0; i < K; i++) {
int V = sc.nextInt();
int E = sc.nextInt();
connect = new List[V + 1];
group = new int[V + 1];
for (int k = 0; k <= V; k++) {
connect[k] = new ArrayList<>();
}
for (int j = 0; j < E; j++) {
int start = sc.nextInt();
int end = sc.nextInt();
connect[start].add(end);
connect[end].add(start);
}
boolean answer = true;
for (int j = 1; j <= V; j++) {
if (group[j] == 0) {
if (!BFS(j)) {
answer = false;
break;
}
}
}
if (answer)
System.out.println("YES");
else
System.out.println("NO");
}
}

static boolean BFS(int n) {
boolean[] visited = new boolean[connect.length];
Queue<Integer> queue = new LinkedList<>();
queue.add(n);
group[n] = 1;
while (!queue.isEmpty()) {
int x = queue.poll();
visited[x] = true;
int g = group[x];
for (int c : connect[x]) {
if (group[c] != 0 && group[c] == g)
return false;
group[c] = -g;
if (!visited[c])
queue.add(c);
}
}
return true;
}
}