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
- DFS
- 그리디 알고리즘
- 우선순위 큐
- 자료구조
- 유니티
- 브루트포스
- 백트래킹
- ue5
- 구현
- BFS
- 투 포인터
- Team Fortress 2
- 재귀
- 시뮬레이션
- VR
- 백준
- 유니온 파인드
- XR Interaction Toolkit
- 수학
- 누적 합
- 정렬
- 문자열
- 다익스트라
- Unreal Engine 5
- 스택
- 트리
- c++
- 알고리즘
- 그래프
- 다이나믹 프로그래밍
Archives
- Today
- Total
1일1알
백준 13905번 세부 C++ 본문
https://www.acmicpc.net/problem/13905
13905번: 세부
첫 번째 줄에는 섬에 존재하는 집의 수 N(2≤N≤100,000)와 다리의 수 M(1≤M≤300,000)이 주어진다. 두 번째 줄에는 숭이의 출발 위치(s)와 혜빈이의 위치(e)가 주어진다. (1≤s, e≤N, s≠e). 다음 M개의 줄
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, s, e;
int ans = 0;
struct Info {
int start;
int end;
int weight;
bool operator<(const Info& other) {
return weight < other.weight;
}
bool operator>(const Info& other) {
return weight > other.weight;
}
};
vector<int> parent;
vector<int> height;
vector<Info> infos;
int GetParent(int n) {
if (n == parent[n]) return n;
return parent[n] = GetParent(parent[n]);
}
void Merge(int u, int v) {
u = GetParent(u);
v = GetParent(v);
if (u == v) return;
if (height[u] > height[v])
::swap(u, v);
parent[u] = v;
if (height[u] == height[v])
height[v]++;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m >> s >> e;
parent = vector<int>(n + 1);
height = vector<int>(n + 1, 1);
for (int i = 1; i <= n; i++) parent[i] = i;
for (int i = 0; i < m; i++) {
int start, end, weight;
cin >> start >> end >> weight;
infos.push_back({ start,end,weight });
}
sort(infos.begin(), infos.end(), greater<>());
bool isPossible = false;
int ans = -1;
for (auto a : infos) {
if (GetParent(a.start) == GetParent(a.end)) continue;
Merge(a.start, a.end);
ans = a.weight;
if (GetParent(s) == GetParent(e)) {
isPossible = true;
break;
}
}
if (!isPossible) cout << 0;
else cout << ans;
}
'알고리즘' 카테고리의 다른 글
백준 1245번 농장 관리 C++ (0) | 2022.11.03 |
---|---|
백준 17178번 줄서기 C++ (0) | 2022.11.02 |
백준 2109번 순회강연 C++ (0) | 2022.10.30 |
백준 15789번 CTP 왕국은 한솔 왕국을 이길 수 있을까? C++ (1) | 2022.10.29 |
백준 25195번 Yes or yes C++ (0) | 2022.10.28 |