hdu5222Exploration
来源:互联网 发布:苹果mac偏好设置在哪 编辑:程序博客网 时间:2024/06/05 02:23
链接: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
- hdu5222Exploration
- 关于学习方法的一点个人体会
- 人工智能、机器学习、深度学习三者之间的同心圆关系
- HDOJ 2120 Ice_cream's world I(并查集)
- 百度商桥的“咨询图标”页面加载时自动隐藏,当点击“咨询”时再显示。
- Mac下在zsh中配置adb命令
- hdu5222Exploration
- 一个应用设置两个icon
- POJ 1611 The Suspects【路径压缩】
- qt tcp长连接 粘包
- Educational Codeforces Round 15 E 树上倍增 RMQ思想
- JDBC连接mysql数据库简单案例
- Qt QPainter中获取字体的像素高度和宽度
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
- 关于Android开发时天天模拟器的使用