Codeforces 1159E 拓扑排序
题意及思路:https://www.cnblogs.com/dd-bond/p/10859864.html
代码:
#include <bits/stdc++.h>
#define LL long long
#define db double
using namespace std;
const int maxn = 500010;
stack<pair<int ,int> >s;
vector<int> G[maxn];
int a[maxn], ans[maxn], tot, n;
void topsort(void) {
queue<int> q;
q.push(n + 1);
while(!q.empty()) {
int x = q.front();
q.pop();
for (int i = 0; i < G[x].size(); i++) {
int y = G[x][i];
ans[y] = tot--;
q.push(y);
}
}
}
int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
tot = n;
while(!s.empty())s.pop();
for (int i = 1; i <= n + 1; i++) G[i].clear();
bool flag = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
if(a[i] == -1) a[i] = i + 1;
while(!s.empty() && s.top().second <= i) s.pop();
if(!s.empty() && a[i] > s.top().second) {
flag = 1;
} else {
s.push(make_pair(i, a[i]));
G[a[i]].push_back(i);
}
}
if(flag) {
printf("-1\n");
}
else {
topsort();
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
printf("\n");
}
} }
Codeforces 1159E 拓扑排序的更多相关文章
- CF思维联系--CodeForces -214C (拓扑排序+思维+贪心)
ACM思维题训练集合 Furik and Rubik love playing computer games. Furik has recently found a new game that gre ...
- CodeForces - 721C 拓扑排序+dp
题意: n个点m条边的图,起点为1,终点为n,每一条单向边输入格式为: a,b,c //从a点到b点耗时为c 题目问你最多从起点1到终点n能经过多少个不同的点,且总耗时小于等于t 题解: 这道 ...
- Codeforces 1100E 拓扑排序
题意及思路:https://blog.csdn.net/mitsuha_/article/details/86482347 如果一条边(u, v),v的拓扑序小于u, 那么(u, v)会形成环,要反向 ...
- National Property CodeForces - 875C (拓扑排序)
大意: n个字符串, 每次操作选出一种字符全修改为大写, 求判断能否使n个字符串字典序非降. 建源点s, 汇点t, s与所有必须转大写的连边, 必须不转大写的与t连边. #include <io ...
- Codeforces Round #292 (Div. 1) B. Drazil and Tiles 拓扑排序
B. Drazil and Tiles 题目连接: http://codeforces.com/contest/516/problem/B Description Drazil created a f ...
- Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 离散化拓扑排序
C. Mail Stamps Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)
D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: = 的情况我们用并查集把他们扔到一个集合,然后根据 > ...
- Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序
https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...
随机推荐
- 五、bootstrap-fileinput
一.bootstrap-fileinput <!DOCTYPE html> <html lang="en"> <head> <meta c ...
- JavaScript 下载大文件解决方案(Blob+OjbectURL)
结合Blob和OjbectURL实现更大的文件下载: var a = document.createElement('a'); var txt = '.....content....'; for(va ...
- zTree节点排序、jsTree节点排序
项目中遇到了这个问题,网上也没找到比较清晰的答案,索性提供一个方案吧. 原理:将整个树形插件的数据源进行排序,插件在构造UI时,自然也是按照顺序来排列的,目前这种思路适用于 zTree 和 jsTre ...
- js 中数组传递到后台controller 批量删除
/*批量删除*/function datadel(url) { var ids=[]; $("input[type='checkbox']:checked").each(funct ...
- POJ 2443 Set Operation (按位压缩)
Description You are given N sets, the i-th set (represent by S(i)) have C(i) element (Here "set ...
- shell正则匹配IP地址
IP分成5大类: A类地址 ⑴ 第1字节为网络地址,其它3个字节为主机地址. ⑵ 范围:1.0.0.1—126.155.255.254 ⑶ 私有地址和保留地址: ① 10.X.X.X是私有地址(只能在 ...
- SAS 读取数据文件
每次读取数据时需要告诉SAS3件事:1:数据存在哪里?2:数据的形式3:创建的数据集的类型(永久/临时) 1 读取SAS数据集 DATA temp; /*temp 为创建的数据集名称*/ INFILE ...
- idea使用 xml文件
如图 搜索template(模板) 点击加号后
- MyBatis简介与配置
1.1MyBatis简介 MyBatis 是一个可以自定义SQL.存储过程和高级映射的持久层框架.MyBatis 摒除了大部分的JDBC代码.手工设置参数和结果集重获.MyBatis 只使用简单的XM ...
- 机器学习算法--Elastic Net
1) alpha : float, optional Constant that multiplies the penalty terms. Defaults to 1.0. See the note ...