Submission #3099856


Source Code Expand

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll MOD = 998244353;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-11;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
int par[200001], rank_[200001];
void init() {
	rep(i, 200001) {
		par[i] = i; rank_[i] = 0;
	}
}
int find(int x) {
	if (par[x] == x)return x;
	else return par[x] = find(par[x]);
}
void unite(int x, int y) {
	x = find(x);
	y = find(y);
	if (x == y)return;
	if (rank_[x] < rank_[y])par[x] = y;
	else {
		par[y] = x;
		if (rank_[x] == rank_[y])rank_[x]++;
	}
}
bool same(int x, int y) {
	return find(x) == find(y);
}
struct edge { int u, v; ll cost; };
bool comp(const edge&e1, const edge&e2) {
	return e1.cost < e2.cost;
}
edge es[100000];
int n, m, k; int cnt = 0;
int c[100000];
ll kruskal() {
	sort(es, es + m, comp);
	init();
	ll res = 0;
	rep(i, m) {
		edge e = es[i];
		if (!same(e.u,e.v)) {
			unite(e.u,e.v);
			res += e.cost;
			cnt++;
		}
		if (cnt == k - 1)break;
	}
	return res;
}
int main(){
	cin >> n>>m>>k;
	rep(i, n) {
		cin >> c[i];
	}
	rep(i, m) {
		int u, v; ll w; cin >> u >> v >> w; u--; v--;
		int c1 = c[u]; int c2 = c[v];
		if (c1 == 0)c1 = u + k + 1;
		if (c2 == 0)c2 = v + k + 1;
		es[i] = { c1,c2,w };
	}
	ll out=kruskal();
	if (cnt == k - 1)cout << out << endl;
	else cout << -1 << endl;
	return 0;
}

Submission Info

Submission Time
Task A - Colorful MST
User heno239
Language C++14 (GCC 5.4.1)
Score 700
Code Size 2077 Byte
Status AC
Exec Time 149 ms
Memory 3712 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 1792 KB
00_example_02.txt AC 2 ms 1792 KB
00_example_03.txt AC 2 ms 1792 KB
00_example_04.txt AC 2 ms 1792 KB
s1_01.txt AC 2 ms 1792 KB
s1_02.txt AC 146 ms 3712 KB
s1_03.txt AC 2 ms 1792 KB
s1_04.txt AC 2 ms 1792 KB
s1_05.txt AC 80 ms 2816 KB
s1_06.txt AC 147 ms 3712 KB
s2_07.txt AC 21 ms 2048 KB
s2_08.txt AC 149 ms 3712 KB
s2_09.txt AC 109 ms 3456 KB
s2_10.txt AC 104 ms 3328 KB
s2_11.txt AC 139 ms 3712 KB
s2_12.txt AC 141 ms 3712 KB
s3_13.txt AC 18 ms 2048 KB
s3_14.txt AC 126 ms 3712 KB
s3_15.txt AC 104 ms 3456 KB
s3_16.txt AC 97 ms 3328 KB
s3_17.txt AC 127 ms 3712 KB
s3_18.txt AC 124 ms 3712 KB
s4_19.txt AC 20 ms 2048 KB
s4_20.txt AC 139 ms 3712 KB
s4_21.txt AC 114 ms 3456 KB
s4_22.txt AC 104 ms 3328 KB
s4_23.txt AC 143 ms 3712 KB
s4_24.txt AC 139 ms 3712 KB