恶魔果实 DFS

恶魔果实 DFS

传送门

D F S DFS 水题,之前一直 M L E MLE ,原来是有回路,FQ
去重边直接用 s e t set 就好了,遍历的时候注意结点是否在当前集合即可。

#include<iostream>
#include<cstdio>
#include<set>
using namespace std;
const int N=10,mod=1e4+7;
typedef long long ll;
set<int>e[N];
int x,n,a,b;
ll ans=1;
void dfs(set<int> &s,int u){
     s.insert(u);
     for(auto v:e[u])
        if(!s.count(v)) dfs(s,v);
}
int main(){
    scanf("%d%d",&x,&n);
    while(n--){
        scanf("%d%d",&a,&b);
        e[a].insert(b);
    }
    while(x){
        set<int>s;
        dfs(s,x%10),ans=ans*s.size()%mod,x/=10;
    }
    printf("%lld\n",ans);
}

猜你喜欢

转载自blog.csdn.net/weixin_45750972/article/details/106884037
dfs
今日推荐