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[] ...
随机推荐
- Centos 7 安装openjdk8
一.使用yum命令搜索支持jdk版本 yum search java|grep jdk 二.使用yum安装jdk8 yum install -y java--openjdk 三.检查是否成功 java ...
- 中州韵输入法(rime)导入搜狗词库
rime是一个非常优秀的输入法,linux平台下的反应速度远超搜狗,也没有隐私风险.2012年开始接触它,到后来抛弃了它,因为rime自带的词库真的太弱了,也懒得折腾.最近发现一个词库转换软件叫ime ...
- awk 起始位置和长度和 mf 一致
1位开始 , 925开始 截取24 awk '{OFS="";print(substr($0,925,24),substr($0,1,24),substr($0,436,1),&q ...
- 力扣——gas station (加油站) python实现
题目描述: 中文: 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] ...
- 【串线篇】REST风格的请求格式
1.什么是rest 答出这两点就够了: 1.1 统一接口 rest其实是基于HTTP的,四种方式. RESTful架构风格规定,数据的元操作,即CRUD(create, read, update和de ...
- 【leetcode】934. Shortest Bridge
题目如下: In a given 2D binary array A, there are two islands. (An island is a 4-directionally connecte ...
- Mybatis缓存+配置
mybatis提供了缓存机制减轻数据库压力,提高数据库性能 mybatis的缓存分为两级:一级缓存.二级缓存 一级缓存是SqlSession级别的缓存,缓存的数据只在SqlSession内有效 二级缓 ...
- [CSP-S模拟测试60]题解
回去要补一下命运石之门了…… A.嘟嘟噜 给定报数次数的约瑟夫,递推式为$ans=(ans+m)\% i$. 考虑优化,中间很多次$+m$后是不用取模的,这种情况就可以把加法变乘法了.问题在于如何找到 ...
- SqlServer2008跨服务器操作
--创建链接服务器 exec sp_addlinkedserver @server= 'SQL2' --链接服务器名 , @srvproduct= '' --OLE DB 数据源的产品名称:如果为 S ...
- MySQL-8.0填坑
Client does not support authentication protocol 或 Authentication plugin 'caching_sha2_password' cann ...