Submission #3099843


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(int n) {
	rep(i, n) {
		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(2*k+2);
	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 200
Code Size 2082 Byte
Status WA
Exec Time 150 ms
Memory 3712 KB

Judge Result

Set Name Sample Subtask1 Subtask2 Subtask3 Subtask4
Score / Max Score 0 / 0 100 / 100 100 / 100 0 / 200 0 / 300
Status
AC × 4
AC × 7
AC × 12
AC × 2
WA × 5
AC × 18
WA × 10
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 145 ms 3712 KB
s1_03.txt AC 1 ms 256 KB
s1_04.txt AC 1 ms 256 KB
s1_05.txt AC 80 ms 2816 KB
s1_06.txt AC 145 ms 3712 KB
s2_07.txt AC 20 ms 640 KB
s2_08.txt AC 150 ms 3712 KB
s2_09.txt AC 108 ms 1792 KB
s2_10.txt AC 101 ms 1792 KB
s2_11.txt AC 139 ms 2304 KB
s2_12.txt AC 142 ms 2304 KB
s3_13.txt WA 17 ms 768 KB
s3_14.txt AC 128 ms 3712 KB
s3_15.txt WA 103 ms 2176 KB
s3_16.txt WA 98 ms 2304 KB
s3_17.txt WA 126 ms 3712 KB
s3_18.txt WA 128 ms 3072 KB
s4_19.txt WA 19 ms 768 KB
s4_20.txt WA 138 ms 2944 KB
s4_21.txt WA 110 ms 2176 KB
s4_22.txt WA 105 ms 2304 KB
s4_23.txt AC 147 ms 3712 KB
s4_24.txt WA 138 ms 3072 KB