Submission #2016710


Source Code Expand

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define ll long long
using namespace std;

const int N=100005;

struct E{
	int x,y,w;
}mem[N];
int n,m,k,x,y,w,r1,r2,c1,c2,cnt;
ll ans;
int f[N],g[N],c[N];

int find1(int k){
	if (f[k]!=k) f[k]=find1(f[k]);
	return f[k];
}

int find2(int k){
	if (g[k]!=k) g[k]=find2(g[k]);
	return g[k];
}

bool cmp(E a,E b){
	return a.w<b.w;
}

int main(){
	int i;
	scanf("%d%d%d",&n,&m,&k);
	for (i=1;i<=n;i++) scanf("%d",&c[i]);
	for (i=1;i<=k;i++) g[i]=i;
	for (i=1;i<=n;i++) f[i]=i;
	for (i=1;i<=m;i++) scanf("%d%d%d",&mem[i].x,&mem[i].y,&mem[i].w);
	sort(mem+1,mem+1+m,cmp);
	for (i=1;i<=m;i++){
		if (cnt==k-1) break;
		x=find1(mem[i].x); y=find1(mem[i].y); w=mem[i].w;
		if (x==y) continue;
		f[x]=y; c1=c[x]; c2=c[y];
		c[y]=c2?c2:c1;
		if (c1&&c2){
			c1=find2(c1); c2=find2(c2);
			if (c1==c2) continue;
			g[c1]=c2; ans+=1ll*w; cnt++;
		}
		else cnt++,ans+=1ll*w;
	}
	if (cnt!=k-1) ans=-1;
	printf("%lld\n",ans);
	return 0;
}

Submission Info

Submission Time
Task A - Colorful MST
User wdyhy
Language C++14 (GCC 5.4.1)
Score 700
Code Size 1085 Byte
Status AC
Exec Time 53 ms
Memory 2560 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:35:26: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d",&n,&m,&k);
                          ^
./Main.cpp:36:38: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  for (i=1;i<=n;i++) scanf("%d",&c[i]);
                                      ^
./Main.cpp:39:66: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  for (i=1;i<=m;i++) scanf("%d%d%d",&mem[i].x,&mem[i].y,&mem[i].w);
                                                                  ^

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 48 ms 2560 KB
s1_03.txt AC 1 ms 256 KB
s1_04.txt AC 1 ms 256 KB
s1_05.txt AC 25 ms 1920 KB
s1_06.txt AC 48 ms 2560 KB
s2_07.txt AC 7 ms 640 KB
s2_08.txt AC 53 ms 2560 KB
s2_09.txt AC 35 ms 1664 KB
s2_10.txt AC 32 ms 1536 KB
s2_11.txt AC 47 ms 2176 KB
s2_12.txt AC 44 ms 2176 KB
s3_13.txt AC 6 ms 640 KB
s3_14.txt AC 45 ms 2560 KB
s3_15.txt AC 33 ms 1792 KB
s3_16.txt AC 32 ms 1664 KB
s3_17.txt AC 45 ms 2560 KB
s3_18.txt AC 41 ms 2304 KB
s4_19.txt AC 7 ms 640 KB
s4_20.txt AC 43 ms 2304 KB
s4_21.txt AC 34 ms 1792 KB
s4_22.txt AC 36 ms 1664 KB
s4_23.txt AC 52 ms 2560 KB
s4_24.txt AC 44 ms 2304 KB