More actions
imported>Unknown No edit summary |
(Table transclusion repair v1) |
||
| Line 1: | Line 1: | ||
==== 소감 ==== | ==== 소감 ==== | ||
2005/05/11 Accepted 0:00.002 64 | |||
평이한 문제. 이산수학이 생각난다. if...else 구문을 사용할때 모든 조건을 프로그램에서 포함하는지 주의깊게 코딩해야 한다. | 평이한 문제. 이산수학이 생각난다. if...else 구문을 사용할때 모든 조건을 프로그램에서 포함하는지 주의깊게 코딩해야 한다. | ||
| Line 13: | Line 13: | ||
#define MAX_VERTEX 200 | #define MAX_VERTEX 200 | ||
bool input(int edge | bool input(int edge[][2], int * nVertex, int * nInputLine); | ||
bool isBicolorale(int edge | bool isBicolorale(int edge[][2], int nVertex, int nInputLine); | ||
bool paintColor(int count, int vertex, int * color); | bool paintColor(int count, int vertex, int * color); | ||
| Line 21: | Line 21: | ||
int nVertex; | int nVertex; | ||
int nInputLine; | int nInputLine; | ||
int edge | int edge[MAX_VERTEX][2]; | ||
while (input(edge, &nVertex, &nInputLine)) | while (input(edge, &nVertex, &nInputLine)) | ||
{ | { | ||
| Line 32: | Line 32: | ||
} | } | ||
bool isBicolorale(int edge | bool isBicolorale(int edge[][2], int nVertex, int nInputLine) | ||
{ | { | ||
bool check | bool check[MAX_VERTEX] = {0,}; // using node = nInputLine | ||
int color | int color[MAX_VERTEX] = {0,}; // using vertex = nVertex | ||
color | color[0] = BLACK; // 0's Node = BLACK | ||
int count = 0; // count = vertex number | int count = 0; // count = vertex number | ||
while (count != nVertex) | while (count != nVertex) | ||
| Line 42: | Line 42: | ||
for (int i = 0; i < nInputLine; i++) | for (int i = 0; i < nInputLine; i++) | ||
{ | { | ||
if (check | if (check[i] == false) // check 되지 않은 라인 | ||
{ | { | ||
if (count == edge | if (count == edge[i][0]) // | ||
{ | { | ||
if (paintColor(count, edge | if (paintColor(count, edge[i][1], color) == false) | ||
return false; | return false; | ||
} | } | ||
if (count == edge | if (count == edge[i][1]) | ||
{ | { | ||
if (paintColor(count, edge | if (paintColor(count, edge[i][0], color) == false) | ||
return false; | return false; | ||
} | } | ||
| Line 63: | Line 63: | ||
bool paintColor(int count, int vertex, int * color) | bool paintColor(int count, int vertex, int * color) | ||
{ | { | ||
if (color | if (color[count] == BLACK) | ||
{ | { | ||
if (color | if (color[vertex] == BLACK) | ||
return false; | return false; | ||
color | color[vertex] = RED; | ||
} | } | ||
else | else | ||
{ | { | ||
if (color | if (color[vertex] == RED) | ||
return false; | return false; | ||
color | color[vertex] = BLACK; | ||
} | } | ||
return true; | return true; | ||
} | } | ||
bool input(int edge | bool input(int edge[][2], int * nVertex, int * nInputLine) | ||
{ | { | ||
cin >> *nVertex; | cin >> *nVertex; | ||
| Line 85: | Line 85: | ||
cin >> *nInputLine; | cin >> *nInputLine; | ||
for (int i = 0; i < *nInputLine; i++) | for (int i = 0; i < *nInputLine; i++) | ||
cin >> edge | cin >> edge[i][0] >> edge[i][1]; | ||
return true; | return true; | ||
} | } | ||
---- | ---- | ||
[[AOI]] [[Bicoloring]] | [[AOI]] [[Bicoloring]] | ||
Latest revision as of 12:46, 27 March 2026
소감
2005/05/11 Accepted 0:00.002 64
평이한 문제. 이산수학이 생각난다. if...else 구문을 사용할때 모든 조건을 프로그램에서 포함하는지 주의깊게 코딩해야 한다.
코드
// no10004 - Bicoloring
#include <iostream>
using namespace std;
#define RED 1
#define BLACK 2
#define MAX_VERTEX 200
bool input(int edge[][2], int * nVertex, int * nInputLine);
bool isBicolorale(int edge[][2], int nVertex, int nInputLine);
bool paintColor(int count, int vertex, int * color);
int main()
{
int nVertex;
int nInputLine;
int edge[MAX_VERTEX][2];
while (input(edge, &nVertex, &nInputLine))
{
if (isBicolorale(edge, nVertex, nInputLine))
cout << "BICOLORABLE.\n";
else
cout << "NOT BICOLORABLE.\n";
}
return 0;
}
bool isBicolorale(int edge[][2], int nVertex, int nInputLine)
{
bool check[MAX_VERTEX] = {0,}; // using node = nInputLine
int color[MAX_VERTEX] = {0,}; // using vertex = nVertex
color[0] = BLACK; // 0's Node = BLACK
int count = 0; // count = vertex number
while (count != nVertex)
{
for (int i = 0; i < nInputLine; i++)
{
if (check[i] == false) // check 되지 않은 라인
{
if (count == edge[i][0]) //
{
if (paintColor(count, edge[i][1], color) == false)
return false;
}
if (count == edge[i][1])
{
if (paintColor(count, edge[i][0], color) == false)
return false;
}
}
}
count++;
}
return true;
}
bool paintColor(int count, int vertex, int * color)
{
if (color[count] == BLACK)
{
if (color[vertex] == BLACK)
return false;
color[vertex] = RED;
}
else
{
if (color[vertex] == RED)
return false;
color[vertex] = BLACK;
}
return true;
}
bool input(int edge[][2], int * nVertex, int * nInputLine)
{
cin >> *nVertex;
if (*nVertex == 0)
return false;
cin >> *nInputLine;
for (int i = 0; i < *nInputLine; i++)
cin >> edge[i][0] >> edge[i][1];
return true;
}