HDU 3342:Legal or Not(拓扑排序)
Legal or Not
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10898 Accepted Submission(s): 5111
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
题意
给出n个人(编号0~n-1)和m对关系关系,判断有没有出现环
AC代码
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp(1);
const int maxn=1e3+10;
using namespace std;
int n,m;
int a[maxn][maxn];
int vis[maxn];
void toposort()
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(!vis[j])
{
vis[j]--;
for(int k=0;k<n;k++)
{
if(a[j][k])
{
a[j][k]--;
vis[k]--;
}
}
break;
}
}
}
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
while(cin>>n>>m&&n&&m)
{
ms(a);
ms(vis);
int x,y;
for(int i=0;i<m;i++)
{
cin>>x>>y;
if(!a[x][y])
{
a[x][y]=1;
vis[y]++;
}
}
int flag=0;
toposort();
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
flag+=a[i][j];
}
if(flag)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
}
return 0;
}
HDU 3342:Legal or Not(拓扑排序)的更多相关文章
- HDU.3342 Legal or Not (拓扑排序 TopSort)
HDU.3342 Legal or Not (拓扑排序 TopSort) 题意分析 裸的拓扑排序 根据是否成环来判断是否合法 详解请移步 算法学习 拓扑排序(TopSort) 代码总览 #includ ...
- hdu 3342 Legal or Not(拓扑排序)
Legal or Not Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- HDU.1285 确定比赛名次 (拓扑排序 TopSort)
HDU.1285 确定比赛名次 (拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 只不过这道的额外要求是,输出字典序最小的那组解.那么解决方案就是 ...
- 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)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 ...
- hdu 3342 Legal or Not(拓扑排序) HDOJ Monthly Contest – 2010.03.06
一道极其水的拓扑排序……但是我还是要把它发出来,原因很简单,连错12次…… 题意也很裸,前面的废话不用看,直接看输入 输入n, m表示从0到n-1共n个人,有m组关系 截下来m组,每组输入a, b表示 ...
- HDU 3342 Legal or Not (图是否有环)【拓扑排序】
<题目链接> 题目大意: 给你 0~n-1 这n个点,然后给出m个关系 ,u,v代表u->v的单向边,问你这m个关系中是否产生冲突. 解题分析: 不难发现,题目就是叫我们判断图中是否 ...
- hdu 3342 Legal or Not (拓扑排序)
重边这样的东西 仅仅能呵呵 就是裸裸的拓扑排序 假设恩可以排出来就YES . else NO 仅仅须要所有搜一遍就好了 #include <cstdio> #include < ...
随机推荐
- InChat版,仅仅两个接口实现自己的IM系统(可兼容)
InChat 一个IM通讯框架 一个轻量级.高效率的支持多端(应用与硬件Iot)的异步网络应用通讯框架.(核心底层Netty) 版本目标:完成基本的消息通讯(仅支持文本消息),离线消息存储,历史消息查 ...
- 音视频学习系列第(七)篇---MediaCodec的使用
音视频系列 什么是MediaCodec MediaCodec是安卓官方提供的一套用于音视频编码和解码的API,该API是在安卓4.1(API 16)引入的,因此只能用于4.1以上的手机 MediaCo ...
- Android Intent参数传递
Android Intent参数传递 Android参数传递有两种方式, 1.是从一个Activity传递参数到第二个Activity并在第二个Activity获取数据 2.是从第二个Activity ...
- 3-13《元编程》第5章Class Definitions 3-14(5-4Singleton Classes,2小时)3-15(3小时✅)
类宏 环绕别名 singleton class Class就是增强的模块,类定义也就是模块定义. 5.1 Class Definitions Demystified 5.11 Inside Class ...
- Pavel and barbecue CodeForces - 756A (排列,水题)
大意: 给定排列p, 0/1序列b, 有n个烤串, 每秒钟第i串会移动到$p_i$, 若$p_i$为1则翻面, 可以修改b和p, 求最少修改次数使得每串在每个位置正反都被烤过. 显然只需要将置换群合并 ...
- Leetcode 77
//这似乎是排列组合的标准写法了已经class Solution { public: vector<vector<int>> combine(int n, int k) { v ...
- mac连接Windows远程桌面
先打开微软官方的下载面面:http://www.microsoft.com/zh-CN/download/details.aspx?id=18140 下载远程连接客户端 http://jingyan. ...
- maven分别打包开发、生产配置文件
项目工程针对开发和生产有两套配置,开发配置文件目录:src/main/resources/ applicationContext.xml (开发和生产共用) dubbo.propertie ...
- Splunk Enterprise architecture——转发器本质上是日志收集client附加负载均衡,indexer是分布式索引,外加一个集中式管理协调的中心节点
Splunk Enterprise architecture and processes This topic discusses the internal architecture and proc ...
- .NET 性能优化方法总结==转
.NET 性能优化方法总结 目录 目录 1. C#语言方面... 4 1.1 垃圾回收... 4 1.1.1 避免不必要的对象创建... 4 1.1.2 不要使用空析构函数 ★... 4 1.1.3 ...