#!/usr/bin/env python3
import lzma, sys, time
from functools import lru_cache

def parse(path):
    lines = lzma.open(path, 'rt').read().splitlines() if path.endswith('.xz') else open(path).read().splitlines()
    clauses=[]; n=m=0
    for line in lines:
        if not line or line[0] in 'cp%': continue
        if line.startswith('p '):
            _,_,n,m=line.split()[:4]; n=int(n); m=int(m); continue
        clauses.append(tuple(map(int,line.split()[:-1])))
    return n, clauses

def detect(NV, clauses):
    s=0
    for i in range(1,300):
        if ((-(i+1),1) in set()): pass
    C=set(clauses)
    N=0
    for cand in range(1,200):
        ok=True
        for i in range(1,cand+1):
            if (-(cand+i), i) not in C or (cand+i, -i) not in C:
                ok=False; break
        if ok: N=cand
    adj=[set() for _ in range(N)]
    for j in range(1,N+1):
        a=2*N+j
        seed=j
        aux=None
        for cl in clauses:
            if len(cl)==3 and -a in cl and seed in cl:
                aux=[x for x in cl if x!=-a and x!=seed][0]
                break
        if aux is None:
            print('no aux', j); continue
        parents=[]
        for cl in clauses:
            if len(cl)==2 and cl[0]==-aux and N < cl[1] <= 2*N:
                parents.append(cl[1]-N)
            elif len(cl)==2 and cl[1]==-aux and N < cl[0] <= 2*N:
                parents.append(cl[0]-N)
        for p in parents:
            adj[p-1].add(j-1)
    return N, [list(x) for x in adj]

def sccs(adj, rem):
    n=len(adj); idx=0; st=[]; on=[False]*n; ind=[-1]*n; low=[0]*n; out=[]
    sys.setrecursionlimit(10000)
    def dfs(v):
        nonlocal idx
        ind[v]=low[v]=idx; idx+=1; st.append(v); on[v]=True
        for w in adj[v]:
            if not (rem>>w)&1: continue
            if ind[w]<0: dfs(w); low[v]=min(low[v],low[w])
            elif on[w]: low[v]=min(low[v],ind[w])
        if low[v]==ind[v]:
            comp=[]
            while True:
                w=st.pop(); on[w]=False; comp.append(w)
                if w==v: break
            out.append(comp)
    for v in range(n):
        if (rem>>v)&1 and ind[v]<0: dfs(v)
    return out

def find_cycle(adj, rem):
    n=len(adj); color=[0]*n; parent=[-1]*n
    sys.setrecursionlimit(10000)
    def dfs(v):
        color[v]=1
        for w in adj[v]:
            if not (rem>>w)&1: continue
            if color[w]==0:
                parent[w]=v
                r=dfs(w)
                if r: return r
            elif color[w]==1:
                cyc=[w]; x=v
                while x!=w and x!=-1:
                    cyc.append(x); x=parent[x]
                return cyc
        color[v]=2
        return None
    for v in range(n):
        if (rem>>v)&1 and color[v]==0:
            r=dfs(v)
            if r: return r
    return None

def solve(adj, K, deadline):
    n=len(adj); deg=[len(adj[i])+sum(1 for u in range(n) if i in adj[u]) for i in range(n)]
    full=(1<<n)-1; memo={}; nodes=0
    def lb(rem):
        cnt=0; r=rem
        while True:
            c=find_cycle(adj,r)
            if not c: return cnt
            cnt+=1
            for v in c: r &= ~(1<<v)
    def rec(rem,k):
        nonlocal nodes
        nodes+=1
        if nodes%10000==0 and time.time()>deadline: raise TimeoutError
        key=(rem,k)
        if key in memo: return None
        c=find_cycle(adj,rem)
        if not c: return []
        if k<=0 or lb(rem)>k:
            memo[key]=1; return None
        c=sorted(c, key=lambda v:-deg[v])
        for v in c:
            r=rec(rem & ~(1<<v), k-1)
            if r is not None: return [v]+r
        memo[key]=1; return None
    try:
        sol=rec(full,K)
        return sol,nodes,len(memo)
    except TimeoutError:
        return None,nodes,len(memo)

for path in sys.argv[1:]:
    NV,clauses=parse(path); N,adj=detect(NV,clauses)
    units=[cl[0] for cl in clauses if len(cl)==1]
    # infer K from negative unit offset: compare same-cardinality position; from filename fallback not used in solver
    import re, os
    m=re.search(r'_(\d+)\.cnf', os.path.basename(path)); K=int(m.group(1)) if m else 0
    t=time.time(); sol,nodes,mem=solve(adj,K,t+30)
    print(os.path.basename(path), 'N',N,'E',sum(map(len,adj)),'K',K,'sol', None if sol is None else len(sol), 'nodes',nodes,'memo',mem,'time',round(time.time()-t,3), 'set', None if sol is None else [v+1 for v in sol])
