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求解有向回路中所有简单回路的帖子,(在按照节点编号情况下)但大多数仅仅寻找了编号递增的回路.又或者未对结果去 ...
随机推荐
- markdown 语法备忘
markdwon语法, 增加以下CSS代码,可以对markdwon语法产生的文件进行分页操作. <div style="page-break-after:always;"&g ...
- nginx的启动和关闭
nginx的启动和关闭nginx -h 查看帮助信息nginx -v 查看Nginx的版本号nginx -V 显示Nginx的版本号和编译信息start nginx 启动Nginxnginx -s s ...
- angular2 如何使用websocket
1.npm下载: npm install angular2-websocket 2.需要在哪个组件使用就在那里引入: import {$WebSocket} from 'angular2-websoc ...
- C# 新建文档CreateNewDocument
// Copyright 2010 ESRI// // All rights reserved under the copyright laws of the United States// and ...
- 初次使用git就遭遇不测,提示没有这个服务连接和需要配置git的一个http参数 NO network connection,SSl host could not be verified ...
第一次使用git 拉取服务上的项目到本地,结果,在拿到访问的url地址后,输入用户名密码,失败了. --eclispe 4.5.3 继承了git客户端插件的版本 ------下一步后,报错 NO n ...
- qt cmake
写在前面的话:强烈建议优先阅读Qt官网文档对cmake的使用介绍——CMake Manual 前言我去年用clion写Qt的时候,找了很多教程,也没有什么让我觉得很满意的.后来自己摸索,构建了一个我自 ...
- Array类型
Array类型 Array也是ECMAScript中常用类型之一,其特点是数组中的每一项都可以保存任何类型的数据,数组的大小可以动态调整. 创建数组 方式1:使用Array构造函数 var books ...
- [WSUS] [Windows 10 Upgrade 1607/1703] 升级出错,出现 0xC1800118 或者卡在下载中…… 0%后失败
1. 安装 KB3159706 ,并进行安装后维护操作:https://support.microsoft.com/en-us/help/3159706/update-enables-esd-decr ...
- 理解和配置Out of memory: Kill process
转自:爱开源 理解 OOM killer 最近有位 VPS 客户抱怨 MySQL 无缘无故挂掉,还有位客户抱怨 VPS 经常死机,登陆到终端看了一下,都是常见的 Out of memory 问题.这通 ...
- 优化REST Framework 的 路由 APIView 和ViewSetMixin
APIview: 我们经常写的是view 这个APIview继承了我们的view,并且对请求进来的信息进行设置, 在APIView这个例子中,调用了drf本身的serializer以及Respons ...