ZOJ 2475 Benny's Compiler(dfs判断有向图给定点有没有参与构成环)
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Description
These days Benny has designed a new compiler for C programming language. His compilation system provides a compiler driver that invokes the language preprocessor, compiler, assembler and linker. C source file (with .C suffix) is translated to relocatable object module first, and then all modules are linked together to generate an executable object file.
The translator (preprocessor, compiler and assembler) works perfectly and can generate well optimized assembler code from C source file. But the linker has a serious bug -- it cannot resolve global symbols when there are circular references. To be more specific, if file 1 references variables defined in file 2, file 2 references variables defined in file 3, ... file n-1 references variables defined in file n and file n references variables defined in file 1, then Benny's linker walks out because it doesn't know which file should be processed first.
Your job is to determine whether a source file can be compiled successfully by Benny's compiler.
Input
There are multiple test cases! In each test case, the first line contains one integer N, and then N lines follow. In each of these lines there are two integers Ai and Bi, meaning that file Ai references variables defined in file Bi (1 <= i <= N). The last line of the case contains one integer E, which is the file we want to compile.
A negative N denotes the end of input. Else you can assume 0 < N, Ai, Bi, E <= 100.
Output
There is just one line of output for each test case. If file E can be compiled successfully output "Yes", else output "No".
Sample Input
4
1 2
2 3
3 1
3 4
1
4
1 2
2 3
3 1
3 4
4
-1
Sample Output
No
Yes
有向图 叫你判断给定点有没有参与构成环
因为是有向图
所以并查集不能解决
直接dfs搜一波
#include<stdio.h>
#include<iostream>
#include<vector>
#include <cstring>
#include <stack>
#include <cstdio>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include<string>
#include<string.h>
#include<math.h>
typedef long long LL;
using namespace std;
#define max_v 105
int vis[max_v];
int G[max_v][max_v];
int flag;
void dfs(int k)
{
for(int i=;i<max_v;i++)
{
if(vis[i]==&&G[k][i]==)
{
flag=;
return ;
}
if(G[k][i]==)
{
vis[i]=;
dfs(i);
vis[i]=;
}
}
}
int main()
{
int n;
int x,y;
while(~scanf("%d",&n))
{
if(n<)
break;
memset(vis,,sizeof(vis));
memset(G,,sizeof(G)); for(int i=;i<n;i++)
{
scanf("%d %d",&x,&y);
if(x!=y)
G[x][y]=;
}
int k;
scanf("%d",&k); vis[k]=;
flag=; dfs(k); if(flag)
printf("Yes\n");
else
printf("No\n");
}
}
/*
题目意思:
有向图 叫你判断给定点有没有参与构成环 分析:
因为是有向图
所以并查集不能解决
直接dfs搜一波
*/
ZOJ 2475 Benny's Compiler(dfs判断有向图给定点有没有参与构成环)的更多相关文章
- K - Strange Country II 暴力dfs判断有向图是否连通//lxm
You want to visit a strange country. There are n cities in the country. Cities are numbered from 1 t ...
- DFS应用——遍历有向图+判断有向图是否有圈
[0]README 0.1) 本文总结于 数据结构与算法分析, 源代码均为原创, 旨在 理解 "DFS应用--遍历有向图+判断有向图是否有圈" 的idea 并用源代码加以实现 : ...
- HDU3342:判断有向图中是否存在3元环-Tarjan或拓扑排序
题目大意: 给你一个关系图,判断是否合法.每个人都有师父和徒弟,可以有很多个: 若A是B的师父,B是C的师父,则A也算C的师父. 不合法: 1) . 互为师徒:(有回路) 2) .你的师父是你徒弟 ...
- hdu 1325 判断有向图是否为树
题意:判断有向图是否为树 链接:点我 这题用并查集判断连通,连通后有且仅有1个入度为0,其余入度为1,就是树了 #include<cstdio> #include<iostream& ...
- DFS判断连通图
因为是连通图,所以从任意一点出发,一定可以通过一遍深度优先遍历就能走过所有的点和边,就可以利用这个性质来很容易的通过DFS判断图是否为连通图 下面是具体算法:
- <数据结构>XDOJ323.判断有向图中是否有环
问题与解答 问题描述 判断有向图中是否有环. 输入格式 输入数据第一行是一个正整数,表示n个有向图,其余数据分成n组,每组第一个为一个整数,表示图中的顶点个数n,顶点数不超过100,之后为有向图的邻接 ...
- hdu3342-判断有向图中是否存在(至少)3元环或回路-拓扑排序
一:题目大意: 给你一个关系图,判断是否合法, 每个人都有师父和徒弟,可以有很多个: 不合法: 1) . 互为师徒:(有回路) 2) .你的师父是你徒弟的徒弟,或者说你的徒弟是你师父的 ...
- hdu1269迷宫城堡(判断有向图是否是一个强连通图)
1 /* 题意: 给你一个图,求这个有向图示否是一个强连通图(每两个节点都是可以相互到达的)! 思路1:按正向边dfs一遍,将经过的节点计数,如果记录的节点的个数小于n,那么就说明图按照正向边就不是连 ...
- 通过DFS求解有向图(邻接表存储)中所有简单回路
前言 查阅了网上许多关于通过DFS算法对有向图中所有简单回路的查找,发现有很多关于使用DFS求解有向回路中所有简单回路的帖子,(在按照节点编号情况下)但大多数仅仅寻找了编号递增的回路.又或者未对结果去 ...
随机推荐
- 搭建本地svn
1. 下载并安装TortoiseSVN,下载地址为:http://tortoisesvn.net/downloads.html. 2. 在本地创建一个文件夹,作为SVN服务的文件夹. ...
- druapl7:"Notice: A non well formed numeric value encountered 在 _hierarchical_select_hierarchy_generate() "
这个是很诡异的一个Notice错误提醒,因为我在Drupal7.54+PHP7.0.1的环境下,并没有报这个错.但是我再Drupal7.59+PHP7.1.7的环境下就报错了.很奇怪,按照报错信息bi ...
- Android Service不能再详细的教程
这篇包含了: Service后台服务.前台服务.IntentService.跨进程服务.无障碍服务.系统服务 几乎所有Android Service相关的东西. 前言 作为四大组件之一的Service ...
- window 命令行
清屏 cls 启动服务 net start 服务名(nexus.mysql) 关闭服务 net stop 服务名(nexus.mysql) 删除服务 sc delete 服务名 如果服务名有空格,加引 ...
- 7.Spring MVC静态资源访问
在SpringMVC中常用的就是Controller与View.但是我们常常会需要访问静态资源,如html,js,css,image等. 默认的访问的URL都会被DispatcherServlet所拦 ...
- https的网站用了百度分享后网站在浏览器中不安全解决方法
本文出至:新太潮流网络博客 一.先下载百度分享需要的所有东西 下载地址:http://pan.baidu.com/s/1boM7N8V 二.打开百度分享,获取到分享的代码 三.将下载好的百度分享需要的 ...
- Oracle EBS AP 供应商取值
SELECT --nvl(substr(po.vendor_name,1,instr(po.vendor_name,',',1)-1),po.vendor_name) vendor_name, po. ...
- .net验证是否合法邮箱和ip地址的方式
通常情况下第一时间会想到使用正则表达式去验证,但由于正则表达式过于复杂或者没有考虑到某些情况,从而导致问题或者判断的效率低.下面通过另一种方式去判断. 判断是否合法邮箱: /// <summar ...
- Linux chkconfig命令详解
chkconfig命令检查.设置系统的各种服务.这是Red Hat公司遵循GPL规则所开发的程序,它可查询操作系统在每一个执行等级中会执行哪些系统服务,其中包括各类常驻服务.谨记chkconfig不是 ...
- Linode VPS主机套餐方案降低处理方法且不影响数据
使用Linode VPS主机产品经历过512MB内存升级至1GB内存,再升级至2GB内存,以及目前推出1GB内存方案月付10美元.比如我们在使用Linode 2GB内存方案的时候占用资源不是太多,其实 ...