判断图中是否有奇环

bool sign;
void dfs(int x,int time)
{
    if(!sign)
        return;
    for(int i=head[x];i!=-1;i=en[i].nxt)
    {
        if(tim[en[i].to]==-1)
        {
            tim[en[i].to]=time;
            dfs(en[i].to,time+1);
        }
        else if((tim[en[i].to]+time)%2)
        {
           // cout<<en[i].to<<endl;
            sign=false;
            return ;
        }
    }
}

调用:

bool flag=true;
        for(int i=1;i<=n;i++)
        {
            memset(tim,-1,sizeof(tim));
            sign=true;
            dfs(i,0);
            if(!sign)
            {
                flag=false;
                break;
            }
        }
原文地址:https://www.cnblogs.com/wsruning/p/5676556.html