Submission #2477561


Source Code Expand

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

/// --- Union Find Library {{{ ///

struct UF {
  int n;
  vector<int> par, rank;
  UF(int n): n(n), par(n, -1), rank(n, 0) {}
  int find(int x) {
    return par[x] < 0 ? x : par[x] = find(par[x]);
  }
  int size(int x) {
    return -par[find(x)];
  }
  bool same(int a, int b) {
    return find(a) == find(b);
  }
  void unite(int a, int b) {
    a = find(a);
    b = find(b);
    if(a == b) return;
    if(rank[a] > rank[b]) swap(a, b);
    par[b] += par[a];
    par[a] = b;
    if(rank[a] == rank[b]) rank[b]++;
  }
};

/// }}}--- ///

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int n, m, k; cin >> n >> m >> k;
  vector<int> c(n);
  for(int &e : c) cin >> e;
  vector<tuple<int, int, int>> g;
  for(int i = 0; i < m; i++) {
    int a, b, w; cin >> a >> b >> w;
    a--; b--;
    g.emplace_back(w, a, b);
    if(c[a]) g.emplace_back(0, a, n + c[a]);
    if(c[b]) g.emplace_back(0, b, n + c[b]);
  }
  sort(begin(g), end(g));
  UF uf(n + k + 10);
  ll ans = 0, cnt = 0;
  for(auto e : g) {
    int w, a, b;
    tie(w, a, b) = e;
    if(uf.same(a, b)) continue;
    uf.unite(a, b);
    if(w) cnt++, ans += w;
    if(cnt == k - 1) return(cout << ans << endl, 0);
  }
  cout << -1 << endl;
}

Submission Info

Submission Time
Task A - Colorful MST
User luma
Language C++14 (GCC 5.4.1)
Score 700
Code Size 1332 Byte
Status AC
Exec Time 73 ms
Memory 8432 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 1 ms 256 KB
00_example_02.txt AC 1 ms 256 KB
00_example_03.txt AC 1 ms 256 KB
00_example_04.txt AC 1 ms 256 KB
s1_01.txt AC 1 ms 256 KB
s1_02.txt AC 73 ms 7920 KB
s1_03.txt AC 1 ms 256 KB
s1_04.txt AC 1 ms 256 KB
s1_05.txt AC 34 ms 3700 KB
s1_06.txt AC 71 ms 8432 KB
s2_07.txt AC 10 ms 1276 KB
s2_08.txt AC 73 ms 7536 KB
s2_09.txt AC 56 ms 7792 KB
s2_10.txt AC 51 ms 4468 KB
s2_11.txt AC 69 ms 7408 KB
s2_12.txt AC 68 ms 7664 KB
s3_13.txt AC 7 ms 704 KB
s3_14.txt AC 44 ms 3572 KB
s3_15.txt AC 34 ms 2168 KB
s3_16.txt AC 33 ms 2168 KB
s3_17.txt AC 44 ms 3572 KB
s3_18.txt AC 42 ms 2932 KB
s4_19.txt AC 9 ms 892 KB
s4_20.txt AC 62 ms 4464 KB
s4_21.txt AC 51 ms 3700 KB
s4_22.txt AC 49 ms 3700 KB
s4_23.txt AC 65 ms 4976 KB
s4_24.txt AC 63 ms 4980 KB