UVA - 10305 【拓扑排序】
题意
给出一些任务的优先级别 将这些任务进行的时间 进行先后排序
思路
拓扑排序
将所以有先后关系的任务都连一条边
然后每次 输出 度为0 的任务
每次把 以这个任务为弧的边 都取消 相对应任务的度也-1
再循环
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;
const double PI = acos(-1.0);
const double E = exp(1);
const double eps = 1e-30;
const int INF = 0x3f3f3f3f;
const int maxn = 1e2 + 5;
const int MOD = 1e9 + 7;
int G[maxn][maxn];
int degree[maxn];
int v[maxn];
int n, m;
vector <int> ans;
int Count;
void dfs()
{
for (int i = 1; i <= n; i++)
{
if (degree[i] == 0 && v[i] == 0)
{
Count++;
ans.pb(i);
v[i] = 1;
for (int j = 1; j <= n; j++)
{
if (G[i][j])
{
G[i][j] = 1;
degree[j]--;
}
}
}
}
if (Count != n)
dfs();
}
int main()
{
while (scanf("%d%d", &n, &m) && (n || m))
{
CLR(G);
CLR(degree);
CLR(v);
int x, y;
for (int i = 0; i < m; i++)
{
scanf("%d%d", &x, &y);
G[x][y] = G[y][x] = 1;
degree[y]++;
}
ans.clear();
Count = 0;
dfs();
vector <int>::iterator it;
for (it = ans.begin(); it != ans.end(); it++)
{
if (it != ans.begin())
printf(" ");
printf("%d", (*it));
}
cout << endl;
}
}
UVA - 10305 【拓扑排序】的更多相关文章
- UVa 10305 (拓扑排序) Ordering Tasks
题意: 经典的拓扑排序.有n个任务,然后某些任务必须安排在某些任务前面完成,输出一种满足要求的序列. 分析: 拓扑排序用离散里面的话来说就是将偏序关系拓展为全序关系.我们将“小于”这种关系看做一条有向 ...
- 【紫书】Ordering Tasks UVA - 10305 拓扑排序:dfs到底再输出。
题意:给你一些任务1~n,给你m个数对(u,v)代表做完u才能做v 让你给出一个做完这些任务的合理顺序. 题解:拓扑排序版题 dfs到底再压入栈. #define _CRT_SECURE_NO_WAR ...
- Uva 10305 拓扑排序
题意: 给定n个点,与m条边, 给出他们的拓扑排序. 分析: 拓扑排序可以有两种做法, 第一种是dfs, 每次都找到某一个点的终点, 然后加入序列末尾, 正在访问的标记为-1, 访问过的标记为1, 未 ...
- uva 10305 拓扑排序裸题
https://vjudge.net/problem/UVA-10305 目前没学dfs做法,用的队列做法,每次找到一个入度为零的点出队后更新其他点,再加入入度为零的点直到查找完毕,这个题目显然一定有 ...
- uva 1423 拓扑排序
刘书上例题 拓扑排序 #include <cstdio> #include <cstdlib> #include <cmath> #include <map ...
- UVa 1572 (拓扑排序) Self-Assembly
题意: 有n种正放形,每种正方形的数量可视为无限多.已知边与边之间的结合规则,而且正方形可以任意旋转和反转,问这n中正方形是否可以拼成无限大的图案. 分析: 首先因为可以旋转和反转,所以可以保证在拼接 ...
- UVA.10305 Ordering Tasks (拓扑排序)
UVA.10305 Ordering Tasks 题意分析 详解请移步 算法学习 拓扑排序(TopSort) 拓扑排序的裸题 基本方法是,indegree表示入度表,vector存后继节点.在tops ...
- Ordering Tasks UVA - 10305 图的拓扑排序
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- Uva 10305 - Ordering Tasks 拓扑排序基础水题 队列和dfs实现
今天刚学的拓扑排序,大概搞懂后发现这题是赤裸裸的水题. 于是按自己想法敲了一遍,用queue做的,也就是Kahn算法,复杂度o(V+E),调完交上去,WA了... 于是检查了一遍又交了一发,还是WA. ...
- [拓扑排序]Ordering Tasks UVA - 10305
拓扑排序模版题型: John has n tasks to do.Unfortunately, the tasks are not independent and the execution of o ...
随机推荐
- Android性能优化第(三)篇---MAT比Menmery Monitor更强大
作者 LooperJing 2016.11.17 16:42* 字数 1687 阅读 1603评论 3喜欢 21 在Android性能优化第(一)篇---基本概念中讲了JAVA的四大引用,讲了一下GC ...
- hdu254 DFS+BFS
这个题目需要注意以下几点: 1)注意界线问题,箱子和人不可以越界. 2)需要判断人是否可以到达人推箱子的指定位置. 3)不可以用箱子作为标记,因为箱子可以走原来走过的地方,我们用箱子和人推箱子的方向来 ...
- C# 中的结构类型(struct type)
ylbtech- .NET-Basic:C# 中的结构类型(struct type) C# 中的结构类型(struct type) 1.A,相关概念返回顶部 像类一样,结构(struct)是能够包 ...
- C++/C# 托管扩展 更改概要 [转]
源文 :https://msdn.microsoft.com/zh-cn/library/ms235298%28v=vs.100%29.aspx Visual Studio 2010 其他版本 此概要 ...
- memcpy( )的使用以及迭代器的使用
memcpy() -- 拷贝内存内容 相关函数: bcopy(), memccpy(), memmove(), strcpy(), strncpy() 表头文件: #include <strin ...
- RF--- selenium
- IP地址 网段的划分
IP和子网掩码我们都知道,IP是由四段数字组成,在此,我们先来了解一下3类常用的IP A类IP段 0.0.0.0 到127.255.255.255 B类IP段 128.0.0.0 到191.255. ...
- 从0开始学习 GitHub 系列汇总笔记
本文学习自Stromzhang, 原文地址请移步:从0开始学习 GitHub 系列汇总 我的笔记: 0x00 从0开始学习GitHub 系列之[初识GitHub] GitHub 影响力 a.全球顶级 ...
- python爬虫获取百度图片(没有精华,只为娱乐)
python3.7,爬虫技术,获取百度图片资源,msg为查询内容,cnt为查询的页数,大家快点来爬起来.注:现在只能爬取到百度的小图片,以后有大图片的方法,我会陆续发贴. #!/usr/bin/env ...
- Oracle SQL性能优化 - 根据大表关联更新小表
需求: 小表数据量20w条左右,大表数据量在4kw条左右,需要根据大表筛选出150w条左右的数据并关联更新小表中5k左右的数据. 性能问题: 对筛选条件中涉及的字段加index后,如下常规的updat ...