State compression DP calculates the number of loops in a simple graph

Source: Internet
Author: User

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 ;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.