Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 에라토스테네스의 체
- 플로이드-와샬
- 시뮬레이션
- JUnit 5
- dfs
- 그리디
- 프로그래머스
- BFS
- Network
- 문자열
- swea
- 수학
- 백준
- mst
- Effective Java
- java
- 위상정렬
- Kotlin
- CS
- 완전탐색
- 스택
- 알고리즘
- 동적계획법
- 유니온 파인드
- 구현
- 이분탐색
- 투 포인터
- 세그먼트 트리
- 백트래킹
- 후니의 쉽게 쓴 시스코 네트워킹
Archives
반갑습니다!
[백준] 13265 색칠하기 본문
풀이
DFS를 통해 탐색하면서 색칠이 가능한지 확인하면 된다. 색칠하는 부분에서 1과 2로 색을 구분하였기 때문에 circle[i] = 3 - circle[idx]
로 다른 색깔로 색칠해주었다.
코드
#include <iostream>
#include <vector>
using namespace std;
int n, m;
bool is_possible;
vector<int> circle;
vector<vector<int>> line;
void dfs(int idx) {
for (auto i : line[idx]) {
if (!circle[i]) {
circle[i] = 3 - circle[idx];
dfs(i);
}
if (circle[idx] == circle[i]) is_possible = false;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
cin >> n >> m;
circle = vector<int>(n + 1);
line = vector<vector<int>>(n + 1);
for (int i = 0; i < m; i++) {
int tmp1, tmp2;
cin >> tmp1 >> tmp2;
line[tmp1].push_back(tmp2);
line[tmp2].push_back(tmp1);
}
is_possible = true;
for (int i = 1; i <= n; i++)
if (!circle[i]) {
circle[i] = 1;
dfs(i);
}
if (is_possible) cout << "possible\n";
else cout << "impossible\n";
}
}
'알고리즘 문제 풀이' 카테고리의 다른 글
[프로그래머스] 징검다리 (0) | 2020.04.27 |
---|---|
[백준] 12100 2048 (Easy) (0) | 2020.04.27 |
[백준] 13460 구슬 탈출 2 (0) | 2020.04.26 |
[SWEA] Ladder1 (0) | 2020.04.26 |
[SWEA] 자기 방으로 돌아가기 (0) | 2020.04.25 |