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 ...
随机推荐
- PHP 调试脚本
如果想要通过php.exe直接运行和调试脚本,可以在PHPStorm配置如下: 1.PHP安装XDebug的扩展. 2.在PHPStorm中,配置XDebug: 1) 打开菜单 "文件&qu ...
- JedisCluster与keys/scan查找
最近买了几个专栏,关于算法.JVM.redis,学不过来.主要是身体也不太好,视物光斑转头疼的问题出现越来越频繁.再加上早上起来嗓子痒打喷嚏.很烦. 稍记录一下redis集群的问题: 1.scan在集 ...
- Google研发面试题
今天很开心找到了一个很不错的研发面试题,很考验临时反应能力,特地拿出来和大家分享一下此题以及自己做该题的心得体会!!! 题目: 总共有12个外表都一样的金属球,其中有11个球的重量是相同的,1个球的重 ...
- python类方法@classmethod与@staticmethod
目录 python类方法@classmethod与@staticmethod 一.@classmethod 介绍 语法 举例 二.@staticmethod 介绍 语法 举例 python类方法@cl ...
- 2019.10.28 CSP%您赛第四场t3
我写不动前两个了. 原谅一下. ____________________________________________________________________________________ ...
- 玩转OneNET物联网平台之MQTT服务⑤ —— OneNet智能灯+MVP框架
授人以鱼不如授人以渔,目的不是为了教会你具体项目开发,而是学会学习的能力.希望大家分享给你周边需要的朋友或者同学,说不定大神成长之路有博哥的奠基石... QQ技术互动交流群:ESP8266&3 ...
- CSS3 变形、过渡、动画、关联属性浅析
一.变形 transform:可以对元素对象进行旋转rotate.缩放scale.移动translate.倾斜skew.矩阵变形matrix.示例: transform: rotate(90deg) ...
- 学习笔记34_EF上下文管理
*上下文对象dbContext最好不要频繁的使用Using(var dbContext = new ....):那么就会产生过多的数据库交互:而且每个dbContext中村的数据,由于操作不同,数据可 ...
- postman发送json参数到后端controller报错415的解决方案
警告 [http-nio-8080-exec-9] org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.l ...
- python之装饰器的两种写法
上一篇文章介绍了 装饰器的概念.现在讲一下在程序中怎么来写装饰器.上代码: def X(fun): def Y(b): print(b) fun() return Y def test(): prin ...