The Party and Sweets CodeForces - 1159C (拓排)
优化连边然后拓排.
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head #ifdef ONLINE_JUDGE
const int N = 1e6+10;
#else
const int N = 111;
#endif int n, num, ok, no, a[N], b[N], inq[N<<2];
int val[N<<2], deg[N<<2], vis[N<<2];
vector<int> g[N<<2];
queue<int> q;
void add(int x, int y) {
g[x].pb(y),++deg[y];
}
int build(int o, int l, int r) {
if (l==r) return val[o]=l;
val[o]=++num, g[num].clear(), deg[num]=vis[num]=0;
add(val[o],build(ls)),add(val[o],build(rs));
return val[o];
}
void upd(int o, int l, int r, int ql, int qr, int x) {
if (ql<=l&&r<=qr) return add(x,val[o]);
if (mid>=ql) upd(ls,ql,qr,x);
if (mid<qr) upd(rs,ql,qr,x);
}
void dfs(int x) {
if (vis[x]) return;
vis[x] = inq[x] = 1;
for (int y:g[x]) {
if (inq[y]) ok = 1;
else dfs(y);
}
if (x<=n) b[x]=++no;
inq[x] = 0;
} int main() {
int t;
scanf("%d", &t);
REP(i,1,t) {
scanf("%d", &n);
REP(i,1,n) g[i].clear();
REP(i,1,n) scanf("%d",a+i),g[i].clear(),deg[i]=vis[i]=0;
if (a[n]!=-1&&a[n]!=n+1) {puts("-1");continue;}
num=n,build(1,1,n);
REP(i,1,n-1) if (a[i]!=-1) {
if (a[i]!=i+1) upd(1,1,n,i+1,a[i]-1,i);
if (a[i]!=n+1) add(a[i],i);
}
ok = no = 0;
REP(i,1,num) if (!deg[i]) dfs(i);
if (ok) {puts("-1");continue;}
REP(i,1,n) printf("%d ", b[i]);hr;
}
}
The Party and Sweets CodeForces - 1159C (拓排)的更多相关文章
- codeforces 721C (拓排 + DP)
题目链接:http://codeforces.com/contest/721/problem/C 题意:从1走到n,问在时间T内最多经过多少个点,按路径顺序输出. 思路:比赛的时候只想到拓排然后就不知 ...
- HDU5438--Ponds (拓排+BFS)
点击打开链接 题意:给定n个池塘,m对池塘相连,现在要将与少于2个池塘相连的池塘拆除,形成森林,求节点数为奇数的树权值之和 思路:按照拓排的思想不断删除度数小于2的节点 #include<bit ...
- HDU-2647 Reward ,逆拓排。
Reward 发工资,以前看过这题,做没做忘了(应该是没做). 很明显的拓排.但数据范围这么大,吓得我当时就不敢动手.后来找题解发现还是相当于两层循环(are you kidding me?)当时卡在 ...
- [poj2762] Going from u to v or from v to u?(Kosaraju缩点+拓排)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Going from u to v or from v to u? Tim ...
- Andrew and Taxi CodeForces - 1100E (思维,拓扑)
大意: 给定有向图, 每条边有一个权值, 假设你有$x$个控制器, 那么可以将所有权值不超过$x$的边翻转, 求最少的控制器数, 使得翻转后图无环 先二分转为判定问题. 每次check删除能动的边, ...
- uva12489 Combating cancer(树同构)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud https://uva.onlinejudge.org/index.php?opt ...
- cdoj 排名表 拓扑排序 排名输出 贪心
//并不理解为什么需要反向建图,由大到小倒序确定排名.感觉正向由小到大和反向由大到小应该是一样的. 解:拓排+贪心,反向建边,先找排名靠后的(now,不知道为什么) #include<cstdi ...
- 2208: [Jsoi2010]连通数
2208: [Jsoi2010]连通数 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1371 Solved: 557[Submit][Status ...
- 传递闭包(例题POJ3660)
概念: 传递一种关系,例如 a//b b//c 则 a//c 从已知的初始关系中 推出最后所有对象之间的关系 初始时把所有有关系的标记为1 即a[i][j] = 1 然后用Floyd 推出最后 ...
随机推荐
- 实现自己的SpringMVC
一.SpringMVC的工作原理 SpringMVC流程 1. 用户发送请求至前端控制器DispatcherServlet. 2. DispatcherServlet收到请求调用HandlerMa ...
- 邻居子系统 之 邻居表的初始化neigh_table_init
概述 邻居子系统支持多种实现,例如ARP,ND等,这些实现需要在其初始化的时候,调用neigh_table_init将邻居表项添加到全局邻居子系统数组中,并对实例中的字段(如hash,定时器等)进行相 ...
- CentOS7 yum install elasticsearch
首先安装 JDK 环境 # 本机是否已经安装,ElasticSearch 最低支持 jdk 1.7 yum list installed | grep java # 查看 yum 库中的 java 安 ...
- [go]从os.Stdin探究文件类源码
咋一看go的标准输入输出函数有一箩筐. 细究了一下. - 从标准输入获取输入 fmt.Scan 以空白(空格或换行)分割,值满后无结束 fmt.Scanln 以空格作为分割,遇到换行结束 fmt.Sc ...
- Systemd 指令
Systemd 指令 原文:http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html 一.由来 历史上,Linux 的 ...
- NAT地址转换常用命令详解
缺省值:没有启用NAT. 命令模式:全局配置模式. 说明:静态NAT主要用于那些对需要对外部用户开放的服务,如Web服务器等,它可以把本地地址映射为指定的全局地址. 第一种格式实现的是一对一的NAT映 ...
- delphi 双击dbgrid 调用另一窗体的例子
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Form ...
- xaml 中 引用嵌套类的对象
public class MotorType1 { public class MotorType2 { public class MotorType3 { /// <summary> // ...
- 提交SVN Working copy locked解决
今天一大早提交SVN的时候,出现这个错误: 百度了一下原因:因为我强制在commit的时候退出了,导致svn项目文件被锁了,不能commit,不能update了 赶紧百度了一下解决办法: http:/ ...
- JavaScript高程第三版笔记-面向对象编程
之前有篇博客曾提到过一点js的面向对象编程:js面向对象编程. 这里就结合js高程详细剖析一下javascript的面向对象编程. 前序: 1⃣️Object.defineProperty() var ...