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
- 알고리즘
- 오퍼레이팅시스템
- vector
- Operating System
- c++
- 오에스
- 자료구조
- bfs
- 개발
- cs
- Stack
- 브루트포스
- 북리뷰
- 정석학술정보관
- DP
- 백준
- 코딩
- 코테
- coding
- Computer science
- 문제풀이
- 그래프
- 컴공과
- 정석
- OS
- 구현
- 컴퓨터공학과
- 컴공
- 스택
- 너비우선탐색
Archives
- Today
- Total
Little Jay
[C++] 백준 10552번 - DOM 본문
인접행렬로 문제를 푸는 것을 선호하는데,
이렇게 인접 리스트로 푸는게 오랜만이라 시간이 좀 걸렸다.
항상 느끼는 거지만 인접행렬이 더 편한 것 같다.
#include <bits/stdc++.h>
#define endl '\n'
#define MAX 100005
using namespace std;
int n, m, init;
vector<int> v[MAX];
bool visited[MAX];
int solve() {
int cnt = 0;
queue<int> q;
q.push(init);
visited[init] = true;
while (!q.empty()) {
auto current = q.front(); q.pop();
if (!v[current].size()) {
return cnt;
}
if (visited[v[current][0]]) break;
q.push(v[current][0]);
visited[v[current][0]] = true;
cnt++;
}
return -1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m >> init;
for (int i = 0; i < n; i++) {
int l, h; cin >> l >> h;
v[h].push_back(l);
}
cout << solve() << endl;
return 0;
}
'알고리즘 > BOJ' 카테고리의 다른 글
[C++] 백준 1753 - 최단경로 (0) | 2022.05.11 |
---|---|
[C++] 백준 17127번 - 벚꽃이 정보섬에 피어난 이유 (0) | 2022.05.09 |
[C++] 백준 13565번 - 침투 (0) | 2022.05.03 |
[C++] 백준 14464번 - 소가 길을 건너간 이유 4 (0) | 2022.04.27 |
[C++] 백준 6679번 - 싱기한 네자리 숫자 (0) | 2022.03.20 |
Comments