๋ฌธ์ ๋งํฌ
1707๋ฒ: ์ด๋ถ ๊ทธ๋ํ (acmicpc.net)
ํ์ด
์ฒ์ ๋ ์ฌ๋ฆฐ ๋ด ์์ด๋์ด๋ ์ด๋ ๋ค. DFS๋ฅผ ์ด์ฉํด ํ์ํ๋ฉด์ ์ธ์ ํ ์ ์ ์ ํ์ฌ ๋ด๊ฐ ์๋ ์ ์ ๊ณผ ๋ค๋ฅธ ๊ทธ๋ฃน์ ์ ์ ๋ฒํธ๋ฅผ ์ง์ด ๋ฃ๋๋ค. ๊ทธ๋ฆฌ๊ณ ๊ทธ๋ฃน์ ์๋ก์ด ์ ์ ์ด ๋ค์ด์ฌ ๋ ๋ง๋ค ๊ฐ์ ๊ทธ๋ฃน์ ์๋ ์์ ๋ค์ด์จ ์ ์ ๋ค๊ณผ ์ธ์ ํ์ง ์ฒดํฌํ๋ค.
๊ทธ๋ ๊ฒ ์ง ์ฝ๋๊ฐ ์๋์ ์ฝ๋์ด๋ค.
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
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <climits>
#include <algorithm>
#include <tuple>
#include <cmath>
using namespace std;
typedef long long ll;
int T;
int V, E;
const int MAX_N = 20000 + 5;
int visited[MAX_N];
vector<int> Edge[MAX_N];
vector<int> group[2];
bool check_;
void dfs(int now , int idx) {
group[idx].push_back(now);
visited[now] = 1;
for (int i = 0; i < group[idx].size() - 1; i++) {
if (find(Edge[now].begin(), Edge[now].end(), group[idx][i]) != Edge[now].end()) {
check_ = true;
return;
}
}
for (int i = 0; i < Edge[now].size(); i++) {
int next = Edge[now][i];
if (!visited[next]) {
dfs(next, (idx + 1) % 2);
}
}
return;
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> T;
while (T--) {
for (int i = 0; i <= V; i++) {
Edge[i].clear();
}
memset(visited, 0, sizeof(visited));
group[0].clear();
group[1].clear();
check_ = false;
cin >> V >> E;
int from, to;
for (int i = 0; i < E; i++) {
cin >> from >> to;
Edge[from].push_back(to);
Edge[to].push_back(from);
}
bool chk = false;
for (int i = 1; i <= V; i++) {
if (!visited[i]) {
dfs(i, 0);
if (check_ == true) {
cout << "NO\n";
chk = true;
break;
}
}
}
if (!chk) {
cout << "YES\n";
}
}
return 0;
}
|
cs |
์์ด๋์ด๋ ๊ด์ฐฎ๋ค๊ณ ์๊ฐํ๋๋ฐ ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ๊ฐ ๋๋ฒ๋ ธ๋ค. Edge ๋ฒกํฐ์ visitied ๋ฐฐ์ด ๊ทธ๋ฆฌ๊ณ group ์ด๋ผ๋ ๋ฒกํฐ๊น์ง
๋๋ฌด ๋ง์ ๋ฐฐ์ด์ ๋ง๋ค์ด์ ๊ทธ๋ฐ ๊ฒ ๊ฐ๋ค. ๊ทธ๋ฐ๋ฐ ์ด ์์ด๋์ด ์ธ์๋ ๋ ์ค๋ฅด๋ ์์ด๋์ด๊ฐ ์์ด ํ์ฐธ ๊ณ ๋ฏผํ๋ค ๊ฒฐ๊ตญ
๋ค๋ฅธ ๋ถ์ ์ฝ๋๋ฅผ ์ฐธ๊ณ ํ๋ค.
์ฐธ๊ณ ํ ๋งํฌ
https://jdselectron.tistory.com/51
์ด๋ถ์ ๋ ผ๋ฆฌ๋ ๊ฒฐ๊ตญ์ ๋๋ ๋๊ฐ์๋ค. ๋๋ ์ ์ ์ ๊ฐ ๊ทธ๋ฃน์ ์ง์ด ๋ฃ์๊ณ ์ด ๋ถ์ ์ ์ ์ ์์น ํ๋ค๋ ๋๋์ผ๋ก visited๋ฐฐ์ด์ 1์ ๋นจ๊ฐ 2๋ ํ๋์ผ๋ก ๊ตฌ๋ถํ๋ค.
์กฐ๊ธ๋ง ๋ ์๊ฐํ๋๋ผ๋ฉด ์ถฉ๋ถํ ๋ด ํ์ผ๋ก ๊ตฌํํ ์ ์์๋ค๋ ์์ฌ์์ด ๋จ๋๋ค.
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
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <climits>
#include <algorithm>
#include <tuple>
#include <cmath>
using namespace std;
typedef long long ll;
int T;
int V, E;
const int MAX_N = 20000 + 5;
int visited[MAX_N];
vector<int> Edge[MAX_N];
void dfs(int now, int color) {
visited[now] = color;
for (int i = 0; i < Edge[now].size(); i++) {
int next = Edge[now][i];
if (!visited[next]) {
if (color == 1)
dfs(next, 2);
else if (color == 2)
dfs(next, 1);
}
}
return;
}
bool is_Bipartite() {
for (int i = 1; i <= V; i++) {
for (int j = 0; j < Edge[i].size(); j++) {
if (visited[i] == visited[Edge[i][j]])
return false;
}
}
return true;
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> T;
while (T--) {
for (int i = 0; i <= V; i++) {
Edge[i].clear();
}
memset(visited, 0, sizeof(visited));
cin >> V >> E;
int from, to;
for (int i = 0; i < E; i++) {
cin >> from >> to;
Edge[from].push_back(to);
Edge[to].push_back(from);
}
for (int i = 1; i <= V; i++) {
if (!visited[i])
dfs(i,1);
}
if (is_Bipartite())
cout << "YES\n";
else cout << "NO\n";
}
return 0;
}
|
cs |
'๋ฐฑ์ค > Gold' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 10217 : KCM Travel (0) | 2022.01.27 |
---|---|
[BOJ] 9370 : ๋ฏธํ์ธ ๋์ฐฉ์ง (0) | 2022.01.24 |
[BOJ] 1579 : ์ฑ (0) | 2022.01.20 |
[BOJ] 10942 : ํฐ๋ฆฐ๋๋กฌ? (0) | 2022.01.20 |
[BOJ] 2629 : ์ํ์ ์ธ (0) | 2022.01.19 |