"""Let G=(V,E) be a directed Graph. Check whether the given Graph is a tree or not.""" import time t0=time.clock() import sys a=[] b=[] c=[] d=[] fi=open("in19_8.txt",'r') w=open("out_18_19_8.txt",'w') count=0 for row in fi: list=row.split(" ") if count==0: for i in list: a.append(int(i)) if count==1: for i in list: b.append(int(i)) if count==2: for i in list: c.append(int(i)) if count==3: for i in list: d.append(int(i)) count+=1 print a,b,c,d flag=0 if a[0]==0: flag=0 else: flag=flag+1 if b[1]==0: flag=0 else: flag=flag+1 if c[2]==0: flag=0 else: flag=flag+1 if d[3]==0: flag=0 else: flag=flag+1 if flag!=0: print "not a tree" w.write("not a tree") else: f=0 if a[1]==1 : if b[0]>=1: f=f+1 else: if a[2]==1: if c[0]>=1: f=f+1 else: if a[3]==1: if d[0]>=1: f=f+1 else: if f>=1: print"not a tree" w.write("not a tree") else: f=0 if b[0]==1: if a[1]>=1: f=f+1 else: if b[2]==1: if c[1]>=1: f=f+1 else: if b[3]==1: if d[1]>=1: f=f+1 if f>=1: print"not a tree" w.write("not a tree") else: f=0 if c[0]==1: if a[2]>=1: f=f+1 else: if c[1]==1: if b[2]>=1: f=f+1 else: if c[3]==1: if d[2]>=1: f=f+1 else: if f>=1: print"not a tree" w.write("not a tree") else: f=0 if d[0]==1: if a[3]>=1: f=f+1 else: if d[1]==1: if b[3]>=1: f=f+1 else: if d[2]==1: if c[3]>=1: f=f+1 else: if f>=1: print"not a tree" w.write("not a tree") else: print"it is a tree" w.write("it is a tree") t1=time.clock() t2=t1-t0 print t2 w.write('\n'+"time used="+repr(t2)) mem=sys.getsizeof(t0)+sys.getsizeof(t1)+sys.getsizeof(t2)+sys.getsizeof(i)+36 print mem w.write('n'+"memory used="+repr(mem)) fi.close() w.close()