-
Notifications
You must be signed in to change notification settings - Fork 0
/
PARADOX.cpp
58 lines (51 loc) · 1.1 KB
/
PARADOX.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
// Problem : PARADOX from SPOJ
#include <bits/stdc++.h>
using namespace std;
#define ll long long
void dfs(int src, int next[], bool val[], int realVal[], bool& paradox, set<int>& nodes){
nodes.insert(src);
if(realVal[next[src]]!=-1){
// Paradox can occur only when the DAG has a cycle
if(nodes.find(next[src])!=nodes.end() &&
realVal[next[src]] == realVal[src] ^ val[src])paradox = true;
}
else{
realVal[next[src]] = !(realVal[src] ^ val[src]);
dfs(next[src], next, val, realVal, paradox, nodes);
}
}
void solve(int n){
int next[n],i;
bool val[n];
string s;
int realVal[n];
for(i=0;i<n;i++){
cin>>next[i]>>s;
val[i] = ((s=="true")?true:false);
next[i]--;
realVal[i] = -1;
}
bool paradox = false,compParadox = false;
set<int> nodes;
for(i=0;i<n;i++){
if(realVal[i] == -1){
realVal[i] = 1;
compParadox = false;
nodes.clear();
dfs(i,next,val,realVal,compParadox,nodes);
paradox = paradox || compParadox;
}
}
cout<<((paradox)?"PARADOX":"NOT PARADOX");
return ;
}
int main(){
int t;
cin>>t;
while(t){
solve(t);
cin>>t;
if(t)cout<<"\n";
}
return 0;
}