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 推出最后 ...
随机推荐
- 「CF442C」 Artem and Array
题目链接 戳我 \(Solution\) 观察发现如果一个数两边都比他大,删掉他可以保证最优,这个应该是显然的.这个东西用单调栈维护一下,最后剩下的就是个单调递减或单调递增的数列,从小到大排个序取前面 ...
- Java 读取模板并生成HTML静态文件实例
原理都很简单,主要是对模板的解析.so,我们先准备一个html模板mb.html,做个文件其中的###title###之类的标签用于程序进行查询替换. HTML code复制代码 <html&g ...
- spiderkeeper使用教程
安装包 pip install scrapy pip install scrapyd pip install scrapyd-client pip install spiderkeeper 进入到sc ...
- Java多线程深入理解
在java中要想实现多线程,有两种手段,一种是继续Thread类,另外一种是实现Runable接口. 对于直接继承Thread的类来说,代码大致框架是: ? 1 2 3 4 5 6 7 8 9 10 ...
- onReachBottom 注意事项
onReachBottom使用注意 可在pages.json里定义具体页面底部的触发距离onReachBottomDistance,比如设为50,那么滚动页面到距离底部50px时,就会触发onReac ...
- 邻居子系统输出 之 neigh_output、neigh_hh_output
概述 ip层在构造好ip头,检查完分片之后,会调用邻居子系统的输出函数neigh_output进行输出,输出分为有二层头缓存和没有两种情况,有缓存时调用neigh_hh_output进行快速输出,没有 ...
- Linux 命令速记本
# 比较1.txt和2.txt的差异 comm [---] .txt .txt # 求1.txt和2.txt的MD5用于区分两个文件是否相同 md5sum .txt .txt #tr 用于转换或删除文 ...
- ASP.NET Core-Docs:在 ASP.NET Core 中启用跨域请求(CORS)
ylbtech-ASP.NET Core-Docs:在 ASP.NET Core 中启用跨域请求(CORS) 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 1. ...
- 淘宝TAE平台定时任务包的部署步骤
淘宝TAE平台定时任务包的部署: 第一步:首先把自己的任务打包成一个jar包.使用maven打包的命令为:mvn clean install 第二步:把任务jar包依赖的jar包全部导出来.使用mav ...
- C++学习 之 初识命名空间
声明: 本人自学C++, 没有计算机基础,在学习的过程难免会出现理解错误,出现风马牛不相及的现象,甚至有可能会贻笑大方. 如果有幸C++大牛能够扫到本人的博客,诚心希望大牛能给予 ...