UVA.10305 Ordering Tasks

题意分析

详解请移步

算法学习 拓扑排序(TopSort)

拓扑排序的裸题

基本方法是,indegree表示入度表,vector存后继节点。在topsort函数中,制造一个辅助队列,首先从入度表中找到入度为0的点作起点,并且置入度为-1。接着依次处理队列中的节点,首先根据他们的后继,将其后继节点的入度依次减1,若其后继节点中的入度存在-1的,说明成环,则不存在拓扑排序。紧接着再从入度表中找到入度为0的节点,加入到队列中,直到队列空。当退出while循环的时候,需要检查ans答案队列中是否已经有全部的节点,若其数量为n,则表明存在拓补排序,否则不存在。

代码总览

#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;
vector<int> v[nmax];
int indegree[nmax];
int n;
bool suc = true;
queue<int> ans;
void topsort()
{
queue<int> q;
while(1){
for(int i = 1; i<=n ;++i){
if(indegree[i] == 0){
q.push(i);
ans.push(i);
indegree[i] = -1;
}
}
if(q.empty()) break;
while(!q.empty()){
int t = q.front(); q.pop();
for(int j = 0;j<v[t].size();++j){
int tt = v[t][j];
if(indegree[tt] == -1){
suc = false;
break;
}else indegree[tt]--;
}
v[t].clear();
if(!suc) break;
}
if(!suc) break;
}
if(ans.size() <n){
suc =false;
return;
}
}
void output()
{
bool isfirst = true;
while(!ans.empty()){
int t = ans.front(); ans.pop();
if(isfirst){
printf("%d",t);
isfirst = false;
}else
printf(" %d",t);
}
printf("\n");
}
int main()
{
//freopen("in.txt","r",stdin);
int m;
while(scanf("%d%d",&n,&m) ==2 && (n||m)){
MEM(indegree);
suc = true;
int a,b;
for(int i = 0; i<m; ++i){
scanf("%d%d",&a,&b);
indegree[b]++;
v[a].push_back(b);
}
topsort();
if(suc) output();
else printf("failed\n");
}
return 0;
}

UVA.10305 Ordering Tasks (拓扑排序)的更多相关文章

  1. UVa 10305 - Ordering Tasks (拓扑排序裸题)

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  2. Uva 10305 - Ordering Tasks 拓扑排序基础水题 队列和dfs实现

    今天刚学的拓扑排序,大概搞懂后发现这题是赤裸裸的水题. 于是按自己想法敲了一遍,用queue做的,也就是Kahn算法,复杂度o(V+E),调完交上去,WA了... 于是检查了一遍又交了一发,还是WA. ...

  3. UVA 10305 Ordering Tasks(拓扑排序的队列解法)

    题目链接: https://vjudge.net/problem/UVA-10305#author=goodlife2017 题目描述 John有n个任务,但是有些任务需要在做完另外一些任务后才能做. ...

  4. Ordering Tasks UVA - 10305 图的拓扑排序

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  5. UVA - 10305 Ordering Tasks(拓扑排序)

    题意:给定优先关系进行拓扑排序. 分析:将入度为0的点加入优先队列,并将与之相连的点入度减1,若又有度数为0的点,继续加入优先队列,依次类推. #pragma comment(linker, &quo ...

  6. UVa 10305 Ordering Tasks (例题 6-15)

    传送门: https://uva.onlinejudge.org/external/103/10305.pdf 拓扑排序(topological sort)简单题 自己代码的思路来自: ==>  ...

  7. M - Ordering Tasks(拓扑排序)

    M - Ordering Tasks Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Descri ...

  8. Ordering Tasks 拓扑排序

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  9. uva 10305 ordering tasks(超级烂题)——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABHIAAAHDCAYAAABI5T2bAAAgAElEQVR4nOydPY7svLW1awQGNABHCm

随机推荐

  1. 180709-Java实现获取本机Ip的工具类

    180709-Java实现获取本机Ip的工具类 获取本机Ip算是比较常见的一个需求场景了,比如业务报警,可能就会带上出问题的机器IP,方便直接上去看日志定位问题,那么问题来了,如何获取机器IP呢? I ...

  2. Selenium(Python)等待元素出现

    1.显式等待 from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdri ...

  3. NGUI组件整理总结

    一图流: 注意: private void RClickUI(Vector3 newPos) { this.gameObject.SetActive(true); this.transform.loc ...

  4. 用Python实现一个端口扫描,只需简单几步就好

    一.常见端口扫描的原理 0.秘密扫描 秘密扫描是一种不被审计工具所检测的扫描技术. 它通常用于在通过普通的防火墙或路由器的筛选(filtering)时隐藏自己. 秘密扫描能躲避IDS.防火墙.包过滤器 ...

  5. 孤荷凌寒自学python第八十六天对selenium模块进行较详细的了解

    孤荷凌寒自学python第八十六天对selenium模块进行较详细的了解 (今天由于文中所阐述的原因没有进行屏幕录屏,见谅) 为了能够使用selenium模块进行真正的操作,今天主要大范围搜索资料进行 ...

  6. hdu刷题1

    1002  大数加法 #include<iostream> #include<cstring> using namespace std; int main() { ],b[]; ...

  7. spark操作数据库的几种方法

    一.使用jdbcRDD的接口: SparkConf conf = new SparkConf(); conf.setAppName("Simple Application").se ...

  8. HDU 3262/POJ 3829 Seat taking up is tough(模拟+搜索)(2009 Asia Ningbo Regional)

    Description Students often have problems taking up seats. When two students want the same seat, a qu ...

  9. Swift 泛型和闭包结合使用

    通常在Swift中定义一个闭包来使用 typealias Closure= (Any?) -> () var tempClosure :Closure? /// 定义一个方法直接调用 func ...

  10. 3dContactPointAnnotationTool开发日志(五)

      今天要做的第一件事就是把obj文件里不同的对象分割开来.   通过仔细观察发现obj文件中以"o "开头的行会跟着一个对象的名字.g代表对象所属组名,我这里只要用到对象名就行了 ...