[BOJ] 1707 이분 그래프

Time Lapse :1hour 50min 43sec

1707.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <vector>
#include <iostream>
#include <queue>
using namespace std;
void solution(){
int v,e,from,to,cur;
cin>>v>>e;
vector<vector<int>> Edges(v+1,vector<int>());
int groups[20001] = {0, };
for(int i=0;i<e;i++){
cin>>from>>to;
Edges[from].push_back(to);
Edges[to].push_back(from);
}
queue<int> q;
for(int i=1;i<=v;i++){
if(groups[i]!=0)
continue;
q.push(i);
groups[i]=1;
while(!q.empty()){
cur = q.front();
q.pop();
for(int j=0;j<Edges[cur].size();j++){
if(groups[Edges[cur][j]]==0){
groups[Edges[cur][j]] = groups[cur] == 1?2:1;
q.push(Edges[cur][j]);
}
else if(groups[Edges[cur][j]]==groups[cur]){
cout<<"NO"<<endl;
return ;
}
}
}
}
cout<<"YES"<<endl;
}

int main(void){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int TC;
cin>>TC;
for(int i=0;i<TC;i++){
solution();
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/1707/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.