Submission #2192275


Source Code Expand

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

const int N = 200005;

struct edge {
	int w; int u; int v;
	bool operator < (const edge &other) const {
		return w < other.w;
	}
};
vector <edge> ed;

int n, m, ncolor;
int c[N];
int par[N];
long long res;

// DSU
int anc(int p) { return p == par[p] ? p : par[p] = anc(par[p]); }
void join(int p, int q) { par[p] = q; } // p = anc(p), q = anc(q)

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	
	cin >> n >> m >> ncolor;

	int more = 0;
	for (int i = 1; i <= n; ++i) {
		cin >> c[i];
		if (!c[i]) ++more, c[i] = ncolor + more;
	}

	while(m--) {
		int u, v, w; cin >> u >> v >> w;
		ed.push_back({w, c[u], c[v]});
	}

	int tot = ncolor + more;

	for (int i = 1; i <= tot; ++i) par[i] = i;
	sort(ed.begin(), ed.end());

	int need = ncolor - 1;
	for (auto &e : ed) {
		int u = anc(e.u);
		int v = anc(e.v);
		if (u == v) continue;

		--need;
		join(u, v);
		res += e.w;

		if (!need) break;
	}

	if (!need) cout << res << endl;
	else cout << -1 << endl;
}

Submission Info

Submission Time
Task A - Colorful MST
User cheater2k
Language C++14 (GCC 5.4.1)
Score 700
Code Size 1071 Byte
Status AC
Exec Time 46 ms
Memory 2804 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 46 ms 2296 KB
s1_03.txt AC 1 ms 256 KB
s1_04.txt AC 1 ms 256 KB
s1_05.txt AC 22 ms 1656 KB
s1_06.txt AC 45 ms 2296 KB
s2_07.txt AC 7 ms 704 KB
s2_08.txt AC 46 ms 2296 KB
s2_09.txt AC 35 ms 2168 KB
s2_10.txt AC 32 ms 2168 KB
s2_11.txt AC 44 ms 2296 KB
s2_12.txt AC 42 ms 2296 KB
s3_13.txt AC 6 ms 704 KB
s3_14.txt AC 43 ms 2804 KB
s3_15.txt AC 33 ms 2168 KB
s3_16.txt AC 32 ms 2168 KB
s3_17.txt AC 44 ms 2804 KB
s3_18.txt AC 40 ms 2548 KB
s4_19.txt AC 7 ms 704 KB
s4_20.txt AC 42 ms 2296 KB
s4_21.txt AC 34 ms 2168 KB
s4_22.txt AC 34 ms 2168 KB
s4_23.txt AC 46 ms 2420 KB
s4_24.txt AC 42 ms 2296 KB