SWexpertAcademy

7465. 창용 마을 무리의 개수

먼지의삶 2019. 7. 12. 03:15
#include<cstdio>
#include<iostream>
#include<vector>
#include<queue>
#include<utility>

using namespace std;

int num;
int a[101][101];

bool d[101][101];

void clean(int n) {//전역변수 초기화.. 쓴거만 초기화하자
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			a[i][j] = 0;
			d[i][j] = false;

		}
	}
}

int main() {
	scanf("%d", &num);
	int non = 1;
	while (num--) {
		int n, m;
		scanf("%d %d", &n, &m);
		for (int i = 0; i < m; i++) {
			int xx, yy;
			scanf("%d %d", &xx, &yy);
			a[xx][yy] = 1;
			a[yy][xx] = 1;
		}

		int r = 0;
		int p = 0;
		for (int i = 1; i <= n; i++){ // 서로 Link되지 않은것 존재가능함 
			p = 0;
			for (int j = 1; j <= n; j++) {
				
				p++;
				if (a[i][j] == 1) p = 0;
			}
			if (p  >= n) r++;
		}
		for (int i = 1; i <= n; i++) {//BFS파트, 인접리스튼 아직 안익숙해서 그냥..인접행렬
			queue<pair<int,int>> q;
			for (int j = 1; j <= n; j++) {
				if (a[i][j] == 1 && d[i][j] == false) {
					q.push(make_pair(i, j));
					d[i][j] = true;
				
					
				}

			}
			
			while (!q.empty()) {
				int x = q.front().second; int y = q.front().first;
				d[x][y] = true;
				q.pop();
				for (int j = 1; j <= n; j++)
				{
					if (a[x][j] == 1 && d[x][j] == false)
					{
						q.push(make_pair(x, j));
						d[x][j] = true;
					}
				}
				if (q.empty())
					r++;
			}
		}
		printf("#%d %d\n", non, r);//값 구하기
		clean(n);//초기화
		non++;
		

	}
	return 0;
}

창용마을의 무리개수 문제는 문제자체는 쉬운데, 서로 연결되지 않은 부분에 대해선 실수할 가능성이 높은듯

쉬운 문제 이지만, 이런 간단한 함정에 빠지면 틀리기 쉬움