题目1448:Legal or Not

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
Source Code:
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

vector<int> Relationship[110];
int inDegree[110];
queue<int> Q;

void init(int length){
    for(int i=0;i<length;++i){
        inDegree[i]=0;
        Relationship[i].clear();
    }
    while(Q.empty()==false)
        Q.pop();
}

int main(){
    int n,m;
    while(cin>>n>>m){
        if(n==0&&m==0)
            break;
        init(n);
        int a,b;
        for(int i=0;i<m;++i){
            cin>>a>>b;
            ++inDegree[b];
            Relationship[a].push_back(b);
        }
        for(int i=0;i<n;++i){
            if(inDegree[i]==0){
                Q.push(i);
            }
        }
        int count=0;
        while(Q.empty()==false){
            int nowP=Q.front();
            for(int i=0;i<Relationship[nowP].size();++i){
                --inDegree[Relationship[nowP][i]];
                if(inDegree[Relationship[nowP][i]]==0)
                    Q.push(Relationship[nowP][i]);
            }
            Q.pop();
            ++count;
        }
        if(count==n)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}
/**************************************************************
    Problem: 1448
    User: lcyvino
    Language: C++
    Result: Accepted
    Time:10 ms
    Memory:1524 kb
****************************************************************/

时间: 2024-10-17 19:26:40

题目1448:Legal or Not的相关文章

九度 题目1448:Legal or Not

判断是否存在环的问题,本文采用的是拓扑排序,如果输出的节点少于N,则形成了环,和之前的1449几乎是一样的代码 题目链接http://ac.jobdu.com/problem.php?pid=1448 转载请注明本文地址http://blog.csdn.net/yangnanhai93/article/details/41226369 #include <iostream> #include <memory.h> #include <queue> using names

hdu3342Legal or Not

题目链接: 点我点我 题目: Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3909    Accepted Submission(s): 1767 Problem Description ACM-DIY is a large QQ group where many excellent acmers get

oj--9度oj---题目1448:Legal or Not

几个注意点:记得加 using namespace std;有时候本地不加,还是能跑.提交时候忘加了会CE.Q存放的是度为0的点.可新建队列保存结果. #include<cstdio> #include<queue> #include<vector> #include<algorithm> using namespace std; vector<int>list[500]; int indegree[500]; queue<int>

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

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

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116

hdu3342(Legal or Not)----- 学习拓扑排序的好例题

经典拓扑排序 点击打开链接 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 exch

题目1449:确定比赛名次(拓扑排序问题)

题目链接:http://ac.jobdu.com/problem.php?pid=1449 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: // // 1449 确定比赛名次.cpp // Jobdu // // Created by PengFei_Zheng on 21/04/2017. // Copyright © 2017 PengFei_Zheng. All rights reserved. // #include <std

hdu 5168 Legal path

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5168 题意: 一个有向图,给定起点终点,每条边上有权值. 一条合法的路径定义为相邻边的权值之差不小于K的路径,即路径上每条边的权值至少要比上一条边的权值大K,如果上一条边存在.合法路径的长度定义为路径上的边权值总和. 求从起点到终点的合法路径的最短长度. 限制: 有多组数据,第一行为数据组数T(T≤10). 对于每组数据,第一行为三个整数n,m,K,n,m分别表示这组数据的有向图的点数,边数,起点