zoukankan      html  css  js  c++  java
  • ZOJ 2475 Benny's Compiler(dfs判断有向图给定点有没有参与构成环)

    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=1;i<max_v;i++)
        {
            if(vis[i]==1&&G[k][i]==1)
            {
                flag=0;
                return ;
            }
            if(G[k][i]==1)
            {
                vis[i]=1;
                dfs(i);
                vis[i]=0;
            }
        }
    }
    int main()
    {
        int n;
        int x,y;
        while(~scanf("%d",&n))
        {
            if(n<0)
                break;
            memset(vis,0,sizeof(vis));
            memset(G,0,sizeof(G));
    
            for(int i=0;i<n;i++)
            {
                scanf("%d %d",&x,&y);
                if(x!=y)
                    G[x][y]=1;
            }
            int k;
            scanf("%d",&k);
    
            vis[k]=1;
            flag=1;
    
            dfs(k);
    
            if(flag)
                printf("Yes
    ");
            else
                printf("No
    ");
        }
    }
    /*
    题目意思:
    有向图 叫你判断给定点有没有参与构成环
    
    分析:
    因为是有向图
    所以并查集不能解决
    直接dfs搜一波
    */
     
  • 相关阅读:
    《iBoard 是什么》之简介
    [iBoard 电子学堂][第八卷 设计任意波发生器]第一篇 iBoard 任意波发生器简介
    [iBoard 电子学堂][第〇卷 电子基础]第二篇 电路图与印刷电路板
    职业规划提示
    督查督办工作基本程序
    今天收拾了个电脑抽屉,发现原来我是个有钱人
    VB之SendKeys键盘模拟
    天骄辅助外挂制作,寻求合作
    用CE找武林外传一级基址的方法
    asp实现数据记录的备份及恢复抛砖引玉
  • 原文地址:https://www.cnblogs.com/yinbiao/p/9494609.html
Copyright © 2011-2022 走看看