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求解有向回路中所有简单回路的帖子,(在按照节点编号情况下)但大多数仅仅寻找了编号递增的回路.又或者未对结果去 ...
随机推荐
- windows 查看端口
windowsnetstat命令查看进程:netstat -ano查看占用端口进程:netstat -ano|findstr “端口号”,例子netstat -ano|findstr “8080”.t ...
- Dialog中更新Activity的数据显示
假设有一个activity,activity中有一个Button和一个TextView,点击按钮,弹出Dialog,对话框中有一个ListView,选中ListView中的某一项,关闭对话框,更新ac ...
- Android 通过URL获取网络资源
1.先在AndroidManifest.xml中注册加入访问因特网服务的权限: <uses-permission android:name="android.permission.IN ...
- 润乾V4报表放入WEBINF保护目录下如何实现
润乾报表放入WEBINF保护目录下如何实现 WEB-INF下面的文件都是受保护的,客户为了保护项目的文件不受到非法的访问,jsp页面都放在WEB-INF下,那润乾报表放入WEB-INF保护目录下 ...
- the detailed annotation of StringBuilder
public int capacity() 返回当前容量.容量指可用于最新插入字符的存储量,超过这一容量便需要再次分配. 返回: 当前容量. public int length() 返回长度(字符数) ...
- 带你从零学ReactNative开发跨平台App开发[expo 打包发布](八)
ReactNative跨平台开发系列教程: 带你从零学ReactNative开发跨平台App开发(一) 带你从零学ReactNative开发跨平台App开发(二) 带你从零学ReactNative开发 ...
- c# 通过html导出pdf,带分页
通过NuGet安装 PechkinPechkin.Synchronized 一下示例是控制台应用程序 static void btnCreate() { SynchronizedPechkin sc ...
- 《Pro Git》轻松学习版本控制
转自 https://kindlefere.com/post/333.html 什么是“版本控制”?我为什么要关心它呢?版本控制是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统.在 ...
- Oracle EBS 跳跳转标准销售订单程序转标准销售订单程序
-- 打开PO PROCEDURE Btn_Open_Po IS BEGIN Fnd_Function.Execute(Function_Name => 'PO_POXPOEPO', Open_ ...
- mysql root更改远程登录
mysql> select user,host from mysql.user; +---------------+-------------+ | user | host | +------- ...