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
#include
using namespace std; const int maxn = 1e5 + 10; int idx[maxn], sz[maxn]; int find_root(int x) { while(x != idx[x]) { idx[x] = idx[idx[x]]; x = idx[x]; } return x; } bool check(int a, int b) { if(find_root(a) == find_root(b)) { return true; } return false; } void unite(int a, int b) { int a_root = find_root(a); int b_root = find_root(b); if(a_root != b_root) { if(sz[a_root] < sz[b_root]) { idx[a_root] = idx[b_root]; sz[b_root] += sz[a_root]; } else { idx[b_root] = idx[a_root]; sz[a_root] += sz[b_root]; } } } int main() { for(int i = 0; i < maxn; i++) { idx[i] = i; sz[i] = 1; } 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; for(int i = 0; i < m; i++) { int a = graph[i].second.first; int b = graph[i].second.second; int c = graph[i].first; if(!check(a, b)) { MST += c; cout << a << " " << b << endl; unite(a, b); } } cout << MST << 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)