Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
#include
#include
#include
#include
#include
#include
using namespace std; const int maxn =305; int idx[maxn], sz[maxn]; int num_of_union; void reset_val() { for(int i = 0; i < maxn; i++) { sz[i] = 1; idx[i] = i; } num_of_union = 0; } int find_root(int x) { while(idx[x] != x) { idx[x] = idx[idx[x]]; x = idx[x]; } return x; } bool is_in_the_same_union(int a, int b) { if(find_root(a) == find_root(b)) { return true; } return false; } void union_two_elems(int a, int b) { int root_a = find_root(a); int root_b = find_root(b); if(root_a != root_b) { num_of_union++; if(sz[root_a] < sz[root_b]) { sz[root_b] += sz[root_a]; idx[root_a] = idx[root_b]; } else { sz[root_a] += sz[root_b]; idx[root_b] = idx[root_a]; } } } int main() { int n, m; cin >> n >> m; vector
> > graph; for(int i = 0; i < m; i++) { int a, b, c; cin >> a >> b >> c; graph.push_back(make_pair(c, make_pair(a, b))); } sort(graph.begin(), graph.end()); int MST = 0; reset_val(); vector
> v; for(int i = 0; i < m; i++) { int c = graph[i].first; int a = graph[i].second.first; int b = graph[i].second.second; if(!is_in_the_same_union(a, b)) { union_two_elems(a, b); v.push_back(make_pair(a, b)); MST += c; } } int M2 = 2e9; for(int i = 0; i < v.size(); i++) { int a1 = v[i].first, b1 = v[i].second; int mst2 = 0; reset_val(); for(int j =0 ; j < m; j++) { if(graph[j].second.first == a1 and graph[j].second.second == b1) continue; int c = graph[j].first; int a = graph[j].second.first; int b = graph[j].second.second; if(!is_in_the_same_union(a, b)) { union_two_elems(a, b); mst2 += c; } } if(num_of_union == n - 1) { M2 = min(M2, mst2); } } cout << MST << endl << M2 << endl; return 0; }
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)