## CJam (58 chars) qi:Q2*,Wa*e!{Wa/{_W%e<}%$}%_&{{,1>},2few:~{:-z(Q(%}%0-!},, [Online demo](http://cjam.aditsu.net/#code=qi%3AQ2*%2CWa*e!%7BWa%2F%7B_W%25e%3C%7D%25%24%7D%25_%26%7B%7B%2C1%3E%7D%2C2few%3A~%7B%3A-z\(Q\(%25%7D%250-!%7D%2C%2C&input=2). It will run online for `n=2` without problems and for `n=3` with a bit of patience. For `n=1` it crashes, but since OP has chosen to exclude that case from the requirements it's not a fundamental problem. ### Dissection qi:Q e# Take input from stdin, parse to int, store in Q 2*,Wa*e! e# Take all permutations of (0, -1, 1, -1, 2, -1, ..., -1, 2*Q-1) { e# Map to canonical form... Wa/ e# Split around the -1s {_W%e<}% e# Reverse paths where necessary to get a canonical form $ e# Sort paths }% _& e# Filter to distinct path sets { e# Filter to path sets with valid paths: {,1>}, e# Ignore paths with fewer than two elements (can't be invalid; break 2ew) 2few:~ e# Break paths into their edges {:-z(Q(%}% e# The difference between the endpoints of an edge should be +/-1 or Q (mod 2Q) e# So their absolute values should be 1, Q, 2Q-1. e# d => (abs(d)-1) % (Q-1) maps those differences, and no other possible ones, to 0 e# NB {:-zQ(%}% to map them all to 1 would save a byte, but wouldn't work for Q=2 0-! e# Test that all values obtained are 0 }, , e# Count the filtered distinct path sets --- A more efficient version takes 98 bytes: qi2*:Q{a{__0=[1Q2/Q(]f+Qf%_&1$-\f{+E}~}:E~}/]{_W%>!},:MW=0{_{M\f{__3$_@&@:e<@|^{=}{^j}?}1b}{,)}?}j [Online demo](http://cjam.aditsu.net/#code=qi2*%3AQ%7Ba%7B__0%3D%5B1Q2%2FQ\(%5Df%2BQf%25_%261%24-%5Cf%7B%2BE%7D~%7D%3AE~%7D%2F%5D%7B_W%25%3E!%7D%2C%3AMW%3D0%7B_%7BM%5Cf%7B__3%24_%40%26%40%3Ae%3C%40%7C%5E%7B%3D%7D%7B%5Ej%7D%3F%7D1b%7D%7B%2C\)%7D%3F%7Dj&input=5) This builds the possible paths by depth-first search, then uses a memoised function which counts the possible restricted forests for a given set of vertices. The function works recursively on the basis that any restricted forest for a given non-empty set of vertices consists of a path containing the smallest vertex and a restricted forest covering the vertices not in that path.