迷宫城堡 HDU - 1269 (正反DFS)

迷宫城堡

  HDU - 1269 



正反DFS


#include <bits/stdc++.h>
using namespace std;
#define maxn 12000
int ans,vis[maxn];
vector<int>a[maxn];
vector<int>b[maxn];
void dfsone(int u)
{
    vis[u]=1;
    ans++;
    for(int i=0; i<a[u].size(); i++)
    {
        int v=a[u][i];
        if(!vis[v])
            dfsone(v);
    }
}
void dfstwo(int v)
{
    vis[v]=1;
    ans++;
    for(int i=0; i<b[v].size(); i++)
    {
        int u=b[v][i];
        if(!vis[u])
            dfstwo(u);
    }
}
int main()
{
    int n,m,x,y;
    while(cin>>n>>m&&(n+m)!=0)
    {
        memset(vis,0,sizeof(vis));
        for(int i=1; i<=n; i++)
        {
            a[i].clear();
            b[i].clear();
        }
        ans=0;
        for(int i=0; i<m; i++)
        {
            cin>>x>>y;
            a[x].push_back(y);
            b[y].push_back(x);
        }
        dfsone(1);
        if(ans!=n)
        {
            cout<<"No"<<endl;
            continue;
        }
        ans=0;
        memset(vis,0,sizeof(vis));
        dfstwo(1);
        if(ans!=n)
        {
            cout<<"No"<<endl;
            continue;
        }
        cout<<"Yes"<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/beposit/article/details/80888284