Submission #1863873


Source Code Expand

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

const int N = 100005;

struct Edge {
	int u, v, w;

	Edge (int u, int v, int w) :
		u(u), v(v), w(w) {} 

	bool operator < (const Edge& rhs) const {
		return w < rhs.w;
	}
};

int n, m, k;
int color[N];
int par[N * 2];
vector<Edge> E;

int find(int u) {
	return (u == par[u]) ? u : par[u] = find(par[u]);
}

bool join(int u, int v) {
	u = find(u), v = find(v), par[u] = v; return (u != v);
}

int main() {
	ios::sync_with_stdio(false);
	cin >> n >> m >> k;
	for (int i = 1; i <= n; ++i) cin >> color[i];
	for (int i = 1; i <= m; ++i) {
		int u, v, w; cin >> u >> v >> w;
		E.push_back(Edge(u, v, w));
	}
	sort(E.begin(), E.end());
	for (int i = 1; i <= n + k; ++i) par[i] = i;
	int cnt = k - 1;
	long long res = 0;
	for (int i = 0; i < m; ++i) {
		int u = E[i].u, v = E[i].v, w = E[i].w;
		if (color[u]) u = color[u] + n;
		if (color[v]) v = color[v] + n;
		if (!join(u, v)) continue;
		res += w, cnt--;
		if (!cnt) break;
	}
	if (cnt) cout << -1;
	else cout << res;
}

Submission Info

Submission Time
Task A - Colorful MST
User aome
Language C++14 (GCC 5.4.1)
Score 700
Code Size 1066 Byte
Status AC
Exec Time 49 ms
Memory 2676 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 47 ms 2676 KB
s1_03.txt AC 1 ms 256 KB
s1_04.txt AC 1 ms 256 KB
s1_05.txt AC 23 ms 2040 KB
s1_06.txt AC 47 ms 2676 KB
s2_07.txt AC 7 ms 704 KB
s2_08.txt AC 49 ms 2676 KB
s2_09.txt AC 34 ms 2168 KB
s2_10.txt AC 32 ms 2168 KB
s2_11.txt AC 44 ms 2420 KB
s2_12.txt AC 42 ms 2420 KB
s3_13.txt AC 7 ms 704 KB
s3_14.txt AC 45 ms 2676 KB
s3_15.txt AC 34 ms 2168 KB
s3_16.txt AC 32 ms 2168 KB
s3_17.txt AC 45 ms 2676 KB
s3_18.txt AC 40 ms 2420 KB
s4_19.txt AC 7 ms 704 KB
s4_20.txt AC 43 ms 2420 KB
s4_21.txt AC 34 ms 2168 KB
s4_22.txt AC 36 ms 2168 KB
s4_23.txt AC 49 ms 2676 KB
s4_24.txt AC 43 ms 2548 KB