zoukankan      html  css  js  c++  java
  • HDU 3342 Legal or Not(拓扑排序)

    描述

    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.

    输入

    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.

    输出

    For each test case, print in one line the judgement of the messy relationship.
    If it is legal, output "YES", otherwise "NO".

    样例输入

    3 2
    0 1
    1 2
    2 2
    0 1
    1 0
    0 0

    样例输出

    YES
    NO
    题意

    给你N个人编号0-N-1,M对关系,(a,b),a是b的师傅,判断所以关系是否合法

    题解

    一道简单的拓扑排序题,如果N的人存在拓扑排序输出YES,否则输出NO

    拓扑排序

    每次找一个入度为0的点(u),删去所有G[u][v]=1(u,v)的边,In[v]--

    如果所有N个点都找过,输出YES

    如果某个点没有找过,输出NO

    代码

     1 #include<cstdio>
     2 int main()
     3 {
     4     int n,m,a,b;
     5     while(scanf("%d%d",&n,&m)!=EOF,n)
     6     {
     7         int In[105]={0},G[105][105]={0};
     8         for(int i=0;i<m;i++)
     9         {
    10             scanf("%d%d",&a,&b);
    11             if(G[a][b]==0)//避免重复
    12             {
    13                 In[b]++;
    14                 G[a][b]=1;
    15             }
    16         }
    17         int cnt=0;
    18         while(cnt<n)
    19         {
    20             int p,flag=0;
    21             for(int i=0;i<n;i++)
    22             {
    23                 if(In[i]==0)
    24                 {
    25                     p=i;
    26                     flag=1;
    27                     In[i]=-1;
    28                     break;
    29                 }
    30             }
    31             if(++cnt==n)//所有n个点都找过
    32             {
    33                 printf("YES
    ");
    34                 break;
    35             }
    36             if(flag==0)//如果某个点不行
    37             {
    38                 printf("NO
    ");
    39                 break;
    40             }
    41             for(int i=0;i<n;i++)
    42             {
    43                 if(G[p][i]==1)//删去边
    44                 {
    45                     In[i]--;
    46                     G[p][i]=0;
    47                 }
    48             }
    49         }
    50     }
    51     return 0;
    52 }

     

     

  • 相关阅读:
    反向传播错漏百出小推导
    使用git遇到的
    Cost Function in Logistic Regression and Neural Network
    鼠标左键长按功能的实现
    同类控件的统一操作(以TCHECKBOX为例)
    HLP帮助文件源文件RTF文件的编写
    FORMAT 的用法
    DELPHI6中DSGNINTF.DCU找不到时的解决方法
    使窗体处于可“移动”,可改变“大小”状态中
    系统菜单的控制,使菜单项灰显及恢复功能
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/8508007.html
Copyright © 2011-2022 走看看