首页 > 代码库 > 九度 题目1448:Legal or Not

九度 题目1448:Legal or Not

题目1448:Legal or Not

时间限制:1 秒

内存限制:128 兆

特殊判题:

提交:1071

解决:485

题目描述:

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
这道题我用的是,邻接矩阵存储,然后看这个图能不能拓扑排序,若能拓扑排序,则输出YES;否则即为NO;
代码如下,写了两个函数,一个是Topo,一个是Topo2,调用哪一个都可以;整体思路还是差不多的:
#include <stdio.h>
#include <string.h>

int Graph[101][101];
int InDegree[101];

int Topo(int N){
    int count = 0;
    int i;
    while(count < N){
            //去掉入度为0的顶点
            int have = 0;//一开始没有加have一直报错!加了have = 0是表明
                        //若一趟判断没有一个入度为0的顶点,则说明无法拓扑
                        //只要有一个入度为0的顶点,则置have为1,进行拓扑!
            for(i = 0; i < N; i++){
                if(InDegree[i] == 0){
                    have = 1;
                    InDegree[i] = -1;
                    for(int j = 0; j < N; j++){
                        if(Graph[i][j] == 1){
                            Graph[i][j] = 0;
                            InDegree[j]--;
                        }
                    }
                    count++;
                }
            }
            if(have == 0) return 0;
        }
        return 1;
}

int Topo2(int n){//有改进了一下,时间复杂度要低一些
    for(int i = 0; i < n; i++){
        int flag = n;
        for(int j = 0; j < n; j++){//寻找第一个入度为0的顶点
            if(InDegree[j] == 0){
                InDegree[j] = -1;
                flag = j;
                break;
            }
        }
        if(flag == n) return 0;//如果查找一趟没有入度为0的顶点,则无法拓扑,直接返回0;
        for(int k = 0; k < n; k++){//否则更新图,更新相应的顶点入度
            if(Graph[flag][k] == 1){
                InDegree[k]--;
                Graph[flag][k] = 0;
            }
        }
    }
    return 1;
}


int main(){
    int N,M;
    int a,b;
    while(scanf("%d %d",&N,&M) != EOF && N != 0){
        //初始化
        int flag = 0;
        memset(Graph,0,sizeof(Graph));
        memset(InDegree,0,sizeof(InDegree));
        //scanf("%d",&M);
        for(int i = 0; i < M; i++){
            scanf("%d %d",&a,&b);
            if(Graph[a][b] == 0){//去重!!!!!!
                Graph[a][b] = 1;
                InDegree[b]++;
            }
        }
        //flag = Topo(N);
        flag = Topo2(N);
        if(flag == 1){
            printf("YES\n");
        }else{
            printf("NO\n");
        }
    }
    return 0;
}

九度 题目1448:Legal or Not