zoukankan      html  css  js  c++  java
  • Legal or Not

    Legal or Not

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
    Total Submission(s) : 43   Accepted Submission(s) : 26
    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
    代码:两种方法:
      1 /*#include<stdio.h>
      2 #include<string.h>
      3 const int MAXN=110;
      4 int map[MAXN][MAXN];
      5 int que[MAXN];
      6 int N,M,flot;
      7 void topsort(){int k;
      8     for(int i=0;i<N;i++){
      9             int temp=-1;
     10         for(int j=0;j<N;j++){
     11             if(!que[j]){
     12             k=j;
     13             flot++;
     14         que[k]=-1;
     15         temp=0;
     16             break;
     17         }
     18         }
     19         if(temp==-1)break;
     20         for(int j=0;j<N;j++){
     21             if(map[k][j])que[j]--;
     22         }
     23     }
     24     if(flot==N)puts("YES");
     25     else puts("NO");
     26 }
     27 void initial(){
     28     memset(map,0,sizeof(map));
     29     memset(que,0,sizeof(que));
     30     flot=0;
     31 }
     32 int main(){
     33     int a,b;
     34         while(~scanf("%d%d",&N,&M),N){
     35                 initial();
     36             while(M--){
     37                 scanf("%d%d",&a,&b);
     38                 if(!map[a][b]){
     39                     map[a][b]=1;
     40                     que[b]++;
     41                 }
     42             }
     43             topsort();
     44         }
     45     return 0;
     46     }
     47 */
     48 #include<stdio.h>
     49 #include<string.h>
     50 #include<queue>
     51 using namespace std;
     52 const int MAXN=110;
     53 struct Node{
     54     int to,next;
     55 };
     56 Node edg[MAXN];
     57 int head[MAXN];
     58 int que[MAXN],N,flot;
     59 priority_queue<int,vector<int>,greater<int> >dl;
     60 void topsort(){
     61    // puts("fasdf/--");
     62     for(int i=0;i<N;i++){
     63             //printf("%d
    ",i);
     64         if(!que[i])dl.push(i);
     65     }
     66     //puts("fasdf/..");
     67     while(!dl.empty()){
     68         int k=dl.top();
     69         dl.pop();
     70         flot++;
     71         for(int j=head[k];j!=-1;j=edg[j].next){
     72             que[edg[j].to]--;
     73             if(!que[edg[j].to])dl.push(edg[j].to);
     74         }
     75     }
     76    // puts("fasdf/++");
     77     if(flot==N)puts("YES");
     78     else puts("NO");
     79 }
     80 void initial(){
     81     memset(head,-1,sizeof(head));
     82     memset(que,0,sizeof(que));
     83     while(!dl.empty())dl.pop();
     84     flot=0;
     85 }
     86 int main(){
     87     int M,a,b;
     88     while(~scanf("%d%d",&N,&M),N){
     89             initial();
     90         for(int i=0;i<M;i++){
     91                 scanf("%d%d",&a,&b);
     92             edg[i].to=b;
     93             edg[i].next=head[a];
     94             head[a]=i;
     95             que[b]++;
     96         }
     97         //puts("fasdf/**");
     98         topsort();
     99     }
    100     return 0;
    101 }
  • 相关阅读:
    SQL Server死锁产生原因及解决办法
    SqlServer表死锁的解决方法
    SQL Server中解决死锁的新方法介绍
    SQL Server 中WITH (NOLOCK)浅析
    二分图匹配
    java list三种遍历方法性能比较
    CSharp Algorithm
    存几个html画图的网站
    [HDU 1358]Period[kmp求周期]
    Android解析Excel文档完整示例
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4732553.html
Copyright © 2011-2022 走看看