알고리즘/SW Expert Academy

[D4] 7465. 창용 마을 무리의 개수

혬혬 2019. 10. 12. 16:56
728x90
#include <stdio.h>
int connect[101][101] = { 0 };
int q = 0;
int person_amount = 0;
int connect_amount = 0;
void search(int list[],int point) {
	for (int i = 1; i <= person_amount; i++) {
		if (point == i)
			continue;
		if (list[i] != q + 1&&connect[point][i]==q+1) {
			list[i] = q + 1;
			search(list, i);
		}
	}
}

int main() {
	freopen("inp.inp", "r", stdin);
	freopen("out.out", "w", stdout);

	int test_case = 0;
	scanf("%d", &test_case);
	for (q = 0; q < test_case; q++) {
		scanf("%d %d", &person_amount, &connect_amount);
		for (int i = 0; i < connect_amount; i++) {
			int a, b;
			scanf("%d %d", &a, &b);
			connect[a][b] = q+1;
			connect[b][a] = q + 1;
		}
		int visit_list[101] = { 0 };
		int amount = 0;
		for (int i = 1; i <= person_amount; i++) {
			if (visit_list[i] != q+1) {
				amount++;
				visit_list[i] = q + 1;
				search(visit_list, i);
			}
		}
		printf("#%d %d\n", q + 1, amount);
	}
	return 0;
}
728x90