๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฐฑ์ค€/Gold

[BOJ] 1707 : ์ด๋ถ„ ๊ทธ๋ž˜ํ”„

by Jaeguk 2022. 1. 21.
๋ฌธ์ œ ๋งํฌ

1707๋ฒˆ: ์ด๋ถ„ ๊ทธ๋ž˜ํ”„ (acmicpc.net)

 

1707๋ฒˆ: ์ด๋ถ„ ๊ทธ๋ž˜ํ”„

์ž…๋ ฅ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š”๋ฐ, ์ฒซ์งธ ์ค„์— ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ K๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ์ฒซ์งธ ์ค„์—๋Š” ๊ทธ๋ž˜ํ”„์˜ ์ •์ ์˜ ๊ฐœ์ˆ˜ V์™€ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ E๊ฐ€ ๋นˆ ์นธ์„ ์‚ฌ์ด์—

www.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, 0sizeof(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

 

[๋ฐฑ์ค€ 1707, c++] ์ด๋ถ„ ๊ทธ๋ž˜ํ”„(bfs,dfs)

๋ฌธ์ œ ๋ฒˆํ˜ธ 1707(https://www.acmicpc.net/problem/1707) ๋ฌธ์ œ ๋ฐ ์ž…/์ถœ๋ ฅ ๊ทธ๋ž˜ํ”„์˜ ์ •์ ์˜ ์ง‘ํ•ฉ์„ ๋‘˜๋กœ ๋ถ„ํ• ํ•˜์—ฌ, ๊ฐ ์ง‘ํ•ฉ์— ์†ํ•œ ์ •์ ๋ผ๋ฆฌ๋Š” ์„œ๋กœ ์ธ์ ‘ํ•˜์ง€ ์•Š๋„๋ก ๋ถ„ํ• ํ•  ์ˆ˜ ์žˆ์„ ๋•Œ, ๊ทธ๋Ÿฌํ•œ ๊ทธ๋ž˜ํ”„๋ฅผ ํŠน๋ณ„

jdselectron.tistory.com

์ด๋ถ„์˜ ๋…ผ๋ฆฌ๋Š” ๊ฒฐ๊ตญ์€ ๋‚˜๋ž‘ ๋˜‘๊ฐ™์•˜๋‹ค. ๋‚˜๋Š” ์ •์ ์„ ๊ฐ ๊ทธ๋ฃน์— ์ง‘์–ด ๋„ฃ์—ˆ๊ณ  ์ด ๋ถ„์€ ์ •์ ์— ์ƒ‰์น ํ•œ๋‹ค๋Š” ๋Š๋‚Œ์œผ๋กœ 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, 0sizeof(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

 

728x90

'๋ฐฑ์ค€ > 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