문제 출처 : https://www.acmicpc.net/problem/1707
팁 : 같은 그룹끼리 연결되면 안 된다
<C++ Code>
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
#include<iostream>
#include<vector>
#include<string.h>
#define MAX 20001
using namespace std;
vector<int> a[MAX];
int co[MAX];
void dfs(int x,int c)
{
if(co[x]!=0) return ;
co[x]=c;
for(int i=0;i<a[x].size();i++)
{
int y=a[x][i];
if(co[y]==0)
{
dfs(y,3-c);
}
}
}
int main(void)
{
int k;
cin>>k;
while(k--)
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
a[i].clear();
co[i]=0;
}
for(int i=0;i<m;i++)
{
int q,w;
cin>>q>>w;
a[q].push_back(w);
a[w].push_back(q);
}
for(int i=1;i<=n;i++)
{
if(co[i]==0)
{
dfs(i,1);
}
}
bool find=true;
for(int i=1;i<=n;i++)
{
for(int t=0;t<a[i].size();t++)
{
int j=a[i][t];
if(co[i]==co[j])
{
find=false;
}
}
}
if(find)
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
}
return 0;
}
|
cs |
'백준 온라인 저지 > BFS, DFS (그래프)' 카테고리의 다른 글
16929번_Two Dots (DFS) (0) | 2019.12.14 |
---|---|
7562번_나이트의 이동(BFS) (0) | 2019.12.14 |
12851번_숨바꼭질 2 (0) | 2019.12.03 |
13549번_숨바꼭질 3 (0) | 2019.12.03 |
13913번_숨바꼭질 4(Bfs) (0) | 2019.12.02 |