题目链接:http://ac.jobdu.com/problem.php?pid=1448

详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus

参考代码:

//
// 1448 Legal or Not.cpp
// Jobdu
//
// Created by PengFei_Zheng on 20/04/2017.
// Copyright © 2017 PengFei_Zheng. All rights reserved.
// #include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cmath>
#include <vector>
#include <queue>
#define MAX_SIZE 110
using namespace std; vector<int> edge[MAX_SIZE];
queue<int> q;
int inDegree[MAX_SIZE]; int n, m; int main(){
while(scanf("%d%d",&n,&m)!=EOF){
if(==n && ==m) break;
for(int i = ; i < n ; i++){
inDegree[i] = ;
edge[i].clear();
}
while(m--){
int a, b;
scanf("%d%d",&a,&b);
inDegree[b]++;
edge[a].push_back(b);
}
while(!q.empty()) {
q.pop();
}
for(int i = ; i < n ; i++){//add all the inDegree node into the queue
if(inDegree[i] == ){
q.push(i);
}
}
int ans = ;
while(!q.empty()){//find all the inDegree == 0 and remove it
int nowP = q.front();
q.pop();
ans++;
for(int i = ; i < edge[nowP].size() ; i++){//and also reduce the node next to the removed node
inDegree[edge[nowP][i]]--;
if(inDegree[edge[nowP][i]] == ){//if inDegree is zero then put this node into the queue
q.push(edge[nowP][i]);
}
}
}
ans == n ? printf("YES\n") : printf("NO\n");
}
return ;
} /**************************************************************
Problem: 1448
User: zpfbuaa
Language: C++
Result: Accepted
Time:10 ms
Memory:1524 kb
****************************************************************/

题目1448:Legal or Not(有向无环图判断——拓扑排序问题)的更多相关文章

  1. C#实现有向无环图(DAG)拓扑排序

    对一个有向无环图(Directed Acyclic Graph简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若边(u,v)∈E(G),则u在线性序列中出现在 ...

  2. 算法87-----DAG有向无环图的拓扑排序

    一.题目:课程排表---210 课程表上有一些课,是必须有修学分的先后顺序的,必须要求在上完某些课的情况下才能上下一门.问是否有方案修完所有的课程?如果有的话请返回其中一个符合要求的路径,否则返回[] ...

  3. CSU 1804: 有向无环图(拓扑排序)

    http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1804 题意:…… 思路:对于某条路径,在遍历到某个点的时候,之前遍历过的点都可以到达它,因此在 ...

  4. 【拓扑】【宽搜】CSU 1084 有向无环图 (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1804 题目大意: 一个有向无环图(DAG),有N个点M条有向边(N,M<=105 ...

  5. 图->有向无环图->拓扑排序

    文字描述 关于有向无环图的基础定义: 一个无环的有向图称为有向无环图,简称DAG图(directed acycline graph).DAG图是一类较有向树更一般的特殊有向图. 举个例子说明有向无环图 ...

  6. 网络流24题 第三题 - CodeVS1904 洛谷2764 最小路径覆盖问题 有向无环图最小路径覆盖 最大流 二分图匹配 匈牙利算法

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - CodeVS1904 题目传送门 - 洛谷2764 题意概括 给出一个有向无环图,现在请你求一些路径,这些路径 ...

  7. CSU 1804 - 有向无环图 - [(类似于)树形DP]

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1804 Bobo 有一个 n 个点,m 条边的有向无环图(即对于任意点 v,不存在从点 ...

  8. 2016 湖南省省赛B题《有向无环图》

    题目链接[https://vjudge.net/problem/CSU-1804] 题意: 给出一个有向无环图,然后让你算下面的结果,count(i,j)表示i->j之间的路径条数. 题解: 根 ...

  9. Taxi Cab Scheme UVALive - 3126 最小路径覆盖解法(必须是DAG,有向无环图) = 结点数-最大匹配

    /** 题目:Taxi Cab Scheme UVALive - 3126 最小路径覆盖解法(必须是DAG,有向无环图) = 结点数-最大匹配 链接:https://vjudge.net/proble ...

随机推荐

  1. CI框架伪静态化配置

    CI框架伪静态化配置 伪静态化,即:去掉入口的index.php, 在url后面加上 .html 后缀 CI默认的rewrite url中是类似这样的,例如你的CI根目录是在/CodeIgniter/ ...

  2. ubuntu安装mongo数据库

    安装mongo数据库,在shell下输入 sudo apt-get install mongodb 如果需要在Python中使用mongo数据库,还需要额外安装Python封装库 pip instal ...

  3. iOS:自定义字体

    转自: <iOS tips: Custom Fonts> Post by Steve Vlaminck My good friend google told me that using a ...

  4. ttl传输中过期

    上renren时遇到一问题,突然间就无法登陆,看了下网络,正常呀,别的网站完全ok,就这不成,所以就ping了一下做以校验:如下图示:传输中过期ttl,这问题少见,新鲜呀:赶紧查了查:原来可能是产生了 ...

  5. JSON.stringify报cyclic object value错误

    这是一个典型的循环引用的错误,一个对象里引用自己就会立刻得到这个错误: obj = { x:555, y: "hi" }; obj.myself = obj; try{ json ...

  6. greenplum的用法

     gp建表的实例         gp 创建外部表的实例:(外部表不能建立分布键)              CREATE EXTERNAL TABLE user_app_tag (         ...

  7. Android 安全提示 笔记

    http://developer.android.com/training/articles/security-tips.html1.数据存储内部存储internal storage存储的数据,只能由 ...

  8. webpack流程图

  9. Linux服务器部署 Elasticsearch 成功,本机却访问不了

    Elasticsearch版本: elasticsearch- 服务器版本: CentOS release 6.8 (Final) 问题: Linux服务器上部署了 Elasticsearch 5.5 ...

  10. MyBatis踩坑记录

    在线文档: 动态SQL  http://www.mybatis.org/mybatis-3/zh/dynamic-sql.html 1. Error setting null for paramete ...