반응형
https://www.acmicpc.net/problem/1707
1707번: 이분 그래프
입력은 여러 개의 테스트 케이스로 구성되어 있는데, 첫째 줄에 테스트 케이스의 개수 K가 주어진다. 각 테스트 케이스의 첫째 줄에는 그래프의 정점의 개수 V와 간선의 개수 E가 빈 칸을 사이에
www.acmicpc.net
#include <bits/stdc++.h>
using namespace std;
int color[20001];
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int k;
cin >> k;
for (int T = 0; T < k; T++) {
int v, e;
cin >> v >> e;
memset(color, 0, sizeof(color));
vector<int> adj[20001];
for (int i = 0; i < e; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
queue<int> Q;
bool flag = true;
for (int i = 1; i <= v; i++) {
if (color[i] == 0) {
color[i] = 1;
Q.push(i);
while (!Q.empty()) {
int x = Q.front();
int cc = color[x];
Q.pop();
for (auto nx : adj[x]) {
if (color[nx] == cc) {
flag = false;
cout << "NO\n";
break;
}
else if (color[nx] == 0) {
color[nx] = cc == 1 ? 2 : 1;
Q.push(nx);
}
}
if (!flag) break;
}
if (!flag) break;
}
}
if (flag) {
cout << "YES\n";;
}
}
return 0;
}
반응형
'Algorithm > problem' 카테고리의 다른 글
백준 2696번 : 중앙값 구하기 Java (0) | 2022.04.23 |
---|---|
백준 2096번 내려가기 C++ (0) | 2022.04.03 |
백준 1738번 : 골목길 - 벨만 포드 알고리즘 C++ (0) | 2022.03.16 |
백준 11997번 : Load Balancing(Silver) - 누적합, 좌표 압축 C++ (0) | 2022.03.11 |
백준 7662번 : 이중 우선순위 큐 - priority queue & map (0) | 2022.03.10 |