티스토리 뷰
백준 소스코드 [C++] 20010 악덕 영주 혜유
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <sstream>
#include <cstdlib>
#define all(v) v.begin(), v.end()
#define pii pair<int, int>
#define make_unique(v) sort(all(v)), v.erase(unique(all(v)), v.end())
typedef long long ll;
using namespace std;
int V, E, u, v;
ll w;
ll res = 0, M = 0;
struct Edge {
ll cost;
int node[2];
Edge(int u,int v,ll cost) {
this->cost = cost;
this->node[0] = u;
this->node[1] = v;
}
bool operator<(Edge e) {
return this->cost < e.cost;
}
};
vector <Edge> edges;
vector <int> Parent, Rank;
vector <bool> chk;
vector<pair<int, ll>> tree[1000];
int findParent(int u){
if (Parent[u] == u) return u;
return Parent[u] = findParent(Parent[u]);
}
void mergeParent(int u, int v){
u = findParent(u);
v = findParent(v);
if (u == v)return;
if (Rank[u] > Rank[v]) swap(u, v);
Parent[u] = v;
if (Rank[u] == Rank[v]) Rank[v]++;
}
void dfs(int k,ll w=0) {
M = max(M, w);
for (int i = 0;i < tree[k].size();i++) {
if (chk[tree[k][i].first] == false) {
chk[tree[k][i].first] = true;
dfs(tree[k][i].first, w+tree[k][i].second);
chk[tree[k][i].first] = false;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> V >> E;
Rank.resize(V, 1);
Parent.resize(V);
for (int i = 0;i < V;i++)Parent[i] = i;
for (int i = 0;i < E;i++) {
cin >> u >> v >> w;
edges.push_back(Edge(u, v, w));
}
sort(all(edges));
for (int i = 0;i < (int)edges.size();i++) {
ll cost = edges[i].cost;
int L = edges[i].node[0];
int R = edges[i].node[1];
if (findParent(L) == findParent(R))continue;
mergeParent(L, R);
tree[L].push_back({ R, cost });
tree[R].push_back({ L, cost });
res += cost;
}
for (int i = 0;i < V;i++) {
chk.clear();
chk.resize(V);
chk[i] = true;
dfs(i);
}
cout << res << '\n' << M;
return 0;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 10021 Watering the Fields (0) | 2021.03.29 |
---|---|
백준 소스코드 [C++] 16398 행성 연결 (0) | 2021.03.29 |
백준 소스코드 [C++] 16393 Lost Map (0) | 2021.03.28 |
백준 소스코드 [C++] 14167 Moocast (0) | 2021.03.28 |
백준 소스코드 [C++] 2887 행성 터널 (0) | 2021.03.28 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- HIG
- CPU와 Memory
- observeOn
- MeTal
- 코딩대회
- 다익스트라 시간복잡도
- 에드몬드 카프 알고리즘
- 최단경로 알고리즘
- rxswift
- CompositionalLayout
- 벨만포드 시간복잡도
- 강한 순환 참조
- mach-o
- WWDC21
- IOS
- 최단경로 문제
- 최단경로문제
- test coverage
- WWDC19
- 부스트캠프 6기
- Testable
- 네트워크 플로우
- WWDC17
- 컴퓨터 추상화
- 최대 매칭
- 네트워크 유량
- State Restoration
- WWDC16
- 벨만포드 알고리즘
- 포드 풀커슨 알고리즘
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함