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. JavaScript document和window属性及方法详解

    [document对象] 该对象是window和frames对象的一个属性,是显示于窗口或框架内的一个文档. 属性 alinkColor 活动链接的颜色(ALINK)  anchor 一个HTMI锚点 ...

  2. CSS字体无法设置成功的问题

    在 CSS 中设置字体名称,直接写中文是可以的.但是在文件编码(GB2312.UTF-8 等)不匹配时会产生乱码的错误.xp 系统不支持 类似微软雅黑的中文. 方案一: 你可以使用英文来替代. 比如 ...

  3. JavaWEB SSH文件上传

    一.提交表单的<form> method属性必须为post  并且添加enctype="multipart/form-data" 属性 前台: <td>上传 ...

  4. opencv3.2.0形态学滤波之腐蚀

    /* 腐蚀(erode)含义: 腐蚀和膨胀是相反的一对操作,所以腐蚀就是求局部最小值的操作,腐蚀操作使原图中 国的高亮部分被腐蚀,效果图比原图有更小的高亮的区域. 腐蚀函数原型API及参数同膨胀相同 ...

  5. Java基础学习—思维导图

    找到两张Java学习的思维导图,特别适合我这样的菜鸟学习,贴过来和小伙伴分享.

  6. 【Python】Java程序员学习Python(二)— 开发环境搭建

    巧妇难为无米之炊,我最爱的还是鸡蛋羹,因为我和鸡蛋羹有段不能说的秘密. 不管学啥,都要有环境,对于程序员来说搭建个开发环境应该不是什么难题.按顺序一步步来就可以,我也只是记录我的安装过程,你也可以滴. ...

  7. leetCode题解之Reshape the Matrix

    1.题目描述 2.分析 使用了一个队列. 3.代码 vector<vector<int>> matrixReshape(vector<vector<int>& ...

  8. [翻译] RAReorderableLayout

    RAReorderableLayout A UICollectionView layout which you can move items with drag and drop. 一种UIColle ...

  9. 使用UICollectionView

    使用UICollectionView 使用UICollectionView的流程: 1. 设定一个UICollectionViewFlowLayout 2. 使用这个设定的UICollectionVi ...

  10. 为什么mysql要做主从复制?

    为什么MySQL要做主从复制(读写分离)? 通俗来讲,如果对数据库的读和写都在同一个数据库服务器中操作,业务系统性能会降低. 为了提升业务系统性能,优化用户体验,可以通过做主从复制(读写分离)来减轻主 ...