hdu 3342 Legal or Not (topsort)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11170 Accepted Submission(s): 5230
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?
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
C/C++:
#include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <climits>
#include <iostream>
#include <algorithm>
#define INF 0xffffff
using namespace std; const int my_max = ;
int n, m, a, b, my_indeg[my_max];
vector <int> my_G[my_max]; void topsort()
{
int my_cnt = ;
while ()
{
int temp = -;
for (int i = ; i < n; ++ i)
if (my_indeg[i] == )
{
temp = i, my_cnt ++, my_indeg[i] = -;
break;
} if (temp == -) break;
for (int i = ; i < my_G[temp].size(); ++ i)
-- my_indeg[my_G[temp][i]];
my_G[temp].clear();
} if (my_cnt == n)
printf("YES\n");
else
{
for (int i = ; i < n; ++ i)
my_G[i].clear();
printf("NO\n");
}
} int main()
{
while (~scanf("%d%d", &n, &m), n || m)
{
memset(my_indeg, , sizeof(my_indeg));
while (m --)
{
scanf("%d%d", &a, &b);
++ my_indeg[a];
my_G[b].push_back(a);
} topsort();
}
return ;
}
hdu 3342 Legal or Not (topsort)的更多相关文章
- HDU.3342 Legal or Not (拓扑排序 TopSort)
HDU.3342 Legal or Not (拓扑排序 TopSort) 题意分析 裸的拓扑排序 根据是否成环来判断是否合法 详解请移步 算法学习 拓扑排序(TopSort) 代码总览 #includ ...
- hdu 3342 Legal or Not
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Description ACM-DIY is a large QQ g ...
- HDU 3342 Legal or Not(判断是否存在环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Othe ...
- HDU 3342 Legal or Not(拓扑排序判断成环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目大意:n个点,m条有向边,让你判断是否有环. 解题思路:裸题,用dfs版的拓扑排序直接套用即 ...
- HDU 3342 -- Legal or Not【裸拓扑排序 &&水题 && 邻接表实现】
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- hdu 3342 Legal or Not(拓扑排序)
Legal or Not Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- HDU 3342 Legal or Not(有向图判环 拓扑排序)
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 3342 Legal or Not (最短路 拓扑排序?)
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU——3342 Legal or Not
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
随机推荐
- [USACO17JAN]Building a Tall Barn建谷仓
题目描述 Farmer John is building a brand new, NNN -story barn, with the help of his KKK cows ( 1≤N≤K≤101 ...
- cmake::helloworld
ubuntu16. cmake安装 apt-get install cmake 1.创建 CMakeLists.txt , main.cpp 2.cmake . 生成 makefile 3.遇到错误c ...
- 利用requests和BeautifulSoup爬取菜鸟教程的代码与图片并保存为markdown格式
还是设计模式的开卷考试,我想要多准备一点资料,于是写了个爬虫爬取代码与图片,有巧妙地进行格式化进一步处理,最终变为了markdown的格式 import requests from bs4 impor ...
- Java NIO之Java中的IO分类
前言 前面两篇文章(Java NIO之理解I/O模型(一).Java NIO之理解I/O模型(二))介绍了,IO的机制,以及几种IO模型的内容,还有涉及到的设计模式.这次要写一些更贴近实际一些的内容了 ...
- ElasticSearch安装及使用
ElasticSearch安装及使用 ELK由Elasticsearch.Logstash和Kibana三部分组件组成. Elasticsearch 是个开源分布式搜索引擎,它的特点有:分布式,零配置 ...
- Python 调用图灵机器人 API
''' Python3''' import requests #导入requests库 import json #导入json库 key = '3119f1e3610f42c5977ea73c4097 ...
- Java11新特性 - 新加一些实用的API
1. 新的本机不可修改集合API 自从Java9开始,JDK里面为集合(List/Set/Map)都添加了of和copyOf方法,他们可以来创建不可变的集合. Question1:什么叫做不可变集合? ...
- OracleService服务不见了|OracleServiceXE服务没有了
服务里面本来应该有OracleService的(或者是Express版的OracleServiceXE),而服务列表没有此服务项,而启动数据库时出现: TNS监听程序当前无法识别连接描述符中请求的服务 ...
- SpringCloud之异常报警通知(八)
在之前整合降级的基础上,整合redis,达到报警的效果(redis的启动还是之前boot里面整合的redis) order-service pom.xml <dependency> < ...
- 死磕 java线程系列之线程池深入解析——定时任务执行流程
(手机横屏看源码更方便) 注:java源码分析部分如无特殊说明均基于 java8 版本. 注:本文基于ScheduledThreadPoolExecutor定时线程池类. 简介 前面我们一起学习了普通 ...