E .Count Simple Paths
题意:给定一个无向图,N个顶点,M条边,问从一开始,走,长度为0,1,2,3.....(不能走重复的点)的简单路径有多少条。设K是简单路径的条数。输出min(K, 106 )。
分析:dfs从1开始,能走就++,超过 106 直接退出。
#include<bits/stdc++.h>
using namespace std;
const int N=2e6+10;
int cnt;
int nex[N];
int head[N];
int to[N];
bool st[N];
int ans;
void add(int u,int v)
{
to[cnt]=v;nex[cnt]=head[u];
head[u]=cnt++;
}
void dfs(int x)
{
ans++;
if(ans>1000000) return ;
st[x]=true;
for(int i=head[x];i!=-1;i=nex[i])
{
int j=to[i];
if(st[j]) continue;
dfs(j);
}
st[x]=false;
}
int main()
{
int n,m;
cin>>n>>m;
memset(head,-1,sizeof(head));
for(int i=1;i<=m;i++)
{
int u,v;
cin>>u>>v;
add(u,v);add(v,u);
}
dfs(1);
cout<<min(ans,1000000)<<endl;
}