結果

問題 No.2991 Hypercubic Graph Flow
ユーザー chineristACchineristAC
提出日時 2024-12-16 00:26:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 156 ms / 2,000 ms
コード長 3,247 bytes
コンパイル時間 179 ms
コンパイル使用メモリ 82,240 KB
実行使用メモリ 88,112 KB
最終ジャッジ日時 2024-12-16 00:26:09
合計ジャッジ時間 2,048 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
56,644 KB
testcase_01 AC 49 ms
63,088 KB
testcase_02 AC 76 ms
78,128 KB
testcase_03 AC 44 ms
56,800 KB
testcase_04 AC 156 ms
88,112 KB
testcase_05 AC 45 ms
56,320 KB
testcase_06 AC 93 ms
79,916 KB
testcase_07 AC 44 ms
56,704 KB
testcase_08 AC 51 ms
66,304 KB
testcase_09 AC 72 ms
69,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys,time

from itertools import permutations
from heapq import heappop,heappush
from collections import deque
import random
import bisect
from math import log,gcd

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

def find_euler_tour(N,M,edge):
    done_counter = [0] * N
    used = [False] * M

    visit = [False] * N

    

    res = []
    for start in range(N):
        if visit[start]:
            continue

        visit[start] = True
        st = [start]
        tmp_res = []
        while st:
            v = st[-1]
            visit[v] = True

            if done_counter[v] == len(edge[v]):
                tmp_res.append(v)
                st.pop()
                continue

            nv,eid = edge[v][done_counter[v]]
            done_counter[v] += 1
            if used[eid]:
                continue
            used[eid] = True
            st.append(nv)
        res.append(tmp_res)
    
    
    return res

def solve(N):
    if N == 1:
        return ("No",[])
    
    if N & 1 == 0:
        edge = [[] for v in range(1<<N)]
        nxt_eid = 0
        for v in range(1<<N):
            for k in range(N):
                if v>>k & 1 == 0:
                    edge[v].append((v^(1<<k),nxt_eid))
                    edge[v^(1<<k)].append((v,nxt_eid))
                    nxt_eid += 1
        
        euler_tour = find_euler_tour(1<<N,nxt_eid,edge)
        res = [[0]*(1<<N) for i in range(1<<N)]

        for ce in euler_tour:
            for u,v in zip(ce,ce[1:]):
                res[u][v] = 1
                res[v][u] = -1
        
        return ("Yes",res)
    
    else:
        edge = [[] for v in range(1<<N)]
        nxt_eid = 0
        for v in range(1<<N):
            for k in range(3,N):
                if v>>k & 1 == 0:
                    edge[v].append((v^(1<<k),nxt_eid))
                    edge[v^(1<<k)].append((v,nxt_eid))
                    nxt_eid += 1
        
        euler_tour = find_euler_tour(1<<N,nxt_eid,edge)
        res = [[0]*(1<<N) for i in range(1<<N)]

        for ce in euler_tour:
            for u,v in zip(ce,ce[1:]):
                res[u][v] = 1
                res[v][u] = -1

        for root in range(0,1<<N,8):
            two_in = [0,3,5,6]
            two_out = [1,2,4,7]

            for a,b in zip(two_in,two_out):
                res[a^root][b^root] = -2
                res[b^root][a^root] = 2
            
            for a in two_in:
                for k in [2,4]:
                    res[a^root][a^k^root] = 1
                    res[a^k^root][a^root] = -1
        
        return ("Yes",res)
    
def checker(N,res):
    for i in range(1<<N):
        for k in range(N):
            if res[i][i^(1<<k)] == 0:
                print(i,i^(1<<k))
                return False
        for j in range(1<<N):
            d = i^j
            if (d & (d-1)) != 0 and res[i][j]!=0:
                return (False,"must 0")
            
        if sum(res[i])!=0:
            return (False,"sum")
    return True

N = int(input())
ans,res = solve(N)

if ans == "Yes":
    print(ans)
    for i in range(1<<N):
        print(*res[i])
else:
    print(ans)
    

        
        
        
            
            

0