회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
6퍼센트에서 자꾸 틀립니다
1707번 - 이분 그래프
iamrane
3년 전
0
반례 찾아서 넣어봐도 제대로 출력이 되는데 제가 놓지고 있는게 무엇일까요?
#include <iostream> #include <vector> #include <set> #include <stack> enum class Color { NONE,RED,BLACK }; void Coloring_DFS(std::vector<std::vector<int>>& graph, std::set<int>& visited, std::vector<Color>& color, int start) { std::stack<int> stack; stack.push(start); Color cur_color = Color::RED; color[start] = cur_color; while (!stack.empty()) { int idx = stack.top(); stack.pop(); if(visited.find(idx) == visited.end()) { visited.insert(idx); if(cur_color == Color::RED) { cur_color = Color::BLACK; } else if(cur_color == Color::BLACK) { cur_color = Color::RED; } for(int i = 0; i < graph[idx].size(); i++) { if(visited.find(graph[idx][i]) == visited.end()) { stack.push(graph[idx][i]); color[graph[idx][i]] = cur_color; } } } } } bool isBipartiteGraph(std::vector<std::vector<int>>& graph, std::vector<Color>& color) { for(int i = 1; i < graph.size(); i++) { for(int j = 0; j < graph[i].size(); j++) { if(color[i] == color[graph[i][j]]) { return false; } } } return true; } int main() { int K, V, E; std::vector<std::vector<int>> graph; std::set<int> visited_set; std::vector<Color> color_vector; std::cin>>K; while (K--) { std::cin>>V>>E; color_vector.resize(V+1,Color::NONE); for(int i = 0; i <= V; i++) { graph.push_back(std::vector<int>()); } for(int i = 0; i < E; i++) { int temp1, temp2; std::cin>>temp1>>temp2; graph[temp1].push_back(temp2); graph[temp2].push_back(temp1); } for(int i = 1; i <= V; i++) { if(visited_set.find(i) == visited_set.end()) { Coloring_DFS(graph,visited_set,color_vector,i); } } if(isBipartiteGraph(graph,color_vector)) { std::cout<<"YES"<<'\n'; } else { std::cout<<"NO"<<'\n'; } //Debug /* for(auto v : color_vector) { if(v == Color::RED) { std::cout<<"RED"<<" "; } else if(v == Color::BLACK) { std::cout<<"BLACK"<<" "; } else { std::cout<<"NONE"<<" "; } } */ // Clearing everything for the next loop // Clear Graph for(int i = 0; i <= V; i++) { graph[i].clear(); } graph.clear(); //Clear visited set visited_set.clear(); } return 0; }
댓글을 작성하려면
로그인
해야 합니다.
iamrane 3년 전
반례 찾아서 넣어봐도 제대로 출력이 되는데 제가 놓지고 있는게 무엇일까요?