hdu5222Exploration

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5222

题意:给定n个点m1条无向边m2条有向边,无向边只允许通过一次,求是否存在环。

分析:直接dfs找,找过的点和边标记不可重复做起点和重复经过,无向边的话在同一次dfs的时候标记只许从一个方向通过。

代码:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=1000010;
const int mod=100000000;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000007;
const int INF=1000000010;
const ll MAX=1ll<<55;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned long long ull;
int n,m1,m2,q[N];
int tot,u[N],v[3*N],p[3*N],pre[3*N];
void add(int a,int b) {
    v[tot]=b;p[tot]=0;pre[tot]=u[a];u[a]=tot++;
}
int dfs(int a) {
    q[a]=2;
    for (int i=u[a];i!=-1;i=pre[i])
    if (!p[i]&&(i>=m1*2||p[i^1]!=2)) {
        if (q[v[i]]!=2) {
            q[v[i]]=2;p[i]=2;
            if (dfs(v[i])) return 1;
            q[v[i]]=1;p[i]=1;
        } else return 1;
    }
    q[a]=1;return 0;
}
int main()
{
    int a,b,i,t,bo;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d%d", &n, &m1, &m2);
        tot=0;memset(u,-1,sizeof(u));
        for (i=1;i<=m1;i++) scanf("%d%d", &a, &b),add(a,b),add(b,a);
        for (i=1;i<=m2;i++) scanf("%d%d", &a, &b),add(a,b);
        for (i=1;i<=n;i++) q[i]=0;
        for (bo=0,i=1;i<=n;i++)
        if (!q[i]) { if (bo=dfs(i)) break ; }
        if (bo) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值