Submission #3010873


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a) for(int i=0;i<(a);i++)
const ll MOD=1000000007;

const int MAX_V=101010;

int par[MAX_V]; // 親
int rnk[MAX_V]; // rank, 木の深さ

// V要素で初期化
void init(){
  for(int i=0;i<MAX_V;i++){
    par[i]=i;
    rnk[i]=0;
  }
}

// 木の根を求める
int find(int x){
  if (par[x] == x){
    return x;
  }else{
    return par[x] = find(par[x]);
  }
}

// xとyの属する集合を併合
void unite(int x, int y){
  x = find(x);
  y = find(y);
  if (x == y) return;

  if (rnk[x] < rnk[y]){
    par[x] = y;
  }else{
    par[y] = x;
    if (rnk[x] == rnk[y]) rnk[x]++;
  }
}

// xとyが同じ集合に属するかどうか
bool same(int x, int y){
  return find(x) == find(y);
}

vector<pair<int,int> > V;
vector<pair<int,pair<int,int> > > E;

int main(){
    init();
    int N,M,K; cin>>N>>M>>K;
    rep(i,N){
        int c; cin>>c;
        V.push_back({c,i});
    }
    sort(V.begin(),V.end());
    rep(i,N-1) if(V[i].first!=0){
        if(V[i].first==V[i+1].first) unite(V[i].second,V[i+1].second);
    }
    rep(i,M){
        int a,b,w; cin>>a>>b>>w; a--,b--;
        E.push_back({w,{a,b}});
    }
    sort(E.begin(),E.end());
    int cnt=0;
    ll sum=0;
    rep(i,M){
        int w=E[i].first, u=E[i].second.first, v=E[i].second.second;
        if(!same(u,v)){
            unite(u,v);
            sum+=w;
            cnt++;
        }
        if(cnt>=K-1) break;
    }
    if(cnt<K-1) cout<<-1<<endl;
    else cout<<sum<<endl;
    return 0;
}

Submission Info

Submission Time
Task A - Colorful MST
User misosoup
Language C++14 (GCC 5.4.1)
Score 700
Code Size 1633 Byte
Status AC
Exec Time 166 ms
Memory 4084 KB

Judge Result

Set Name Sample Subtask1 Subtask2 Subtask3 Subtask4
Score / Max Score 0 / 0 100 / 100 100 / 100 200 / 200 300 / 300
Status
AC × 4
AC × 7
AC × 12
AC × 7
AC × 28
Set Name Test Cases
Sample 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, 00_example_04.txt
Subtask1 00_example_03.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt
Subtask2 s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s2_07.txt, s2_08.txt, s2_09.txt, s2_10.txt, s2_11.txt, s2_12.txt
Subtask3 00_example_02.txt, s3_13.txt, s3_14.txt, s3_15.txt, s3_16.txt, s3_17.txt, s3_18.txt
Subtask4 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, 00_example_04.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s2_07.txt, s2_08.txt, s2_09.txt, s2_10.txt, s2_11.txt, s2_12.txt, s3_13.txt, s3_14.txt, s3_15.txt, s3_16.txt, s3_17.txt, s3_18.txt, s4_19.txt, s4_20.txt, s4_21.txt, s4_22.txt, s4_23.txt, s4_24.txt
Case Name Status Exec Time Memory
00_example_01.txt AC 2 ms 1024 KB
00_example_02.txt AC 2 ms 1024 KB
00_example_03.txt AC 2 ms 1024 KB
00_example_04.txt AC 2 ms 1024 KB
s1_01.txt AC 2 ms 1024 KB
s1_02.txt AC 155 ms 4084 KB
s1_03.txt AC 2 ms 1024 KB
s1_04.txt AC 2 ms 1024 KB
s1_05.txt AC 86 ms 3060 KB
s1_06.txt AC 158 ms 4084 KB
s2_07.txt AC 23 ms 1468 KB
s2_08.txt AC 166 ms 4084 KB
s2_09.txt AC 123 ms 3316 KB
s2_10.txt AC 113 ms 3316 KB
s2_11.txt AC 160 ms 4084 KB
s2_12.txt AC 157 ms 4084 KB
s3_13.txt AC 19 ms 1468 KB
s3_14.txt AC 138 ms 4084 KB
s3_15.txt AC 113 ms 3316 KB
s3_16.txt AC 105 ms 3316 KB
s3_17.txt AC 141 ms 4084 KB
s3_18.txt AC 135 ms 4084 KB
s4_19.txt AC 22 ms 1468 KB
s4_20.txt AC 155 ms 4084 KB
s4_21.txt AC 119 ms 3316 KB
s4_22.txt AC 116 ms 3316 KB
s4_23.txt AC 157 ms 4084 KB
s4_24.txt AC 157 ms 4084 KB