My code doesn't seem to work at all on CodeForces.
When I got RE for the first time , I increased the maximum recursion depth and tried again. There was no improvement. I tried to print out the error using try except block, but there was no output. I'm out of ideas as to what caused the RE although I do suspect the recursion depth to be the culprit. Any help/suggestion will be helpful
Code without increased MaximumRecusrionDepth:-
Code with increased recursion depth:-
Code with some optimization and lower RunTime:-
import sys
from sys import stdin, stdout
sys.setrecursionlimit(4000000)
n=int(input())
adjli=[];inp={};parent=[];output=[];ty=[]
for i in range(n):
adjli.append([]);parent.append(-1);output.append(-1)
for i in range(n):
x=list(stdin.readline().strip().split(' '))
if len(x)==3:
t1,t2,t3=x[0],x[1],x[2]
adjli[i].append(int(t2)-1);parent[int(t2)-1]=i;
adjli[i].append(int(t3)-1);parent[int(t3)-1]=i;
if t1=="AND":
ty.append(1)
elif t1=="OR":
ty.append(2)
elif t1=="XOR":
ty.append(3)
if len(x)==2:
t1,t2=x[0],x[1]
if t1=="NOT":
adjli[i].append(int(t2)-1);parent[int(t2)-1]=i;
ty.append(4)
elif t1=="IN":
ty.append(5);inp[i]=int(t2)
#print(adjli)
#print(parent)
#print(ty)
#print(inp)
def assignout(x):
global adjli,output
if len(adjli[x])==2:
if ty[x]==1:#AND
assignout(adjli[x][0]);assignout(adjli[x][1])
output[x]=output[adjli[x][0]]&output[adjli[x][1]]
if ty[x]==2:#OR
assignout(adjli[x][0]);assignout(adjli[x][1])
output[x]=output[adjli[x][0]]|output[adjli[x][1]]
if ty[x]==3:#XOR
assignout(adjli[x][0]);assignout(adjli[x][1])
output[x]=output[adjli[x][0]]^output[adjli[x][1]]
elif len(adjli[x])==1:
assignout(adjli[x][0])
output[x]= output[adjli[x][0]]^1
elif len(adjli[x])==0:
output[x]=inp[x]
assignout(0)
ans=""
#print(output)
def getnew(ind):
cf=output[ind];cf=cf^1
while True:
#print(ind,cf)
baap=parent[ind];bt=ty[baap];bkb=adjli[baap];
if baap==-1:
return cf
break
if len(bkb)==2:
c1=ind;c2=-1;
if bkb[0]==c1:
c2=bkb[1]
else:
c2=bkb[0]
if bt==1:
cf=cf&output[c2]
if bt==2:
cf=cf|output[c2]
if bt==3:
cf=cf^output[c2]
ind=baap
if len(bkb)==1:
cf=cf^1
ind=baap
if cf==output[baap]:
return output[0]
try:
for i in inp:
ans+=str(getnew(i))
except BaseException as e:
print(e,"ERROR")
print(ans)
Input:-(The entire input is not given, However the number of input lines is given)
1000000
NOT 372184
NOT 940908
NOT 59086
NOT 229025
IN 0
NOT 42130
NOT 411794
NOT 995474
NOT 872431
NOT 554205
AND 387115 701144
NOT 261440
IN 0
NOT 758246
IN 1
NOT 758645
NOT 230272
AND 694079 64950
NOT 23776
IN 0
IN 1
NOT 587099
NOT 967592
NOT 519539
IN 1
NOT 573893
NOT 592845
NOT 390452
NOT 636185
NOT 505487
XOR 109272 632046
NOT 180211
NOT 840521
IN 0
NOT 659437
NOT 284909
NOT 250405
NOT 376365
NOT 188939
NOT 311363
NOT 826221
NOT 459050
NOT 771490
NOT 724469
...
What is the question? I'll try to summarize it as much as possible:-
How to interpret the input? The first line of the input contains n , the number of vertices in the graph. It is followed by N-lines, where the i-th line tells us if that vertex is AND,OR,XOR,NOT or an input denoted by IN. It is followed by two integers in case of AND,OR,XOR where the two integers point to the vertices whose output are taken as input for the current vertex. The final output vertex is always 1.The given graph is a tree.
Basically:-
operation input1 input2 (AND,OR,XOR)
operation input1 (NOT)
IN input-value (INPUT)
Now we have to complement the value of each of the input gates and record the final output at 1. The recorded values are concatenated as a string and displayed The question Link In case my explanation leaves any doubts:-Question