luckYrat's library.

This documentation is automatically generated by competitive-verifier/competitive-verifier


:heavy_check_mark: cpp/z_test/aoj-GRL_2_A.test.cpp

Depends on

Required by

Code

#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/all/GRL_2_A"

#include "../../cpp/template/template.cpp"

#include "../../cpp/graph/MST/kruskal.cpp"

int main(){
  int v,e;cin>>v>>e;
  Kruskal A(v);
  for(int i = 0; e > i; i++){
    long long s,t,w;cin>>s>>t>>w;
    A.push(s,t,w);
  }
  A.build();
  cout << A.cost << endl;
}
#line 1 "cpp/z_test/aoj-GRL_2_A.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/all/GRL_2_A"

#line 2 "cpp/template/template.cpp"

//yukicoder@cpp17

#include <iostream>
#include <iomanip>

#include <algorithm>

#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>

#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>

#include <random>

#include <bitset>

#include <complex>

#include <utility>

#include <numeric>

#include <functional>


using namespace std;
using ll = long long;
using P = pair<ll,ll>;


const ll MOD = 998244353;
const ll MODx = 1000000007;
const int INF = (1<<30)-1;
const ll LINF = (1LL<<62LL)-1;
const double EPS = (1e-10);


P ar4[4] = {{0,1},{0,-1},{1,0},{-1,0}};
P ar8[8] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};


template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }
 
/*
確認ポイント
cout << fixed << setprecision(n) << 小数計算//n桁の小数表記になる

計算量は変わらないが楽できるシリーズ
min(max)_element(iter,iter)で一番小さい(大きい)値のポインタが帰ってくる
count(iter,iter,int)でintがiterからiterの間にいくつあったかを取得できる
*/

/* comment outed because can cause bugs
__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
*/
#line 4 "cpp/z_test/aoj-GRL_2_A.test.cpp"

#line 1 "cpp/data-structure/union-find.cpp"
struct UnionFind {
  int n;
  vector<int> par;
  vector<int> size_;
  UnionFind(int n_) : n(n_), par((size_t)n_), size_((size_t)n_,1){
    for(int i = 0; n > i; i++)par[i] = i;
  }

  int root(int x){
    if(par[x] == x)return x;
    return par[x] = root(par[x]);
  }

  void unite(int a,int b){
    int ra = root(a);
    int rb = root(b);
    if(ra==rb)return;
    if(size(ra) > size(rb)) swap(ra,rb);
    par[ra] = rb;
    size_[rb] += size_[ra];
  }

  bool same(int a, int b){
    return root(a) == root(b);
  }

  int size(int a){
    return size_[root(a)];
  }
  void debug(){
    for(int i = 0; n > i; i++){
      cout << size_[root(i)] << " ";
    }
    cout << endl;

    return;
  }
};
#line 2 "cpp/graph/MST/kruskal.cpp"

struct Kruskal {
  struct edge {
    int v1,v2,cost;
    bool operator<(const edge &p){
      return (*this).cost < p.cost;
    }
    bool operator==(const edge &p){
      return (*this).cost == p.cost;
    }
    bool operator>(const edge &p){
      return (*this).cost > p.cost;
    }
  };
  int n;
  int cost = -1;
  vector<edge> K;
  Kruskal(int n_):n(n_) {}
  void push(int v1, int v2, int cost){
    K.push_back({v1,v2,cost});
  }

  void build(){
    vector<edge> after_K;
    vector<edge> remunant_K;
    sort(K.begin(),K.end());
    UnionFind C(n);
    cost = 0;
    for(int i = 0; K.size() > i; i++){
      if(C.same(K[i].v1,K[i].v2)){
        remunant_K.push_back(K[i]);
        continue;
      }
      cost += K[i].cost;
      after_K.push_back(K[i]);
      C.unite(K[i].v1,K[i].v2);
    }
    if(after_K.size() != n-1)cost = -1;
  }
};
#line 6 "cpp/z_test/aoj-GRL_2_A.test.cpp"

int main(){
  int v,e;cin>>v>>e;
  Kruskal A(v);
  for(int i = 0; e > i; i++){
    long long s,t,w;cin>>s>>t>>w;
    A.push(s,t,w);
  }
  A.build();
  cout << A.cost << endl;
}

Test cases

Env Name Status Elapsed Memory
g++ 00_sample1.in :heavy_check_mark: AC 6 ms 3 MB
g++ 00_sample2.in :heavy_check_mark: AC 6 ms 3 MB
g++ critical1.in :heavy_check_mark: AC 6 ms 3 MB
g++ critical2.in :heavy_check_mark: AC 11 ms 4 MB
g++ critical3.in :heavy_check_mark: AC 53 ms 7 MB
g++ out1.in :heavy_check_mark: AC 5 ms 3 MB
g++ out10.in :heavy_check_mark: AC 62 ms 7 MB
g++ out11.in :heavy_check_mark: AC 22 ms 4 MB
g++ out12.in :heavy_check_mark: AC 34 ms 5 MB
g++ out13.in :heavy_check_mark: AC 28 ms 5 MB
g++ out14.in :heavy_check_mark: AC 33 ms 5 MB
g++ out15.in :heavy_check_mark: AC 24 ms 4 MB
g++ out2.in :heavy_check_mark: AC 6 ms 3 MB
g++ out3.in :heavy_check_mark: AC 6 ms 3 MB
g++ out4.in :heavy_check_mark: AC 5 ms 3 MB
g++ out5.in :heavy_check_mark: AC 6 ms 3 MB
g++ out6.in :heavy_check_mark: AC 62 ms 7 MB
g++ out7.in :heavy_check_mark: AC 61 ms 7 MB
g++ out8.in :heavy_check_mark: AC 61 ms 7 MB
g++ out9.in :heavy_check_mark: AC 61 ms 7 MB
Back to top page