forked from zhuli19901106/poj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
POJ1029(AC).cpp
131 lines (122 loc) · 2.14 KB
/
POJ1029(AC).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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
using namespace std;
int b[1005];
int l[1005];
int r[1005];
int heavy[1005];
int light[1005];
vector<int> vl[100], vh[100];
int ueqc;
int n, k;
int main()
{
int p;
int i, j;
char s[1000];
int res;
while(scanf("%d%d", &n, &k) == 2){
memset(b, 0, 1005 * sizeof(int));
memset(heavy, 0, 1005 * sizeof(int));
memset(light, 0, 1005 * sizeof(int));
ueqc = 0;
for(i = 0; i < k; ++i){
scanf("%d", &p);
for(j = 0; j < p; ++j){
scanf("%d", &l[j]);
}
for(j = 0; j < p; ++j){
scanf("%d", &r[j]);
}
scanf("%s", s);
if(s[0] == '='){
for(j = 0; j < p; ++j){
heavy[l[j]] = light[l[j]] = 0;
b[l[j]] = 1;
heavy[r[j]] = light[r[j]] = 0;
b[r[j]] = 1;
}
}else if(s[0] == '<'){
++ueqc;
vh[ueqc - 1].clear();
vl[ueqc - 1].clear();
for(j = 0; j < p; ++j){
vh[ueqc - 1].push_back(r[j]);
vl[ueqc - 1].push_back(l[j]);
if(!b[l[j]]){
++light[l[j]];
}
if(!b[r[j]]){
++heavy[r[j]];
}
}
}else if(s[0] == '>'){
++ueqc;
vh[ueqc - 1].clear();
vl[ueqc - 1].clear();
for(j = 0; j < p; ++j){
vh[ueqc - 1].push_back(l[j]);
vl[ueqc - 1].push_back(r[j]);
if(!b[l[j]]){
++heavy[l[j]];
}
if(!b[r[j]]){
++light[r[j]];
}
}
}
}
for(i = 1; i <= n; ++i){
if(b[i]){
continue;
}
if(heavy[i] > 0 && heavy[i] < ueqc){
b[i] = 1;
}else if(light[i] > 0 && light[i] < ueqc){
b[i] = 1;
}
}
p = 0;
j = -1;
for(i = 1; i <= n; ++i){
if(!b[i]){
++p;
j = i;
}
}
if(p == 1 && (j >= 1 && j <= n)){
printf("%d\n", j);
}else{
res = -1;
for(i = 0; i < ueqc; ++i){
p = 0;
for(j = 0; j < (int)vh[i].size(); ++j){
if(!b[vh[i][j]]){
res = vh[i][j];
++p;
}
}
for(j = 0; j < (int)vl[i].size(); ++j){
if(!b[vl[i][j]]){
res = vl[i][j];
++p;
}
}
if(p == 1){
break;
}else{
res = -1;
}
}
if(res >= 1 && res <= n){
printf("%d\n", res);
}else{
printf("0\n");
}
}
}
return 0;
}