zoukankan      html  css  js  c++  java
  • HDU 3342 Legal or Not (拓扑排序、有向图是否存在环)

    Legal or Not

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

    Problem Description
    ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many "holy cows" like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to exchange their ideas. When someone has questions, many warm-hearted cows like Lost will come to help. Then the one being helped will call Lost "master", and Lost will have a nice "prentice". By and by, there are many pairs of "master and prentice". But then problem occurs: there are too many masters and too many prentices, how can we know whether it is legal or not?

    We all know a master can have many prentices and a prentice may have a lot of masters too, it's legal. Nevertheless,some cows are not so honest, they hold illegal relationship. Take HH and 3xian for instant, HH is 3xian's master and, at the same time, 3xian is HH's master,which is quite illegal! To avoid this,please help us to judge whether their relationship is legal or not.

    Please note that the "master and prentice" relation is transitive. It means that if A is B's master ans B is C's master, then A is C's master.
     
    Input
    The input consists of several test cases. For each case, the first line contains two integers, N (members to be tested) and M (relationships to be tested)(2 <= N, M <= 100). Then M lines follow, each contains a pair of (x, y) which means x is y's master and y is x's prentice. The input is terminated by N = 0.
    TO MAKE IT SIMPLE, we give every one a number (0, 1, 2,..., N-1). We use their numbers instead of their names.
     
    Output
    For each test case, print in one line the judgement of the messy relationship.
    If it is legal, output "YES", otherwise "NO".
     
    Sample Input
    3 2 0 1 1 2 2 2 0 1 1 0 0 0
     
    Sample Output
    YES NO
     

    题目大意与分析

    给出N个人和M个关系,对于每对关系(a,b),代表a是b的老师,且关系具有传递性,a是b的老师,b是c的老师,a也是c的老师

    但是学生不能是老师的老师,即a是b的老师,那么b就不能是a的老师

    现在要判断给出的M个关系是否合法,即不会出现上述学生是老师的老师的情况。

    很明显的判断有向图是否存在环的问题,直接拓扑排序就可以解决了:进行拓扑排序,将入度为0的点放入队列,然后更新入度,直到没有可以放入队列的为止,如果拓扑排序结束后所有的点都经过了一遍,则合法,否则说明有不能参与排序的点,也就是存在环。

    #include<bits/stdc++.h>
    using namespace std;
    
    int n,m,mp[105][105],into[105],cnt,x,y,vis[105];
    queue<int> q;
    
    void bfs()
    {
        while(!q.empty())
        {
            int now=q.front();
            q.pop();
            for(int i=0;i<n;i++)
            {
                if(mp[now][i]==1)
                {
                    into[i]--;
                }
                if(into[i]==0&&vis[i]==0)
                {
                    q.push(i);
                    cnt++;
                    vis[i]=1;
                }
            }
        }
    }
    
    int main()
    {
        while(cin>>n>>m,n!=0)
        {
            cnt=0;
            memset(mp,0,sizeof(mp));
            memset(into,0,sizeof(into));
            memset(vis,0,sizeof(vis));
            while(m--)
            {
                cin>>x>>y;
                if(mp[x][y]==0)
                {
                    mp[x][y]=1;
                    into[y]++;
                }
            }
            for(int i=0;i<n;i++)
            {
                if(into[i]==0)
                {
                    cnt++;
                    q.push(i);
                    vis[i]=1;
                }
            }
            bfs();
            if(cnt==n)
                cout<<"YES"<<endl;
            else
                cout<<"NO"<<endl; 
        }
     } 
  • 相关阅读:
    Redission分布式锁原理
    【idea】idea自动为类生成文档注释
    【idea】idea自动导包设置
    【idea】idea编译环境改为1.8
    邮件html内容中带内网图片地址发送
    JVM8自适应导致内存居高不下
    分布式自增ID算法snowflake(JAVA版)
    制作 leanote docker 镜像
    Git学习之路(5)- 同步到远程仓库及多人协作问题
    Git学习之路(4)- 撤销操作、删除文件和恢复文件
  • 原文地址:https://www.cnblogs.com/dyhaohaoxuexi/p/12405189.html
Copyright © 2011-2022 走看看