码迷,mamicode.com
首页 > 编程语言 > 详细

HDU3342 Legal or Not【拓扑排序】【链式前向星】

时间:2014-12-19 22:06:40      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:

Legal or Not


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

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


题目大意:给你一个有向图,判断是否有环。

思路:构建拓扑排序,如果排序失败,说明该有向图存在有向环。

另一种思路,用链式前向星存储图,在数据输入的同时统计每个点的入度,

并存入indegree数组,每删除一个点,就遍历以这个点为起点的边,将边

对应的入度减1即可选择并删除下一点。用队列来存储已发现的入度为0的

点,更新入度的同时更新这个队列。如果最终得到队列中的元素个数小于

总的元素个数,说明排序失败,存在环。

第一种方法:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

int N,M,t;
int topo[110],vis[110],G[110][110];

bool dfs(int u)
{
    vis[u] = -1;
    for(int v = 0; v < N; v++)
    {
        if(G[u][v])
        {
            if(vis[v] < 0)
                return false;
            else if(!vis[v] && !dfs(v))
                return false;
        }

    }
    vis[u] = 1;
    topo[--t] = u;
    return true;
}

bool toposort()
{
    t = N;
    memset(vis,0,sizeof(vis));
    for(int u = 0; u < N; u++)
        if(!vis[u])
            if(!dfs(u))
                return false;
    return true;
}
int main()
{
    int a,b;
    while(cin >> N >> M)
    {
        if(!N && !M)
            break;
        memset(G,0,sizeof(G));
        for(int i = 0; i < M; i++)
        {
            cin >> a >> b;
            G[a][b] = 1;
        }
        if(toposort())
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }

    return 0;
}
第二种方法:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN = 110;
const int MAXM = 110;

int head[MAXN],queue[MAXN],indegree[MAXN],N,M;
struct EdgeNode
{
    int to;
    int w;
    int next;
};

EdgeNode Edges[MAXM];

bool toposort()
{
    int iq = 0;
    for(int i = 0; i < N; i++)
    {
        if(indegree[i] == 0)
            queue[iq++] = i;
    }

    for(int i = 0; i < iq; i++)
    {
        for(int k = head[queue[i]]; k != -1; k = Edges[k].next)
        {
            indegree[Edges[k].to]--;
            if(indegree[Edges[k].to] == 0)
            {
                queue[iq++] = Edges[k].to;
            }
        }
    }
    if(iq == N)
        return true;
    return false;
}
int main()
{
    int x,y;
    while(cin >> N >> M)
    {
        if(!N && !M)
            break;
        memset(Edges,0,sizeof(Edges));
        memset(head,-1,sizeof(head));
        memset(indegree,0,sizeof(indegree));
        memset(queue,0,sizeof(queue));
        for(int i = 0; i < M; i++)
        {
            cin >> x >> y;
            Edges[i].to = y;
            Edges[i].w = 1;
            Edges[i].next = head[x];
            head[x] = i;
            indegree[y]++;
        }
        if(toposort())
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }

    return 0;
}




HDU3342 Legal or Not【拓扑排序】【链式前向星】

标签:

原文地址:http://blog.csdn.net/lianai911/article/details/42031407

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!