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");
		}
	}

}

  

原文地址:https://www.cnblogs.com/pkgunboat/p/10884218.html

时间: 2024-11-25 17:31:44

Codeforces 1159E 拓扑排序的相关文章

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/29/C Description One day Bob got a letter in an envelope. Bob knows that when Berland's post officers send a letter directly from city «A» to city «B

Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序

https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[],使得两个数组中最大的数尽量小 题解 按照偏序表,构造出从小到大的拓扑图 如何解决相等的数的偏序关系? 用并查集缩点后再进行拓扑排序 如何解决最大的数最小? 只需要使得同一层的数相同就行,可以一批处理栈中的元素,对于一批栈中的元素产生的新点,先放进一个容器里,然后等到这批栈清空了,再把这个容器中的点

Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)

Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given a directed graph consisting of n vertices and m edges (each edge is directed, so it can

Codeforces Round #290 (Div. 2) C. Fox And Names 拓扑排序

C. Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: t

Codeforces Round #363 Fix a Tree(树 拓扑排序)

先做拓扑排序,再bfs处理 #include<cstdio>#include<iostream>#include<cstdlib>#include<cstring>#include<string>#include<algorithm>#include<map>#include<stack>#include<queue>#include<vector>#include<cmath&g

Codeforces Round #292 (Div. 2) D. Drazil and Tiles [拓扑排序 dfs]

传送门 D. Drazil and Tiles time limit per test 2 seconds memory limit per test 256 megabytes Drazil created a following problem about putting 1 × 2 tiles into an n × m grid: "There is a grid with some cells that are empty and some cells that are occupie

Codeforces Round #292 (Div. 2) -- D. Drazil and Tiles (拓扑排序)

D. Drazil and Tiles time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Drazil created a following problem about putting 1?×?2 tiles into an n?×?m grid: "There is a grid with some cells that a

codeforces 510C Fox And Names 拓扑排序

传送门:cf 510D 给定n个字符串,问能否存在这样的字母表,使得字符串的排序满足字典序.即依据新的字母表,排序满足字典序大小. 假设满足字典序,则我们可以依据已有的字符串得出各字母之间的大小关系,然后通过拓扑排序来判断是否存在可行解,输出任意解,因此只需要判断是否存在解即可. /****************************************************** * File Name: a.cpp * Author: kojimai * Create Time: 2

Codeforces Round #290 (Div. 2) 拓扑排序

C. Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: t