Question link http://www.codeforces.com/contest/11/problem/D
The number of questions cannot exceed 20, so the status compression DP can be used.
DP [I] [J] indicates the number of simple paths ending with J when I is in the state, and 1 in the binary representation of I indicates this point in the path.
In binary I, vertex 1 indicates the starting point.
View code
# Include <cstdio>
# Include <cstring>
Typedef _ int64 LLD;
Const Int Maxn = 19 ;
Lld dp [ 1 <Maxn] [maxn]; //
Int MP [maxn] [maxn];
Int Main (){
Int N, m, A, B, I, J, K;
While (Scanf ( " % D " , & N, & M )! = EOF ){
Memset (MP, 0 , Sizeof (MP ));
While (M --){
Scanf ( " % D " , & A, & B );
A --; B --;
MP [a] [B] = MP [B] [a] = 1 ;
}
For (I = 0 ; I <( 1 <N); I ++) For (J = 0 ; J <n; j ++) DP [I] [J] = 0 ;
For (I = 0 ; I <n; I ++) DP [ 1 <I] [I] =1 ;
LLD ans = 0 ;
For (I = 0 ; I <( 1 <N); I ++ ){
For (J = 0 ; J <n; j ++ ){
If (! DP [I] [J]) Continue ;
Bool Flag = 0 ;
For (K = 0 ; K <n; k ++ ){
If (( 1 <K) & I ){
If (! Flag & MP [J] [k] & I! = (( 1 <J) + ( 1 <K ))){ // If it is the starting point and connected to the ending point, and there are at least three vertices in the vertex set, the ring is formed.
Ans + = DP [I] [J];
}
Flag =1 ;
}
Else If (Flag & MP [J] [k])
DP [I ^ ( 1 <K)] [k] + = DP [I] [J];
}
}
}
Printf ( " % I64d \ n " , ANS> 1 );
}
Return 0 ;
}