D - A Simple Task CodeForces - 11D
题目链接:http://codeforces.com/problemset/problem/11/D
Given a simple graph, output the number of simple cycles in it. A simple cycle is a cycle with no repeated vertices or edges.
Input
The first line of input contains two integers n and m (1 ≤ n ≤ 19, 0 ≤ m) – respectively the number of vertices and edges of the graph. Each of the subsequent m lines contains two integers a and b, (1 ≤ a, b ≤ n, a ≠ b) indicating that vertices a and b are connected by an undirected edge. There is no more than one edge connecting any pair of vertices.
Output
Output the number of cycles in the given graph.
Examples
Input
4 6
1 2
1 3
1 4
2 3
2 4
3 4
Output
7
Note
The example graph is a clique and contains four cycles of length 3 and three cycles of length 4.
题目大意:给定一个简单图,输出其中的简单环的数目。简单环的含义是,不包含重复顶点、重复边的环。
代码:
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<vector> #include<stack> #include<map>#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<vector> #include<stack> #include<map> #include<queue> #include<cmath> using namespace std; typedef long long LL; typedef unsigned long long ull; #define sc1(a) scanf("%lld",&a) #define pf1(a) printf("%lld\n",a) #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 const LL INF=1e18; const ull base=2333; const int maxn=1e1+50; const int maxm=1e3+50; const int maxv=1e6+5; const int mod=1e9+7; const int ba=3e5; LL N,M; LL e[maxn][maxn]; LL dp[1<<20][25];//dp[S][i] 经过集合S 当前在i的总方案数 LL ans=0; LL lowbit(LL x) { for(LL i=0;i<N;i++) { if(x&(1<<i)) return i; } } void solve() { for(LL i=0;i<N;i++) dp[1<<i][i]=1; for(LL s=0;s<(1<<N);s++) { for(LL i=0;i<N;i++) { if(s&(1<<i)) //已经走过当前点 { LL x=lowbit(s); for(LL j=x;j<N;j++)//当前集合最小的点 { if(e[i][j]) //有边 { if((s&(1<<j))==0) //没有走过当前点 { dp[s|(1<<j)][j]+=dp[s][i]; } else //已经走过了 再次走到 说明成环了 { if(j==x) { ans+=dp[s][i]; } } } } } } } pf1((ans-M)/2); } int main() { sc1(N);sc1(M); LL u,v; for(int i=1;i<=M;i++) { sc1(u);sc1(v); u--;v--; e[u][v]=e[v][u]=1; } solve(); return 0; } /** */
当初的梦想实现了吗,事到如今只好放弃吗~