# 拓扑总数

11

## 规则

• 您可以使用任何格式来表示图，例如邻接矩阵，邻接列表或边列表，只要您不对编码进行有用的计算。如果有用，您也可以在输入中包含诸如顶点数或顶点列表之类的东西。
• 您可以假设输入中的图形始终是DAG（没有任何循环）。
• 理论上，您的程序应该可以进行任何输入。但是，如果它溢出您语言中的基本整数类型，它可能会失败。
• 顶点名称可以是任何类型的任何连续值。例如：以0或1开头的数字。（当然，仅当您不使用该数字存储代码时）。
• 这是代码高尔夫球。最短的代码获胜。

## 例

``````Adjacency list:
[ [1 2 3 5] [2 4] [] [2] [] [3] ]
[ [0 1 1 1 0 1] [0 0 1 0 1 0] [0 0 0 0 0 0] [0 0 1 0 0 0] [0 0 0 0 0 0] [0 0 0 1 0 0] ]
Edge list:
6 [ [0 1] [0 2] [0 3] [0 5] [1 2] [1 4] [3 2] [5 3] ]
``````

``````9
``````

``````[0 1 4 5 3 2]
[0 1 5 4 3 2]
[0 1 5 3 4 2]
[0 1 5 3 2 4]
[0 5 1 4 3 2]
[0 5 1 3 4 2]
[0 5 1 3 2 4]
[0 5 3 1 4 2]
[0 5 3 1 2 4]
``````

isaacg 2015年

@isaacg可以。
jimmy23013 2015年

4

# 果酱-25

``````q~{_f{1\$-_j@j@&!*}_!+:+}j
``````

user23013的一项伟大创新是将j与不同的数据类型一起使用，并将邻接表用作初始值的数组。

``````q~             read and evaluate the input (vertex list followed by adjacency list)
{…}j           run the block on the vertex list, doing memoized recursion
and using the adjacency list for initial values
_          copy the vertex list
f{…}       for each vertex and the vertex list
1\$-    copy the vertex and remove it from the list
Python: "V-{v}"
_j     copy the reduced list and call the j block recursively
this solves the problem for the reduced vertex list
Python: "f(G,V-{v})"
@j     bring the vertex to the top of the stack and call the j block recursively
in this case, it's called with a vertex rather than a list
and the memoized value is instantly found in the list of initial values
effectively, this gets the list of vertices adjacent to the current vertex
Python: "G[v]"
@&     bring the reduced list to the top of the stack and intersect
!*     multiply the number of topological sorts of the reduced vertex list
with 1 if the intersection was empty and 0 if not
Python: equivalent to "*(V-G[v]==V)"
after this loop we get an array of sub-results for the reduced vertex lists
_!+        add 1 or 0 to the array if the array was empty or not
because we want to get 1 for the empty array
Python: equivalent to "V<{0}or"
:+         add the numbers in the array
Python: "sum(…)"
``````

1

jimmy23013

@ user23013这是什么巫术？：o

7

# Python，58岁

``f=lambda G,V:V<{0}or sum(f(G,V-{v})*(V-G[v]==V)for v in V)``

``````G = {0:{1,2,3,5}, 1:{2,4}, 2:set(), 3:{2}, 4:set(), 5:{3}, 6:set()}
V = {0,1,2,3,4,5}
``````

+1（表示不使用边列表）。
jimmy23013

5

# Mathematica，80 57 51字节

``````Count[Permutations@#,l_/;l~Subsets~{2}~SubsetQ~#2]&
``````

``````f[{1, 2, 3, 4, 5, 6}, {{1, 2}, {1, 3}, {1, 4}, {1, 6}, {2, 3}, {2, 5}, {4, 3}, {6, 4}}]
``````

2

# Pyth，27个字节

``````Mlf!sm}_dHfq2lYyTfqSZUZ^UGG
``````

``````Code:
Mlf!sm}_dHfq2lYyTfqSZUZ^UGGghQeQ

Input:
6, [ [0, 1], [0, 2], [0, 3], [0, 5], [1, 2], [1, 4], [3, 2], [5, 3] ]
``````

@ user23013在表达式中使用了计数器计数和列表`^UGG`，它将生成的所有`G`条目列表`range(len(G))`
isaacg 2015年

jimmy23013

@ user23013不，它的长度是相同的：`^GlG`vs `^UGG`
isaacg 2015年

2

# Haskell，102 107 100 89 85字节

``````import Data.List
(%)=elemIndex
n#l=sum[1|p<-permutations[0..n],and[u%p<v%p|[u,v]<-l]]
``````

@ user23013：现在适用于未连接顶点的图形。它甚至变得更短。
nimi 2015年