Submission #2068445


Source Code Expand

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;

typedef pair<int, int> P;
typedef pair<int, P> P2;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int N, M, K;
int A[100000];
int U[100000], R[100000];
int find(int x) {
  if (U[x] == x) return x;
  return U[x] = find(U[x]);
}
void unite(int x, int y) {
  x = find(x), y = find(y);
  if (x == y) return;
  if (R[x] < R[y]) swap(x, y);
  U[y] = x;
  R[x] += R[y];
}
bool same(int x, int y) { return find(x) == find(y); }

vector<int> G[100000];
signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  cin >> N >> M >> K;
  rep(i, N) {
    cin >> A[i], A[i]--;
    if (A[i] != -1) G[A[i]].pb(i);
    U[i] = i, R[i] = 1;
  }
  rep(c, K) for (int x : G[c]) unite(x, G[c][0]);
  vector<P2> edges;
  rep(i, M) {
    int a, b, w;
    cin >> a >> b >> w;
    a--, b--;
    edges.pb(P2(w, P(a, b)));
  }
  sort(all(edges));
  long long sum = 0;
  int cnt = K-1;
  for (P2 p : edges) {
    if (cnt == 0) break;
    int w = p._1, u = p._2._1, v = p._2._2;
    if (same(u, v)) continue;
    sum += w;
    unite(u, v);
    cnt--;
  }
  if (cnt > 0) cout << -1 << "\n";
  else cout << sum << "\n";
  return 0;
}

Submission Info

Submission Time
Task A - Colorful MST
User funcsr
Language C++14 (GCC 5.4.1)
Score 700
Code Size 1688 Byte
Status AC
Exec Time 57 ms
Memory 8568 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 2560 KB
00_example_02.txt AC 2 ms 2560 KB
00_example_03.txt AC 2 ms 2560 KB
00_example_04.txt AC 2 ms 2560 KB
s1_01.txt AC 2 ms 2560 KB
s1_02.txt AC 54 ms 8568 KB
s1_03.txt AC 2 ms 2560 KB
s1_04.txt AC 2 ms 2560 KB
s1_05.txt AC 31 ms 7804 KB
s1_06.txt AC 53 ms 8568 KB
s2_07.txt AC 10 ms 3392 KB
s2_08.txt AC 57 ms 7416 KB
s2_09.txt AC 36 ms 5244 KB
s2_10.txt AC 34 ms 5120 KB
s2_11.txt AC 52 ms 6264 KB
s2_12.txt AC 50 ms 6264 KB
s3_13.txt AC 8 ms 3136 KB
s3_14.txt AC 45 ms 5496 KB
s3_15.txt AC 35 ms 4856 KB
s3_16.txt AC 34 ms 4856 KB
s3_17.txt AC 44 ms 5496 KB
s3_18.txt AC 43 ms 5496 KB
s4_19.txt AC 9 ms 3392 KB
s4_20.txt AC 51 ms 6264 KB
s4_21.txt AC 39 ms 5368 KB
s4_22.txt AC 38 ms 5368 KB
s4_23.txt AC 54 ms 7032 KB
s4_24.txt AC 51 ms 6392 KB