seongjae3588   2년 전

저기에 들어간 논리는 다음과 같습니다.

3개의 노드가 다 서로를 가리키고 있다면, (A->B, B->C, C->A) 

저 ABC를 포함한 그래프는 어떤 식으로도 분할이 불가능한 모양이라고 판단했습니다.

모든 정점을 다 돌아서 저런 순환구조가 나타나는지 확인합니다.

다른 반례도 다 옳게 답이 나오는데 백준 제출 결과는 18%에서 오류가 뜨네요... 도와주십셔

hayan7904   2년 전

노드 3개만을 확인해서는 판별이 불가능하지 않을까 싶습니다.

예시로 A-B-C-D-E-A인 그래프의 경우 이분 그래프가 아니지만 작성하신 코드로는 판별이 불가능합니다.

seongjae3588   2년 전

예외상황이 있었네요. 덕분에 수정 방향을 찾았습니다.

#include
#include
#include
using namespace std;

int vertex; int edge;
vector visited;

class Node
{
public:
Node() {}
Node(int num) :num(num) {}

void link(Node* to)
{
next.push_back(to);
}
int num = 0;
vector next;
};
bool check;

void DFS(Node* curr, int rep)
{
visited[curr->num] = rep;
for (int i = 0; i < curr->next.size(); i++)
{
Node* nextNode = curr->next[i];

if (visited[nextNode->num] > 0)
{
if ((rep - visited[nextNode->num]) % 2 == 0)
{
check = false;
return;
}
continue;
}
DFS(nextNode, rep + 1);
}
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int rep; cin >> rep;
for (int i = 0; i < rep; i++)
{
cin >> vertex >> edge;
Node* graph = new Node[vertex + 1];
for (int a = 1; a <= vertex; a++)
graph[a].num = a;

for (int j = 0; j < edge; j++)
{
int from; int to; cin >> from >> to;
graph[from].link(&graph[to]);
graph[to].link(&graph[from]);
}
check = true;

visited = vector(vertex + 1, -1);
vector> visitNode;
for (int k = 1; k <= vertex; k++)
if (visited[graph[k].num] < 0)
DFS(&graph[k], 1);

if (check)
cout << "YES" << "\n";
else
cout << "NO" << "\n";
}
return 0;
}

12345aa   2년 전

님 근데 제가 그룹 초대장을 보냈는데 허락해주세요ㅠㅠ

12345aa   2년 전

preview

댓글을 작성하려면 로그인해야 합니다.