Skip to content

Commit

Permalink
6-yuyu0830
Browse files Browse the repository at this point in the history
6-yuyu0830
  • Loading branch information
yuyu0830 authored Apr 8, 2024
2 parents 66184fa + d54329f commit 615bc15
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
2 changes: 2 additions & 0 deletions yuyu0830/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
| 2์ฐจ์‹œ | 2024.03.15 | ํƒ์ƒ‰ | [์ˆจ๋ฐ”๊ผญ์งˆ 2](https://www.acmicpc.net/problem/12851) | - |
| 3์ฐจ์‹œ | 2024.03.20 | ํƒ์ƒ‰ | [ํ•˜์ดํผ ํ† ๋งˆํ† ](https://www.acmicpc.net/problem/17114) | - |
| 4์ฐจ์‹œ | 2024.03.26 | ๋ถ„ํ• ์ •๋ณต | [ํ–‰๋ ฌ ์ œ๊ณฑ](https://www.acmicpc.net/problem/10830) | - |
| 5์ฐจ์‹œ | 2024.03.29 | ํƒ์ƒ‰ | [์Šค๋„์ฟ ](https://www.acmicpc.net/problem/2239) | - |
| 6์ฐจ์‹œ | 2024.04.03 | ์ž๋ฃŒ๊ตฌ์กฐ | [๋‚˜๋งŒ ์•ˆ๋˜๋Š” ์—ฐ์• ](https://www.acmicpc.net/problem/14621) | - |
---
81 changes: 81 additions & 0 deletions yuyu0830/์ž๋ฃŒ๊ตฌ์กฐ/14621.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <iostream>
#include <queue>

#define fastio cin.tie(NULL); cin.sync_with_stdio(false);

using namespace std;

struct edge {
int vertex1, vertex2, value;

edge(int v1, int v2, int _value) : vertex1(v1), vertex2(v2), value(_value) {};
};

struct cmp {
bool operator()(edge a, edge b) { return a.value > b.value; }
};

bool manOnly[1001] = {0, };

// Union-Find Function
int parent[1001] = {0, };

int find(int v) {
if (parent[v] == v) return v;
return parent[v] = find(parent[v]);
}

void merge(int x, int y) { parent[find(x)] = find(y); }

bool isUnion(int x, int y) { return find(x) == find(y); }

int main() {
fastio
int n, m; cin >> n >> m;

for (int i = 1; i <= n; i++) {
// Union Initialize
parent[i] = i;

// is University Man only?
char c; cin >> c;
if (c == 'M') manOnly[i] = 1;
}

priority_queue<edge, vector<edge>, cmp> q;

for (int i = 0; i < m; i++) {
int a, b, c; cin >> a >> b >> c;

// No same gender...
if (manOnly[a] == manOnly[b]) continue;

q.push(edge(a, b, c));
}

int cnt = 1, ans = 0;

while (!q.empty()) {
edge tmp = q.top();
q.pop();

int v1 = tmp.vertex1;
int v2 = tmp.vertex2;

// is cycle created? Then pass this edge
if (isUnion(v1, v2)) continue;

// add this edge
merge(v1, v2);
cnt++;
ans += tmp.value;

// if all Vertex visited
if (cnt == n) {
printf("%d\n", ans);
return 0;
}
}

printf("-1\n");
}

0 comments on commit 615bc15

Please sign in to comment.