首页 > 其他 > 详细

hdu3594 Cactus

时间:2018-03-17 10:10:18      阅读:212      评论:0      收藏:0      [点我收藏+]

仙人掌入门简单题。
先看一篇文档

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int T, n, uu, vv, hea[20005], cnt, dfn[20005], loo[20005], idx, vis[20005];
bool isok;
struct Edge{
    int too, nxt;
}edge[50005];
void add_edge(int fro, int too){
    edge[++cnt].nxt = hea[fro];
    edge[cnt].too = too;
    hea[fro] = cnt;
}
void dfs(int x){
    if(!isok)   return ;
    dfn[x] = loo[x] = ++idx;
    vis[x] = 1;
    int num=0;
    for(int i=hea[x]; i; i=edge[i].nxt){
        int t=edge[i].too;
        if(vis[t]==2)   isok = false;//1
        else if(!vis[t]){
            dfs(t);
            if(loo[t]>dfn[x])   isok = false;//2
            if(loo[t]<dfn[x]){//3
                num++;
                loo[x] = min(loo[x], loo[t]);
            }
        }
        else{//3
            loo[x] = min(loo[x], dfn[t]);
            num++;
        }
    }
    vis[x] = 2;
    if(num>1)   isok = false;
}
int main(){
    cin>>T;
    while(T--){
        scanf("%d", &n);
        memset(dfn, 0, sizeof(dfn));
        memset(loo, 0, sizeof(loo));
        memset(hea, 0, sizeof(hea));
        memset(vis, 0, sizeof(vis));
        isok = true;
        cnt = idx = 0;
        while(scanf("%d %d", &uu, &vv)!=EOF){
            if(!uu && !vv)  break;
            add_edge(uu+1, vv+1);
        }
        dfs(1);
        if(idx<n)   isok = false;
        printf(isok?"YES\n":"NO\n");
    }
    return 0;
}

hdu3594 Cactus

原文:https://www.cnblogs.com/poorpool/p/8587088.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!