Submission #3473138


Source Code Expand

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, P> Pll;
int par[200002];
int rk[200002];
 
void init(int n){
	for(int i=0; i<n; i++){
		par[i]=i;
		rk[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(rk[x]<rk[y]){
		par[x]=y;
	}else{
		par[y]=x;
		if(rk[x]==rk[y]) rk[x]++;
	}
}
 
bool same(int x, int y){
	return find(x)==find(y);
}

int main()
{
	int n, m, k;
	cin>>n>>m>>k;
	init(n+k);
	int c[100000];
	int ct[100000]={};
	for(int i=0; i<n; i++){
		cin>>c[i];
		if(c[i]){
			unite(i, n+c[i]-1);
			ct[c[i]-1]++;
		}
	}
	int ct0=0;
	for(int i=0; i<k; i++){
		if(ct[i]>0) ct0+=(ct[i]-1);
	}
	Pll e[100000];
	for(int i=0; i<m; i++){
		int a, b; ll w;
		cin>>a>>b>>w;
		a--; b--;
		unite(a, b);
		e[i]=Pll(w, P(a, b));
	}
	vector<int> vr[100000];
	int r[100000];
	for(int i=0; i<n; i++){
		r[i]=find(i);
		vr[r[i]].push_back(i);
	}
	int rn=0;
	for(int i=0; i<n; i++){
		if(!vr[i].empty()) rn++;
	}
	init(n+k);
	for(int i=0; i<n; i++){
		cin>>c[i];
		if(c[i]) unite(i, n+c[i]-1);
	}
	int ct1=n-k-ct0;
	if(ct1-(rn-1)<0){
		cout<<-1<<endl;
		return 0;
	}
	sort(e, e+m);
	ll ans=0;
	vector<ll> use;
	for(int i=0; i<m; i++){
		if(!same(e[i].second.first, e[i].second.second)){
			unite(e[i].second.first, e[i].second.second);
			use.push_back(e[i].first);
		}
	}
	int c0=ct1-rn+1;
	while(!use.empty()){
		if(c0==0) break;
		use.pop_back();
		c0--;
	}
	for(int i=0; i<use.size(); i++) ans+=use[i];
	cout<<ans<<endl;
    return 0;
}

Submission Info

Submission Time
Task A - Colorful MST
User chocorusk
Language C++14 (GCC 5.4.1)
Score 700
Code Size 2010 Byte
Status AC
Exec Time 130 ms
Memory 8832 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 3 ms 5248 KB
00_example_02.txt AC 2 ms 4608 KB
00_example_03.txt AC 2 ms 4608 KB
00_example_04.txt AC 2 ms 5120 KB
s1_01.txt AC 2 ms 4736 KB
s1_02.txt AC 123 ms 7804 KB
s1_03.txt AC 3 ms 5376 KB
s1_04.txt AC 3 ms 4608 KB
s1_05.txt AC 74 ms 8832 KB
s1_06.txt AC 123 ms 7804 KB
s2_07.txt AC 18 ms 5504 KB
s2_08.txt AC 127 ms 7548 KB
s2_09.txt AC 98 ms 5888 KB
s2_10.txt AC 91 ms 6016 KB
s2_11.txt AC 128 ms 6776 KB
s2_12.txt AC 126 ms 6780 KB
s3_13.txt AC 18 ms 5376 KB
s3_14.txt AC 109 ms 7804 KB
s3_15.txt AC 96 ms 6268 KB
s3_16.txt AC 89 ms 6780 KB
s3_17.txt AC 118 ms 8440 KB
s3_18.txt AC 118 ms 7928 KB
s4_19.txt AC 19 ms 5248 KB
s4_20.txt AC 130 ms 7672 KB
s4_21.txt AC 99 ms 6268 KB
s4_22.txt AC 94 ms 6268 KB
s4_23.txt AC 120 ms 7420 KB
s4_24.txt AC 130 ms 7544 KB