HDU.3342 Legal or Not (拓扑排序 TopSort)

题意分析

裸的拓扑排序 根据是否成环来判断是否合法

详解请移步

算法学习 拓扑排序(TopSort)

代码总览

#include <iostream>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#define nmax 200
#define MEM(x) memset(x,0,sizeof(x))
using namespace std;
int n,m;
int indegree[nmax];
vector<int> v[nmax];
bool suc = true;
void topsort()
{
int cnt = 0;
queue<int> q;
while(1){
for(int i = 0; i<n; ++i){
if(indegree[i] == 0){
q.push(i);
cnt++;
indegree[i] = -1;
}
}
if(q.empty()) break;
while(!q.empty()){
int t = q.front();q.pop();
for(int i = 0; i<v[t].size();++i){
int tt = v[t][i];
if(indegree[tt] == -1){
suc =false;
break;
}else
indegree[tt]--;
}
if(!suc) break;
}
if(!suc) break;
}
if(suc && cnt!=n) suc = false;
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&m) == 2 && n){
MEM(indegree);
suc = true;
for(int i = 0; i<n;++i) v[i].clear();
for(int i = 0; i<m; ++i){
int a,b;
scanf("%d%d",&a,&b);
indegree[b]++;
v[a].push_back(b);
}
topsort();
printf("%s\n",suc?"YES":"NO");
}
return 0;
}

HDU.3342 Legal or Not (拓扑排序 TopSort)的更多相关文章

  1. hdu 3342 Legal or Not(拓扑排序)

    Legal or Not Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

  2. HDU.1285 确定比赛名次 (拓扑排序 TopSort)

    HDU.1285 确定比赛名次 (拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 只不过这道的额外要求是,输出字典序最小的那组解.那么解决方案就是 ...

  3. HDU.2647 Reward(拓扑排序 TopSort)

    HDU.2647 Reward(拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 这道题有一点变化是要求计算最后的金钱数.最少金钱值是888,最少的 ...

  4. HDU 3342 Legal or Not(拓扑排序判断成环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目大意:n个点,m条有向边,让你判断是否有环. 解题思路:裸题,用dfs版的拓扑排序直接套用即 ...

  5. HDU 3342 -- Legal or Not【裸拓扑排序 &amp;&amp;水题 &amp;&amp; 邻接表实现】

    Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  6. HDU 3342 Legal or Not(有向图判环 拓扑排序)

    Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  7. HDU 3342 Legal or Not (最短路 拓扑排序?)

    Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  8. hdu 3342 Legal or Not(拓扑排序) HDOJ Monthly Contest – 2010.03.06

    一道极其水的拓扑排序……但是我还是要把它发出来,原因很简单,连错12次…… 题意也很裸,前面的废话不用看,直接看输入 输入n, m表示从0到n-1共n个人,有m组关系 截下来m组,每组输入a, b表示 ...

  9. HDU 3342 Legal or Not (图是否有环)【拓扑排序】

    <题目链接> 题目大意: 给你 0~n-1 这n个点,然后给出m个关系 ,u,v代表u->v的单向边,问你这m个关系中是否产生冲突. 解题分析: 不难发现,题目就是叫我们判断图中是否 ...

随机推荐

  1. uvaoj1586Molar mass(暴力)

    An organic compound is any member of a large class of chemicalcompounds whose molecules contain carb ...

  2. hdu1181变形课(floyd)

    变形课 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submis ...

  3. Python 关键字参数和可变参数

    关键字参数 如果你有一些具有许多参数的函数,而你又希望只对其中的一些进行指定,那么你可以通过命名它们来给这些参数赋值——这就是python关键字参数(Keyword Arguments)——我们使用命 ...

  4. flex布局笔记

    flex布局: 容器: 容器主轴方向: 项目的主轴对齐方式: space-between:两端对齐,项目之间的间隔都相等. space-around:每个项目两侧的间隔相等.所以,项目之间的间隔比项目 ...

  5. 从零开始的Python学习Episode 5——字典

    字典 字典是另一种可变容器模型,且可存储任意类型对象. 一.添加 (1)直接添加 dict={'name':'smilepup'} dict['age']=20 dict['name']='piggy ...

  6. 使用eclipse创建maven项目出现的一个问题

    错误信息 This error occurs when you employ a plugin that Maven could not download. Possible causes for t ...

  7. ACM hust 2.1

    来自咸鱼王的呻吟 http://www.xiami.com/song/3599639?spm=a1z1s.3521865.23309997.1.PbLu7E 配合咸鱼食用效果更佳(右键新窗口打开) 题 ...

  8. HashMap get()返回值问题

    问题描述:在进行mysql查询必要字段后,需要根据id进行es其它数据字段的查询拼接.使用HashMap以id为key 以查询过来的数据值为value. 代码如下: Map<String,Int ...

  9. Cstring, TCHAR*, char*的转换

    最近老用到Cstring, TCHAR*, char*的转换. 找到一篇写得蛮详细的. 引用过来, 方便自己以后查阅. char是类型TCHAR也是!不过他可以通过是否定义了UNICODE宏来判断到底 ...

  10. iOS- <项目笔记> UIApplication常见属性与方法总结

    UIApplication 1.简介 1> 整个应用程序的象征,一个应用程序就一个UIApplication对象,使用了单例设计模式 2> 通过[UIApplication sharedA ...