Submission #3174937


Source Code Expand

#include <stdio.h>
#include <stdlib.h>
#define int long long

typedef struct {
	int N;
	int *u;
	int *u_rank;
}union_find;

union_find *make_union_find(int N){
	int i;
	union_find *uf = (union_find *)malloc(sizeof(union_find));
	uf->N = N;
	uf->u = (int *)malloc(sizeof(int) * N);
	uf->u_rank = (int *)malloc(sizeof(int) * N);
	for(i = 0; i < N; i++){
		(uf->u)[i] = i;
		(uf->u_rank)[i] = 1;
	}
	return uf;
}

int root_uf(int x, union_find *uf){
	int *u = uf->u;
	if(u[x] == x){
		return x;
	}
	else{
		u[x] = root_uf(u[x], uf);
		return u[x];
	}
}

void combine_uf(int x, int y, union_find *uf){
	int x_root = root_uf(x, uf);
	int y_root = root_uf(y, uf);
	int *u = uf->u;
	int *u_rank = uf->u_rank;
	if(x_root == y_root){
		return;
	}
	else if(u_rank[x_root] < u_rank[y_root]){
		u[x_root] = y_root;
		u_rank[y_root] += u_rank[x_root];
		u_rank[x_root] = 0;
	}
	else{
		u[y_root] = x_root;
		u_rank[x_root] += u_rank[y_root];
		u_rank[y_root] = 0;
	}
}

//xとyが同じ集合に属していれば1を,そうでなければ0を返す
int is_same_union_uf(int x, int y, union_find *uf){
	if(root_uf(x, uf) == root_uf(y, uf)){
		return 1;
	}
	else{
		return 0;
	}
}

//xが属する集合の要素数を返す
int rank_uf(int x, union_find *uf){
	return (uf->u_rank)[root_uf(x, uf)];
}

typedef struct {
	int a;
	int b;
	int w;
}edge;

signed compair_edge(const void *x, const void *y){
	return ((edge *)x)->w - ((edge *)y)->w;
}

signed main(){
	int N, M, K, i;
	scanf("%lld%lld%lld", &N, &M, &K);
	int *c = (int *)malloc(sizeof(int) * N);
	for(i = 0; i < N; i++){
		scanf("%lld", &c[i]);
		c[i]--;
	}
	edge *es = (edge *)malloc(sizeof(edge) * M);
	for(i = 0; i < M; i++){
		scanf("%lld%lld%lld", &es[i].a, &es[i].b, &es[i].w);
		es[i].a--;
		es[i].b--;
	}
	qsort(es, M, sizeof(edge), compair_edge);
	union_find *uf = make_union_find(N);
	int *prev = (int *)malloc(sizeof(int) * K);
	for(i = 0; i < K; i++){
		prev[i] = -1;
	}
	for(i = 0; i < N; i++){
		if(c[i] >= 0){
			if(prev[c[i]] >= 0){
				combine_uf(i, prev[c[i]], uf);
			}
			prev[c[i]] = i;
		}
	}
	int ea, eb, now = 0, ans = 0;
	for(i = 0; i < M && now < K - 1; i++){
		ea = es[i].a;
		eb = es[i].b;
		if(is_same_union_uf(ea, eb, uf) == 0){
			combine_uf(ea, eb, uf);
			ans += es[i].w;
			now++;
		}
	}
	if(now == K - 1){
		printf("%lld\n", ans);
	}
	else{
		printf("-1\n");
	}
	return 0;
}

Submission Info

Submission Time
Task A - Colorful MST
User abc050
Language C (GCC 5.4.1)
Score 700
Code Size 2479 Byte
Status AC
Exec Time 59 ms
Memory 5628 KB

Compile Error

./Main.c: In function ‘main’:
./Main.c:82:2: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%lld%lld%lld", &N, &M, &K);
  ^
./Main.c:85:3: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld", &c[i]);
   ^
./Main.c:90:3: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld%lld%lld", &es[i].a, &es[i].b, &es[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 128 KB
00_example_02.txt AC 1 ms 128 KB
00_example_03.txt AC 1 ms 128 KB
00_example_04.txt AC 1 ms 128 KB
s1_01.txt AC 1 ms 128 KB
s1_02.txt AC 56 ms 5628 KB
s1_03.txt AC 1 ms 128 KB
s1_04.txt AC 1 ms 128 KB
s1_05.txt AC 29 ms 4300 KB
s1_06.txt AC 57 ms 5628 KB
s2_07.txt AC 8 ms 984 KB
s2_08.txt AC 59 ms 5628 KB
s2_09.txt AC 44 ms 4732 KB
s2_10.txt AC 41 ms 4476 KB
s2_11.txt AC 57 ms 5628 KB
s2_12.txt AC 56 ms 5628 KB
s3_13.txt AC 7 ms 984 KB
s3_14.txt AC 55 ms 5628 KB
s3_15.txt AC 43 ms 4732 KB
s3_16.txt AC 41 ms 4476 KB
s3_17.txt AC 54 ms 5628 KB
s3_18.txt AC 53 ms 5628 KB
s4_19.txt AC 8 ms 984 KB
s4_20.txt AC 56 ms 5628 KB
s4_21.txt AC 44 ms 4732 KB
s4_22.txt AC 43 ms 4476 KB
s4_23.txt AC 58 ms 5628 KB
s4_24.txt AC 55 ms 5628 KB