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 |
Tags
- 유니온 파인드
- 누적 합
- 다이나믹 프로그래밍
- 스택
- Unreal Engine 5
- DFS
- VR
- ue5
- 그래프
- 백준
- 자료구조
- 재귀
- BFS
- 시뮬레이션
- 문자열
- 그리디 알고리즘
- 유니티
- 수학
- 구현
- 트리
- 투 포인터
- 다익스트라
- Team Fortress 2
- XR Interaction Toolkit
- 백트래킹
- 알고리즘
- 브루트포스
- c++
- 우선순위 큐
- 정렬
Archives
- Today
- Total
1일1알
백준 5567번 결혼식 C++ 본문
https://www.acmicpc.net/problem/5567
5567번: 결혼식
예제 1의 경우 2와 3은 상근이의 친구이다. 또, 3과 4는 친구이기 때문에, 4는 상근이의 친구의 친구이다. 5와 6은 친구도 아니고, 친구의 친구도 아니다. 따라서 2, 3, 4 3명의 친구를 결혼식에 초대
www.acmicpc.net
그래프 탐색 문제
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int n, m;
vector<vector<int>> graph;
vector<int> dists;
void dfs(int curr, int cnt) {
if (dists[curr] == -1) {
dists[curr] = cnt;
}
else {
if (dists[curr] <= cnt) return;
dists[curr] = cnt;
}
for (int i = 1; i <= n; i++) {
if (graph[curr][i] == -1) continue;
dfs(i, cnt + 1);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
graph = vector<vector<int>>(n + 1, vector<int>(n + 1, -1));
dists = vector<int>(n + 1, -1);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
graph[a][b] = 1;
graph[b][a] = 1;
}
dfs(1, 0);
int ans = 0;
for (int i = 2; i <= n; i++) {
if (dists[i] == -1) continue;
if (dists[i] <= 2) ans++;
}
cout << ans;
}
'알고리즘' 카테고리의 다른 글
백준 13903번 출근 C++ (0) | 2023.01.04 |
---|---|
백준 3980번 선발 명단 C++ (0) | 2023.01.03 |
백준 16957번 체스판 위의 공 C++ (0) | 2022.12.29 |
백준 17265번 나의 인생에는 수학과 함께 C++ (0) | 2022.12.28 |
백준 12845번 모두의 마블 C++ (0) | 2022.12.27 |