-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10004.cpp
More file actions
80 lines (64 loc) · 1.76 KB
/
10004.cpp
File metadata and controls
80 lines (64 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<stdio.h>
#define MAXV 200 /* maximum number of vertices */
#define MAXDEGREE 200 /* maximum outdegree of a vertex */
#define BOOL int
/* 10004 - Bicoloring */
typedef struct {
int painted[MAXV];
int edges[MAXV][MAXDEGREE]; /* adjacency info */
int degree[MAXV]; /* outdegree of each vertex */
int nvertices; /* number of vertices in the graph */
} graph;
enum { FALSE, TRUE};
int BICOLORABLE;
void initialize_graph(graph *g){
int i; /* counter */
g -> nvertices = 0;
for (i = 0 ; i <= MAXV ; i++) {
g->degree[i] = 0;
g->painted[i] = 0;
}
}
void insert_edge(graph *g, int x, int y, BOOL directed){
g->edges[x][g->degree[x]] = y;
g->degree[x] ++;
if (directed == FALSE) insert_edge(g,y,x,TRUE);
}
void read_graph(graph *g, BOOL directed){
int i; /* counter */
int m; /* number of edges */
int x, y; /* vertices in edge (x,y) */
initialize_graph(g);
scanf("%d", &(g->nvertices));
if(g->nvertices == 0)
exit(0);
scanf("%d", &m);
for (i = 0 ; i < m ; i++) {
scanf("%d %d", &x, &y);
insert_edge(g, x, y, directed);
}
}
void paint(graph *g, int v, int color){
int i;
if(g->painted[v] == -color)
BICOLORABLE = FALSE;
if(g->painted[v] != 0)
return;
g->painted[v] = color;
for(i = 0 ; i < g->degree[v] ; i++)
paint(g, g->edges[v][i], -color);
return;
}
int main(){
graph g;
while(1){
BICOLORABLE = TRUE;
read_graph(&g, FALSE);
paint(&g, 1, 1);
if(BICOLORABLE)
puts("BICOLORABLE.");
else
puts("NOT BICOLORABLE.");
}
return 0;
}