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
- 브루트포스
- 백트래킹
- 투 포인터
- 누적 합
- 구현
- 수학
- 재귀
- 유니티
- ue5
- 정렬
- 시뮬레이션
- 문자열
- BFS
- 알고리즘
- 백준
- XR Interaction Toolkit
- VR
- Unreal Engine 5
- 다이나믹 프로그래밍
- 우선순위 큐
- 트리
- 그래프
- 유니온 파인드
- c++
- DFS
- 자료구조
- 다익스트라
- Team Fortress 2
- 스택
- 그리디 알고리즘
Archives
- Today
- Total
1일1알
백준 2660번 회장뽑기 C++ 본문
https://www.acmicpc.net/problem/2660
2660번: 회장뽑기
입력의 첫째 줄에는 회원의 수가 있다. 단, 회원의 수는 50명을 넘지 않는다. 둘째 줄 이후로는 한 줄에 두 개의 회원번호가 있는데, 이것은 두 회원이 서로 친구임을 나타낸다. 회원번호는 1부터
www.acmicpc.net
bfs
#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;
int minScore = 987654321;
vector<int> ans;
vector<vector<bool>> graph;
vector<bool> found;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
graph = vector<vector<bool>>(n + 1, vector<bool>(n + 1, false));
while (true) {
int a, b;
cin >> a >> b;
if (a == -1 && b == -1) break;
graph[a][b] = true;
graph[b][a] = true;
}
for (int i = 1; i <= n; i++) {
found = vector<bool>(n + 1, false);
int tmpScore = 0;
queue<pair<int, int>> q;
q.push({ i,0 });
found[i] = true;
while (!q.empty()) {
auto curr = q.front();
q.pop();
tmpScore = max(tmpScore, curr.second);
for (int i = 1; i <= n; i++) {
if (graph[curr.first][i] == false) continue;
if (found[i]) continue;
found[i] = true;
q.push({ i,curr.second + 1 });
}
}
if (tmpScore < minScore) {
ans.clear();
minScore = tmpScore;
ans.push_back(i);
}
else if (tmpScore == minScore) {
ans.push_back(i);
}
}
cout << minScore << " " << ans.size() << "\n";
for (auto a : ans) {
cout << a << " ";
}
}
'알고리즘' 카테고리의 다른 글
백준 17141번 연구소 2 C++ (0) | 2023.01.14 |
---|---|
백준 17451번 평행 우주 C++ (0) | 2023.01.13 |
백준 11663번 선분 위의 점 C++ (0) | 2023.01.11 |
백준 18115번 카드 놓기 C++ (0) | 2023.01.10 |
백준 17391번 무한부스터 C++ (0) | 2023.01.09 |