forked from marioyc/Online-Judge-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1518 - This Sentence is False.cpp
89 lines (69 loc) · 1.87 KB
/
1518 - This Sentence is False.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 <cstdio>
#include <cstring>
#include <vector>
using namespace std;
#define MAXN 1005
struct edge{
int to,w;
edge(){}
edge(int _to, int _w):
to(_to), w(_w){}
};
bool ok,visited[MAXN],done[MAXN];
vector<edge> L[MAXN];
int h[MAXN],pathxor[MAXN];
int cont[2];
void dfs(int cur, int p, int curh, int val){
visited[cur] = true;
h[cur] = curh;
bool back = true;
++cont[val];
for(int i = L[cur].size() - 1,to,w;i >= 0;--i){
to = L[cur][i].to;
w = L[cur][i].w;
if(!visited[to]){
pathxor[curh + 1] = (pathxor[curh] ^ w);
dfs(to,cur,curh + 1,(val ^ w));
}else if(!done[to] && (to != p || !back)){
if((pathxor[curh] ^ pathxor[ h[to] ]) != w)
ok = false;
}
if(to == p) back = false;
}
done[cur] = true;
}
int main(){
int N;
char s1[10],s2[10],s3[10];
while(true){
scanf("%d",&N);
if(N == 0) break;
for(int i = 1;i <= N;++i)
L[i].clear();
for(int i = 1,v;i <= N;++i){
scanf("%s %d %s %s",s1,&v,s2,s3);
if(s3[0] == 'f'){
L[i].push_back(edge(v,1));
L[v].push_back(edge(i,1));
}else{
L[i].push_back(edge(v,0));
L[v].push_back(edge(i,0));
}
}
memset(visited,false,sizeof visited);
memset(done,false,sizeof done);
pathxor[0] = 0;
ok = true;
int ans = 0;
for(int i = 1;i <= N;++i){
if(!visited[i]){
cont[0] = cont[1] = 0;
dfs(i,0,0,0);
ans += max(cont[0],cont[1]);
}
}
if(!ok) puts("Inconsistent");
else printf("%d\n",ans);
}
return 0;
}