diff --git a/[MST]1206/1774.cpp b/[MST]1206/1774.cpp new file mode 100644 index 0000000..3b1b945 --- /dev/null +++ b/[MST]1206/1774.cpp @@ -0,0 +1,111 @@ +// +// Created by LG on 2021-12-05. +// +#include +#include +#include +#include +#include + +using namespace std; + +typedef pair ci; +typedef tuple tp; + +vector parent; +double sum; + +int findParent(int node){ + if(parent[node] < 0) + return node; + return parent[node] = findParent(parent[node]); +} + +bool unionInput(int x, int y){ + int xp = findParent(x); + int yp = findParent(y); + + if(xp == yp) + return false; + if(parent[xp] < parent[yp]){ + parent[xp] += parent[yp]; + parent[yp] = xp; + } + else{ + parent[yp] += parent[xp]; + parent[xp] = yp; + } + return true; +} + +double kruskal(int v, priority_queue, greater<>> &pq){ + int cnt = 0; + + while(cnt < v-1){ + double weight = get<0>(pq.top()); + int x = get<1>(pq.top()); + int y = get<2>(pq.top()); + pq.pop(); + + if(unionInput(x, y)){ + sum += weight; + cnt++; + } + } + return sum; +} + +int main(){ + + int n, m; + cin >> n >> m; + parent.assign(n+1, -1); + priority_queue, greater<>> pq; + priority_queue, greater<>> pq_cycle; + + vector> connected(n+1, vector(n+1, false)); + vector god(n+1); + for(int i=1; i<=n; i++){ + cin >> god[i].first >> god[i].second; + } + + int cnt = 0; + for(int i=0; i> a >> b; + connected[a][b] = true; + + int xp = findParent(a); + int yp = findParent(b); + + if(xp != yp){ + if (parent[xp] < parent[yp]) { + parent[xp] += parent[yp]; + parent[yp] = xp; + } + else { + parent[yp] += parent[xp]; + parent[xp] = yp; + } + cnt++; + } + } + + for(int i=1; i +#include +#include +#include + +using namespace std; +typedef tuple tp; + +vector parent; + +int findParent(int node) { + if (parent[node] < 0) + return node; + return parent[node] = findParent(parent[node]); +} + +bool unionInput(int x, int y) { + int xp = findParent(x); + int yp = findParent(y); + + if (xp == yp) + return false; + if (parent[xp] < parent[yp]) { + parent[xp] += parent[yp]; + parent[yp] = xp; + } else { + parent[yp] += parent[xp]; + parent[xp] = yp; + } + return true; +} + +long long kruskal(int v, priority_queue, greater<>> &pq){ + int cnt = 0; + int sum = 0; + + while(cnt < v-1){ + if(pq.empty()) + return 0; + int weight = get<0>(pq.top()); + int x = get<1>(pq.top()); + int y = get<2>(pq.top()); + pq.pop(); + + if(unionInput(x, y)){ + cnt++; + sum += weight; + } + } + + return sum; +} + +int main(){ + int n, m, a, b, w; + cin >> n >> m; + priority_queue, greater<>> pq; + + parent.assign(n+1, -1); + long long total = 0; + for(int i=0; i> a >> b >> w; + total += w; + pq.push({w, a, b}); + } + + long long cost = kruskal(n, pq); + if(cost == 0) + cout << -1; + else + cout << total - cost; + + return 0; +} \ No newline at end of file