-
Notifications
You must be signed in to change notification settings - Fork 5
/
UVA-1572.cpp
89 lines (83 loc) · 1.36 KB
/
UVA-1572.cpp
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
81
82
83
84
85
86
87
88
89
#include<stdio.h>
#include<string.h>
#define MAXN 55
int graph[MAXN][MAXN];
int flag[MAXN];
int has[MAXN];
char str[10];
void handlestr() {
int i, j;
char arr[4];
for(i = 0; i < 4; ++i) {
if(str[2*i] == '0') {
arr[i] = -1;
continue;
}
arr[i] = (str[2*i] - 'A') * 2 + ((str[2*i+1] == '+') ? 0 : 1);
has[arr[i]] = 1;
}
for(i = 0; i < 4; ++i) {
if(arr[i] == -1) continue;
for(j = i+1; j < 4; ++j) {
if(arr[j] == -1) continue;
graph[arr[i]][arr[j]] = 1;
graph[arr[j]][arr[i]] = 1;
}
}
}
bool dfs(int i) {
i = (i % 2 == 0) ? i + 1 : i - 1;
if(!has[i]) {
return false;
}
if(flag[i]) {
return true;
}
flag[i] = true;
int j;
for(j = 0; j < MAXN; ++j) {
if(graph[i][j]) {
if(dfs(j)) {
return true;
}
}
}
flag[i] = false;
return false;
}
bool count() {
int i, j;
for(i = 0; i < MAXN; ++i) {
if(has[i]) {
flag[i] = true;
for(j = 0; j < MAXN; ++j) {
memset(flag, 0, sizeof(flag));
flag[i] = true;
if(graph[i][j]) {
if(dfs(j)) {
return true;
}
}
}
}
}
return false;
}
int main() {
int n, i;
while(scanf("%d", &n) > 0) {
memset(graph, 0, sizeof(graph));
memset(has, 0, sizeof(has));
memset(flag, 0, sizeof(flag));
for(i = 0; i < n; ++i) {
scanf("%s", str);
handlestr();
}
if(!count()) {
puts("bounded");
} else {
puts("unbounded");
}
}
return 0;
}