Submission #1819828


Source Code Expand

#include<bits/stdc++.h>
#define maxn 100100
using namespace std;
struct edge{
	int u,v,w;
	void scan(){scanf("%d%d%d",&u,&v,&w);}
	int operator<(const edge& e)const{return w<e.w;}
}d[maxn];
int n,m,k,c[maxn],f[maxn],sta[maxn],tp;
int find(int x){
	return x==f[x]?x:f[x]=find(f[x]);
}
int main(){
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=n;++i)scanf("%d",&c[i]);
	for(int i=1;i<=m;++i)d[i].scan();
	for(int i=1;i<=k;++i)f[i]=i;
	for(int i=1;i<=m;++i)if(c[d[i].u]&&c[d[i].v])
		f[find(c[d[i].u])]=find(c[d[i].v]);
	sort(d+1,d+m+1);
	for(int i=1;i<=k;++i)if(find(i)==i)sta[++tp]=i;
	for(int i=1;i<=m;++i){
		int u=d[i].u,v=d[i].v;
		if(!c[v])swap(u,v);
		if(!c[u]&&!c[v]){
			if(tp==1)c[u]=c[v]=sta[tp];
			else c[u]=sta[tp],c[v]=sta[tp-1],f[find(c[u])]=find(c[v]),tp-=1;
		} else if(!c[u]){
			if(tp==1)c[u]=sta[tp];
			else if(sta[tp]==find(c[v]))
				c[u]=sta[tp-1],f[find(sta[tp-1])]=find(sta[tp]),sta[tp-1]=sta[tp],tp--;
			else c[u]=sta[tp],f[find(sta[tp])]=find(c[v]),tp--;
		} else f[find(c[u])]=find(c[v]);
		while(tp>=2&&find(sta[tp-1])==find(sta[tp]))tp--;
	}
	if(tp!=1)return puts("-1"),0;
	for(int i=1;i<=k;++i)f[i]=i;
	long long ans=0;
	for(int i=1;i<=m;++i)if(find(c[d[i].u])!=find(c[d[i].v]))
		f[find(c[d[i].u])]=find(c[d[i].v]),ans+=d[i].w;
	printf("%lld",ans);
}

Submission Info

Submission Time
Task A - Colorful MST
User yfzcsc
Language C++14 (GCC 5.4.1)
Score 200
Code Size 1321 Byte
Status WA
Exec Time 50 ms
Memory 2560 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:14: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:15:40: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  for(int i=1;i<=n;++i)scanf("%d",&c[i]);
                                        ^
./Main.cpp: In member function ‘void edge::scan()’:
./Main.cpp:6:38: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  void scan(){scanf("%d%d%d",&u,&v,&w);}
                                      ^

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 47 ms 2304 KB
s1_03.txt AC 1 ms 256 KB
s1_04.txt AC 1 ms 256 KB
s1_05.txt AC 25 ms 1792 KB
s1_06.txt AC 47 ms 2304 KB
s2_07.txt AC 7 ms 512 KB
s2_08.txt AC 49 ms 2304 KB
s2_09.txt AC 34 ms 1536 KB
s2_10.txt AC 32 ms 1408 KB
s2_11.txt AC 45 ms 1792 KB
s2_12.txt AC 44 ms 1792 KB
s3_13.txt WA 7 ms 512 KB
s3_14.txt AC 43 ms 2560 KB
s3_15.txt WA 34 ms 1536 KB
s3_16.txt WA 36 ms 1664 KB
s3_17.txt WA 43 ms 2560 KB
s3_18.txt WA 44 ms 2048 KB
s4_19.txt WA 7 ms 512 KB
s4_20.txt WA 49 ms 1920 KB
s4_21.txt WA 38 ms 1536 KB
s4_22.txt WA 34 ms 1536 KB
s4_23.txt AC 50 ms 2432 KB
s4_24.txt WA 50 ms 2048 KB