Submission #3448945


Source Code Expand

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

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

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

union_find *make_union_find(int N, int val){
	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);
	uf->u_val = (int *)malloc(sizeof(int) * N);
	for(i = 0; i < N; i++){
		(uf->u)[i] = i;
		(uf->u_rank)[i] = 1;
		(uf->u_val)[i] = val;
	}
	return uf;
}

int root_uf(int x, union_find *uf){
	int *u = uf->u;
	int *u_val = uf->u_val;
	if(u[x] == x){
		return x;
	}
	else if(u[u[x]] == u[x]){
		return u[x];
	}
	else{
		int x_root = root_uf(u[x], uf);
		u_val[x] += u_val[u[x]];
		u[x] = x_root;
		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;
	int *u_val = uf->u_val;
	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;
		u_val[x_root] -= u_val[y_root];
	}
	else{
		u[y_root] = x_root;
		u_rank[x_root] += u_rank[y_root];
		u_rank[y_root] = 0;
		u_val[y_root] -= u_val[x_root];
	}
}

//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;
	}
}

void add_val_uf(int x, int val, union_find *uf){
	int x_root = root_uf(x, uf);
	uf->u_val[x_root] += val;
}

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

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

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

signed main(){
	int N, i;
	scanf("%lld", &N);
	edge *es = (edge *)malloc(sizeof(edge) * (N - 1));
	for(i = 0; i < N - 1; i++){
		scanf("%lld%lld%lld", &es[i].a, &es[i].b, &es[i].c);
		es[i].a--;
		es[i].b--;
	}
	qsort(es, N - 1, sizeof(edge), compare);
	union_find *uf = make_union_find(N, 0);
	int a, b, c, a_rank, b_rank;
	for(i = 0; i < N - 1; i++){
		a = es[i].a;
		b = es[i].b;
		c = es[i].c;
		a_rank = rank_uf(a, uf);
		b_rank = rank_uf(b, uf);
		add_val_uf(a, c * b_rank, uf);
		add_val_uf(b, c * a_rank, uf);
		combine_uf(a, b, uf);
	}
	for(i = 0; i < N; i++){
		printf("%lld\n", val_uf(i, uf));
	}
	return 0;
}

Submission Info

Submission Time
Task E - Black Cats Deployment
User abc050
Language C (GCC 5.4.1)
Score 800
Code Size 2771 Byte
Status AC
Exec Time 65 ms
Memory 6248 KB

Compile Error

./Main.c: In function ‘main’:
./Main.c:110:2: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%lld", &N);
  ^
./Main.c:113: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].c);
   ^

Judge Result

Set Name Sample Subtask1 Subtask2 All
Score / Max Score 0 / 0 200 / 200 200 / 200 400 / 400
Status
AC × 3
AC × 10
AC × 9
AC × 32
Set Name Test Cases
Sample 00_example_01.txt, 00_example_02.txt, 00_example_03.txt
Subtask1 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s1_07.txt
Subtask2 00_example_02.txt, s1_07.txt, s2_08.txt, s2_09.txt, s2_10.txt, s2_11.txt, s2_12.txt, s2_13.txt, s2_14.txt
All 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s1_07.txt, s2_08.txt, s2_09.txt, s2_10.txt, s2_11.txt, s2_12.txt, s2_13.txt, s2_14.txt, s3_15.txt, s3_16.txt, s3_17.txt, s3_18.txt, s3_19.txt, s3_20.txt, s3_21.txt, s3_22.txt, s3_23.txt, s3_24.txt, s3_25.txt, s3_26.txt, s3_27.txt, s3_28.txt, s3_29.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
s1_01.txt AC 1 ms 252 KB
s1_02.txt AC 1 ms 252 KB
s1_03.txt AC 1 ms 252 KB
s1_04.txt AC 1 ms 252 KB
s1_05.txt AC 1 ms 252 KB
s1_06.txt AC 1 ms 252 KB
s1_07.txt AC 1 ms 128 KB
s2_08.txt AC 44 ms 4852 KB
s2_09.txt AC 22 ms 2428 KB
s2_10.txt AC 13 ms 1468 KB
s2_11.txt AC 50 ms 5528 KB
s2_12.txt AC 50 ms 5532 KB
s2_13.txt AC 49 ms 5464 KB
s2_14.txt AC 50 ms 5588 KB
s3_15.txt AC 56 ms 5492 KB
s3_16.txt AC 28 ms 2812 KB
s3_17.txt AC 16 ms 1700 KB
s3_18.txt AC 28 ms 2800 KB
s3_19.txt AC 64 ms 6228 KB
s3_20.txt AC 64 ms 6228 KB
s3_21.txt AC 64 ms 6228 KB
s3_22.txt AC 65 ms 6228 KB
s3_23.txt AC 64 ms 5972 KB
s3_24.txt AC 56 ms 6100 KB
s3_25.txt AC 50 ms 6248 KB
s3_26.txt AC 56 ms 6100 KB
s3_27.txt AC 50 ms 6248 KB
s3_28.txt AC 55 ms 6100 KB
s3_29.txt AC 49 ms 6248 KB