본문 바로가기
백준 알고리즘/BFS

백준 1707번 C++

by paysmile 2019. 8. 14.

#include <iostream>
#include <cstring>
#include <vector>

using namespace std;
const int MAX = 20001;
int v, e, flag;
int floors[MAX];
vector<int> graph[MAX];

void dfs(int point, int group) {
	floors[point] = group;

	for(int i=0; i<graph[point].size(); i++){
		if(floors[graph[point][i]] == -1)
			dfs(graph[point][i], !group);
	}
	return;
}

int main(void) {
	int testcase;

	cin >> testcase;
	for (int k = 0; k < testcase; k++) {
		cin >> v >> e;
		flag = 0;
		for (int j = 0; j < MAX; j++)
			graph[j].clear();
		memset(floors, -1, sizeof(floors));
		for (int i = 0; i < e; i++) {
			int start, end;
			cin >> start >> end;
			graph[start].push_back(end);
			graph[end].push_back(start);
		}
		for (int i = 1; i <= v; i++) {
			if (floors[i] == -1)
				dfs(i, 0);
		}
		for (int i = 1; i <= v; i++) {
			for (int j = 0; j < graph[i].size(); j++) {
				if (floors[i] == floors[graph[i][j]]) {
					flag = 1;
					break;
				}
			}
		}
		if (flag)
			cout << "NO" << endl;
		else
			cout << "YES" << endl;
	}
	return 0;
}

'백준 알고리즘 > BFS' 카테고리의 다른 글

백준 2206번 C++  (0) 2019.08.15
백준 3055번 C++  (0) 2019.08.14
백준 5014번 C++  (0) 2019.08.12
백준 2589번 C++  (0) 2019.08.12
백준 7569번 C++  (0) 2019.08.10