B - Benny's Compiler

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搜一波
 
code:
#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判断有向图给定点有没有参与构成环)的更多相关文章

  1. 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 ...

  2. DFS应用——遍历有向图+判断有向图是否有圈

    [0]README 0.1) 本文总结于 数据结构与算法分析, 源代码均为原创, 旨在 理解 "DFS应用--遍历有向图+判断有向图是否有圈" 的idea 并用源代码加以实现 : ...

  3. HDU3342:判断有向图中是否存在3元环-Tarjan或拓扑排序

    题目大意: 给你一个关系图,判断是否合法.每个人都有师父和徒弟,可以有很多个: 若A是B的师父,B是C的师父,则A也算C的师父. 不合法:  1) . 互为师徒:(有回路)  2) .你的师父是你徒弟 ...

  4. hdu 1325 判断有向图是否为树

    题意:判断有向图是否为树 链接:点我 这题用并查集判断连通,连通后有且仅有1个入度为0,其余入度为1,就是树了 #include<cstdio> #include<iostream& ...

  5. DFS判断连通图

    因为是连通图,所以从任意一点出发,一定可以通过一遍深度优先遍历就能走过所有的点和边,就可以利用这个性质来很容易的通过DFS判断图是否为连通图 下面是具体算法:

  6. <数据结构>XDOJ323.判断有向图中是否有环

    问题与解答 问题描述 判断有向图中是否有环. 输入格式 输入数据第一行是一个正整数,表示n个有向图,其余数据分成n组,每组第一个为一个整数,表示图中的顶点个数n,顶点数不超过100,之后为有向图的邻接 ...

  7. hdu3342-判断有向图中是否存在(至少)3元环或回路-拓扑排序

    一:题目大意:   给你一个关系图,判断是否合法,    每个人都有师父和徒弟,可以有很多个:  不合法:  1) . 互为师徒:(有回路)  2) .你的师父是你徒弟的徒弟,或者说你的徒弟是你师父的 ...

  8. hdu1269迷宫城堡(判断有向图是否是一个强连通图)

    1 /* 题意: 给你一个图,求这个有向图示否是一个强连通图(每两个节点都是可以相互到达的)! 思路1:按正向边dfs一遍,将经过的节点计数,如果记录的节点的个数小于n,那么就说明图按照正向边就不是连 ...

  9. 通过DFS求解有向图(邻接表存储)中所有简单回路

    前言 查阅了网上许多关于通过DFS算法对有向图中所有简单回路的查找,发现有很多关于使用DFS求解有向回路中所有简单回路的帖子,(在按照节点编号情况下)但大多数仅仅寻找了编号递增的回路.又或者未对结果去 ...

随机推荐

  1. PoPo数据可视化周刊第6期

    PoPo数据可视化 聚焦于Web数据可视化与可视化交互领域,发现可视化领域有意思的内容.不想错过可视化领域的精彩内容, 就快快关注我们吧 :) 本期可视化精彩视频请关注公众号浏览 全天智能获Pre-A ...

  2. oracel存储过程编写 以及plsql存储过程的debug

    1.语法: create or replace procedure messagebackup_createTable       //此处存储过程名称不能超过30个字符 as  tableName ...

  3. 应用ArcGIS Server JavaScript API实现地图卷帘效果实现

    var maskDynamicMapServiceLayer = null; var maskDynamicMapServiceLayerDiv = null; var pointNumb = 0; ...

  4. LK光流算法的三个假设

    在实际过程中采用 Lucas-Kanade 光流算法跟踪运动物体特征点的时候,一个很明显的特点是LK算法(包括其他光流算法)不能计算"大运动",加上金子塔的方法稍微好点. 这是什么 ...

  5. Pwn with File结构体之利用 vtable 进行 ROP

    前言 本文以 0x00 CTF 2017 的 babyheap 为例介绍下通过修改 vtable 进行 rop 的操作 (:-_- 漏洞分析 首先查看一下程序开启的安全措施 18:07 haclh@u ...

  6. doPost方法不支持 a 标签和地址栏直接输入地址访问

    demo <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  7. Pig store用法举例

    store:将数据存储到HDFS等文件系统里   将数据保存到/data目录 store data into '/data'; 以逗号为分隔符 store data into '/data' usin ...

  8. Oracle Sequence Cache 参数说明

    转自 http://blog.csdn.net/tianlesoftware/article/details/5995051 之前整理的一篇文章: ORACLE SEQUENCE 介绍 http:// ...

  9. sun.misc.BASE64Encoder在Eclipse中不能直接使用的原因和解决方案

    1.为什么在Eclipse中不能直接使用sun.misc.BASE64Encoder和sun.misc.BASE64Decoder呢? 因为sun.misc.BASE64Encoder和sun.mis ...

  10. windows下建立netcore控制台程序,然后传送到centos7下的docker容器里运行

    1.首先,在window下用vs2017开发netcore控制台项目. 2.把建立好的项目传送到centos7下面的容器里. docker cp sharefoldersforwindows/ 359 ...