zoukankan      html  css  js  c++  java
  • HDU 5154 Harry and Magical Computer bfs

    Harry and Magical Computer

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 499    Accepted Submission(s): 233


    Problem Description
    In reward of being yearly outstanding magic student, Harry gets a magical computer. When the computer begins to deal with a process, it will work until the ending of the processes. One day the computer got n processes to deal with. We number the processes from 1 to n. However there are some dependencies between some processes. When there exists a dependencies (a, b), it means process b must be finished before process a. By knowing all the m dependencies, Harry wants to know if the computer can finish all the n processes.
     
    Input
    There are several test cases, you should process to the end of file.
    For each test case, there are two numbers n m on the first line, indicates the number processes and the number of dependencies. 1n100,1m10000
    The next following m lines, each line contains two numbers a b, indicates a dependencies (a, b). 1a,bn
     
    Output
    Output one line for each test case.
    If the computer can finish all the process print "YES" (Without quotes).
    Else print "NO" (Without quotes).
     
    Sample Input
    3 2 3 1 2 1 3 3 3 2 2 1 1 3
     
    Sample Output
    YES NO
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 100001
    const int inf=0x7fffffff;   //无限大 
    int map[101][101];
    int vis[101];
    int flag[101];
    int main()
    {
        int n,m;
        while(cin>>n>>m)
        {
            memset(map,0,sizeof(map));
            memset(vis,0,sizeof(vis));
            memset(flag,0,sizeof(flag));
            int a,b;
            for(int i=0;i<m;i++)
            {
                cin>>a>>b;
                map[b-1][a-1]=1;
                flag[a-1]=1;
            }
            queue<int> q;
            for(int i=0;i<n;i++)
            { 
                if(flag[i]==0)
                {
                    q.push(i);
                    vis[i]=1;
                }
            }
            int now;
            int next;
            while(!q.empty())
            {
                now=q.front();
                for(int i=0;i<n;i++)
                {
                    if(map[now][i]==1)
                    {
                        if(vis[i]==1)
                            continue;
                        q.push(i);
                        vis[i]=1;
                    }
                }
                q.pop();
            }
            int flag1=0;
            for(int i=0;i<n;i++)
            {
                if(vis[i]==0)
                {
                    flag1=1;
                    break;
                }
            }
            if(flag1==1)
                cout<<"NO"<<endl;
            else
                cout<<"YES"<<endl;
        }
    }
  • 相关阅读:
    API文档大集合
    jenkins 构建 job 并获取其状态的实现
    jenkins 插件乱码处理与文件上传
    更优雅的配置:docker/运维/业务中的环境变量
    部署仓库中 nginx 下游依赖配置改进
    dotnet core 在 MIPS64 下的移值进度:EA 版本已经发布
    tmux 编译安装过程
    各数据源的时间/日期的提取能力对比
    关于若干性能指标的阐述
    为缓存、外部接口调用添加超时处理
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4215538.html
Copyright © 2011-2022 走看看