๋ฌธ์ ๋งํฌ
10037๋ฒ: Decorating the Pastures (acmicpc.net)
10037๋ฒ: Decorating the Pastures
Farmer John has N (1 <= N <= 50,000) pastures, conveniently numbered 1...N, connected by M (1 <= M <= 100,000) bidirectional paths. Path i connects pasture A_i (1 <= A_i <= N) to pasture B_i (1 <= B_i <= N) with A_i != B_i. It is possible for two paths to
www.acmicpc.net
๊ทธ๋ํ ํ์ ๋ฌธ์ .
๋ฌธ์ ํด์
๋๋ถ ์กด์ N๊ฐ์ ๋ชฉ์ฅ์ ๊ฐ์ง๊ณ ์๊ณ , ๋์ฅ์ M๊ฐ์ ๊ธธ๋ก ์ฐ๊ฒฐ๋์ด์๋ค. ๊ทธ๋ฆฌ๊ณ ๋์ฅ๋ผ๋ฆฌ๋ ์ต๋ 2๊ฐ์ ๊ธธ๋ก ์ฐ๊ฒฐ๋ ์๋ ์๋ค. ๋ฒ ์๊ฐ ์กด์ ์์ผ์ ์ถํํด์ฃผ๊ธฐ ์ํด ๊ฐ ๋์ฅ์ ๊พธ๋ฏธ๋ ค๊ณ ํ๋ค. F ๋๋ J๋ก ๊พธ๋ฐ ์ ์๋๋ฐ, ์ฐ๊ฒฐ๋์ด์๋ ๋์ฅ๋ผ๋ฆฌ๋ ์๋ก ๊ฐ์ ์ํ๋ฒณ์ผ๋ก ๊พธ๋ฐ ์ ์๋ค. ๊ทธ๋ฐ๋ฐ F๋ณด๋ค J๋ก ๊พธ๋ฏธ๋๊ฒ ๋ ์ธ๊ธฐ ๋๋ฌธ์ ์ต๋ํ J๋ฅผ ๋ง์ด ์ด์ฉํ๋ ค๊ณ ํ๋ค. ๋ง์ฝ ์กฐ๊ฑด์ ์งํค๋ฉด์ ๋ชจ๋ ๋์ฅ์ ๊พธ๋ฐ ์ ์๋ค๋ฉด -1์ ์ถ๋ ฅํ๊ณ , ๊พธ๋ฐ ์ ์๋ค๋ฉด ์ต๋ J์ ๊ฐ์๋ฅผ ์ถ๋ ฅํด๋ผ.
ํ์ด
๋จ์ํ BFS ๋ฌธ์ ์ฒ๋ผ ๋ณด์ด์ง๋ง J๋ฅผ ์ต๋ํ ๋ง์ด ์ด์ฉํ๋ผ๋ ์กฐ๊ฑด์ด ์๋ค. ๋ง์ฝ ์ด์ ๋์ฅ๊ณผ ์ฐ๊ฒฐ๋ ๊ธธ์ด ํ๋๋ ์๋ ๋์ฅ์ด๋ผ๋ฉด ๊ทธ๋ฅ J๋ก ๊พธ๋ฏธ๋ฉด ๋๋ค. ํ์ง๋ง ์ฌ๋ฌ ๋์ฅ์ด ์ฐ๊ฒฐ๋์ด ์๋ ์ํฉ์ด๋ผ๋ฉด ์ฒ์์ ๋จผ์ J๋ก ๊พธ๋ฏธ๋๊ฒ ์ด๋์ธ์ง F๋ก ๊พธ๋ฏธ๋๊ฒ ์ด๋์ธ์ง ๋ฐ๋ก ํ๋จํ ์๊ฐ ์๋ค.
๋๋ J์ F๋ฅผ ์ซ์๋ก ํ๊ธฐํ๊ธฐ ์ํด์ ๋จ์ํ J๋ 2๋ก F๋ 3์ผ๋ก ํ๊ธฐํ๋ค(์ด์ ์์). ๊ทธ๋์ visited ๋ฐฐ์ด์ ๋ง๋ ํ ํด๋น ๋์ฅ์ ๊ฐ์ด 0์ด๋ฉด ์์ง ๊พธ๋ฉฐ์ง์ง์์ ๋์ฅ, 2๋ฉด J๋ก ๊พธ๋ฉฐ์ง ๋์ฅ, 3์ด๋ฉด F๋ก ๊พธ๋ฉฐ์ง ๋์ฅ์ผ๋ก ํ๊ธฐํ๋ค.
1 ~ N ๊น์ง ๋๋ฉด์ ํด๋น ๋์ฅ์ด ์์ง ๊พธ๋ฉฐ์ง์ง ์์์ผ๋ฉด BFS๋ฅผ ํตํด์ ์ธ์ ํ ๋์ฅ์ ๊ฒน์น์ง ์๊ฒ ๊พธ๋ฉฐ์ฃผ์๋ค. ์ฒ์ ์์์ ์ J๋ก ๊พธ๋ฐ์ง F๋ก ๊พธ๋ฐ์ง ๋ชจ๋ฅด๋ฏ๋ก ๋ ๊ฐ์ง ๊ฒฝ์ฐ ๋ชจ๋ ํด๋ณด์๋ค. ํ์ ๋์ค์ ์ธ์ ํ ๋ ์นธ์ด ๋๊ฐ์ ์ํ๋ฒณ์ผ๋ก ๊พธ๋ฉฐ์ง๋ฉด ๊ทธ๋๋ ์๋ชป๋ ๊ฒฝ์ฐ์ด๋ฏ๋ก ํ์์ ์ค๋จํ๋ค. ๊ทธ๋ฆฌ๊ณ J๋ก ์์ํ ๊ฒฝ์ฐ F๋ก ์์ํ ๊ฒฝ์ฐ ๋ชจ๋ ์ ํจํ ๊ฒฝ์ฐ, ์ฆ ๋ชจ๋ ์ฐ๊ฒฐ๋ ๋์ฅ์ด ๋ค๋ฅธ ์ํ๋ฒณ์ผ๋ก ๊พธ๋ฉฐ์ง ๊ฒฝ์ฐ ๋ ์ค์ J๋ฅผ ๋ ๋ง์ด ์ฌ์ฉํ ๊ฒฝ์ฐ๋ฅผ ํํ๋ค. ๋ฐ๋๋ก ๋ ๊ฒฝ์ฐ ๋ชจ๋ ์ ํจํ์ง ์์ ๊ฒฝ์ฐ ๊พธ๋ฏธ๋ ๋ฐฉ๋ฒ์ด ์๋ ๊ฒฝ์ฐ์ด๋ฏ๋ก -1์ ์ถ๋ ฅํ๋ค.
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
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <stack>
#include <vector>
#include <math.h>
#include <queue>
#include <climits>
#include <algorithm>
#include <iomanip>
#include <map>
#define mod 998244353
using namespace std;
typedef long long ll;
typedef priority_queue<pair<ll, pair<int, int>>, vector<pair<ll, pair<int, int>>>, greater<pair<ll, pair<int, int>>>> Priority_queue;
const ll INF = LLONG_MAX;
const int MAX_N = 50000 + 5;
vector<int> visited(MAX_N,0);
vector<int> visited_1(MAX_N,0);
vector<int> visited_2(MAX_N,0);
vector<int> Edge[MAX_N];
int N, M;
int Fill_pasture(int st, int letter) {
queue<pair<int, int>> q;
q.push({ st,letter });
if (letter == 2) {
int cnt = 0;
while (!q.empty()) {
int now = q.front().first;
int Letter = q.front().second;
q.pop();
if (visited_1[now] == Letter)
continue;
else if (visited[now] != 0 && visited[now] != Letter)
return -1;
visited_1[now] = Letter;
if (Letter == 2)
cnt++;
for (int i = 0; i < Edge[now].size(); i++) {
int next = Edge[now][i];
int next_letter;
if (Letter == 2)
next_letter = 3;
else next_letter = 2;
if (!visited_1[next])
q.push({ next,next_letter });
else if (visited_1[next] != next_letter)
return -1;
}
}
return cnt;
}
else if (letter == 3) {
int cnt = 0;
while (!q.empty()) {
int now = q.front().first;
int Letter = q.front().second;
q.pop();
if (visited_2[now] == Letter)
continue;
else if (visited[now] != 0 && visited[now] != Letter)
return -1;
visited_2[now] = Letter;
if (Letter == 2)
cnt++;
for (int i = 0; i < Edge[now].size(); i++) {
int next = Edge[now][i];
int next_letter;
if (Letter == 2)
next_letter = 3;
else next_letter = 2;
if (!visited_2[next])
q.push({ next,next_letter });
else if (visited_2[next] != next_letter)
return -1;
}
}
return cnt;
}
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> M;
for (int i = 0; i < M; i++) {
int a, b;
cin >> a >> b;
Edge[a].push_back(b);
Edge[b].push_back(a);
}
for (int i = 1; i <= N; i++) {
if (visited[i] != 0)
continue;
visited_1 = visited;
visited_2 = visited;
int cnt_1 = Fill_pasture(i, 2);
int cnt_2 = Fill_pasture(i, 3);
if (cnt_1 == -1 && cnt_2 == -1) {
cout << "-1\n";
return 0;
}
else if (cnt_1 >= cnt_2) {
visited = visited_1;
}
else if (cnt_2 > cnt_1) {
visited = visited_2;
}
}
int ans = 0;
for (int i = 1; i <= N; i++) {
if (visited[i] == 2)
ans++;
}
cout << ans << "\n";
return 0;
}
|
cs |
'๋ฐฑ์ค > Gold' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 9879 : Cross Country Skiing (1) | 2022.05.03 |
---|---|
[BOJ] 14529 : Where's Bessie? (0) | 2022.04.29 |
[BOJ] 13595 : Mania de Par (0) | 2022.04.27 |
[BOJ] 5901 : Relocation (0) | 2022.04.25 |
[BOJ] 9874 : Wormholes (0) | 2022.04.22 |