优化连边然后拓排.

#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 (拓排)的更多相关文章

  1. codeforces 721C (拓排 + DP)

    题目链接:http://codeforces.com/contest/721/problem/C 题意:从1走到n,问在时间T内最多经过多少个点,按路径顺序输出. 思路:比赛的时候只想到拓排然后就不知 ...

  2. HDU5438--Ponds (拓排+BFS)

    点击打开链接 题意:给定n个池塘,m对池塘相连,现在要将与少于2个池塘相连的池塘拆除,形成森林,求节点数为奇数的树权值之和 思路:按照拓排的思想不断删除度数小于2的节点 #include<bit ...

  3. HDU-2647 Reward ,逆拓排。

    Reward 发工资,以前看过这题,做没做忘了(应该是没做). 很明显的拓排.但数据范围这么大,吓得我当时就不敢动手.后来找题解发现还是相当于两层循环(are you kidding me?)当时卡在 ...

  4. [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 ...

  5. Andrew and Taxi CodeForces - 1100E (思维,拓扑)

    大意: 给定有向图, 每条边有一个权值, 假设你有$x$个控制器, 那么可以将所有权值不超过$x$的边翻转, 求最少的控制器数, 使得翻转后图无环 先二分转为判定问题. 每次check删除能动的边, ...

  6. uva12489 Combating cancer(树同构)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud https://uva.onlinejudge.org/index.php?opt ...

  7. cdoj 排名表 拓扑排序 排名输出 贪心

    //并不理解为什么需要反向建图,由大到小倒序确定排名.感觉正向由小到大和反向由大到小应该是一样的. 解:拓排+贪心,反向建边,先找排名靠后的(now,不知道为什么) #include<cstdi ...

  8. 2208: [Jsoi2010]连通数

    2208: [Jsoi2010]连通数 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1371  Solved: 557[Submit][Status ...

  9. 传递闭包(例题POJ3660)

    概念: 传递一种关系,例如 a//b   b//c  则 a//c 从已知的初始关系中  推出最后所有对象之间的关系 初始时把所有有关系的标记为1 即a[i][j] = 1 然后用Floyd 推出最后 ...

随机推荐

  1. ETL-拉链算法-带删除的拉链算法

    truncate table CUST;truncate table TAG_CUST; truncate table vt_inc;truncate table vt_new; insert int ...

  2. Keras学习笔记一:修改数据读入方式为本地图片读入

    第一种方法: Keras官方给的图片去噪示例要自动下载mnist数据集并处理,不能修改和加入自己的数据集. from keras.datasets import mnist (x_train, _), ...

  3. 使用conda进行本地安装

    1. 由于安装源被墙转换为本地安装 例如要安装 boost-1.59版本,因为在默认源中没有,我们指定了特定源进行安装 conda install -c menpo dlib=boost-1.59 然 ...

  4. SRS之分发HLS

    来自: Delivery HLS 1. 综述 SRS支持HLS/RTMP两种成熟而且广泛应用的流媒体分发方式. RTMP指Adobe的RTMP(Realtime Message Protocol),广 ...

  5. Mysql general_log 日志详解

    开启 general log  将所有到达MySQL Server的SQL语句记录下来. 一般不会开启开功能,因为log的量会非常庞大.但个别情况下可能会临时的开一会儿general log以供排障使 ...

  6. RN性能优化(重新探索react吧)

    最近做RN遇到了一些性能瓶颈,逼着自己不得不做一些优化 已经做过,或者尝试过得优化方案: 1.点击效果防止重复点击. 2.左右两边分别用两个异步栈进行更新,这样能让右边的缓慢不影响左边的更新. 3.I ...

  7. WPF Slider Tickbar 中显示数值

    class CustomTickBar : TickBar { protected override void OnRender(System.Windows.Media.DrawingContext ...

  8. python 连接mysql数据库:pymysql

    示例:import pymysql conn=pymysql.connect( host="127.0.0.1", #数据库IP port=3306, #数据库端口 user=&q ...

  9. Web jsp开发自学——ajax+servlet+echarts+json+gson 实现ajax传输servlert和echarts的数据,可视化结果

    感谢下面的博主,我学习的博客有: https://blog.csdn.net/ITBigGod/article/details/81023802  Jsp+Servlet+Echarts实现动态数据可 ...

  10. flannel下k8s pod及容器无法跨主机互通问题

    参照文档    https://blog.csdn.net/a610786189/article/details/80340556 https://blog.csdn.net/weixin_43092 ...