CF1152E Neko and Flashback--欧拉路径
RemoteJudge
第一次见到欧拉路径的题
注意到\(b\)和\(c\)的构造方法很特殊,即对于一个位置(经过\(p\)作用后)\(i\),若两个数分别为\(b_i\)和\(c_i\),那么在\(a\)中\(b_i\)与\(c_i\)相邻
其实\(p\)并没有什么用
从每一个\(b_i\)向\(c_i\)连边,那么问题转化为是否存在一条长度为\(n\)的欧拉路径,直接\(dfs\)就行了
几个\(-1\)的情况:
1.存在\(i\),使得\(b_i> c_i\)
2.不存在欧拉路径
3.求出来的路径长度不为\(n\)
上代码:
//
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// .' \\| |// `.
// / \\||| : |||// \
// / _||||| -:- |||||- \
// | | \\\ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// 佛祖保佑 全是BUG
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <map>
#include <set>
using namespace std;
#define ull unsigned long long
#define pii pair<int, int>
#define uint unsigned int
#define mii map<int, int>
#define lbd lower_bound
#define ubd upper_bound
#define ll long long
#define mp make_pair
#define pb push_back
#define re register
#define il inline
#define N 100000
int n, m, tot;
mii id;
multiset<int> to[2*N+5];
int ans[10*N+5], tp, d[2*N+5], cnt, p[10*N+5];
int b[N+5], c[N+5], val[2*N+5];
void dfs(int u) {
for(auto i = to[u].begin(); i != to[u].end(); i = to[u].begin()) {
auto v = *i;
to[u].erase(i), to[v].erase(to[v].lbd(u));
dfs(v);
}
ans[++tp] = u;
}
int main() {
scanf("%d", &n);
for(int i = 1; i < n; ++i) {
scanf("%d", &b[i]);
if(!id.count(b[i])) id[b[i]] = ++tot, val[tot] = b[i];
}
for(int i = 1; i < n; ++i) {
scanf("%d", &c[i]);
if(b[i] > c[i]) {
printf("-1\n");
return 0;
}
if(!id.count(c[i])) id[c[i]] = ++tot, val[tot] = c[i];
to[id[c[i]]].insert(id[b[i]]), to[id[b[i]]].insert(id[c[i]]);
d[id[c[i]]]++, d[id[b[i]]]++;
}
for(int i = 1; i <= tot; ++i) if(d[i]&1) p[++cnt] = i;
if(cnt != 0 && cnt != 2) printf("-1\n");
else {
if(cnt == 0) dfs(1);
else dfs(p[1]);
if(tp != n) printf("-1\n");
else {
while(tp) printf("%d ", val[ans[tp--]]);
printf("\n");
}
}
return 0;
}
CF1152E Neko and Flashback--欧拉路径的更多相关文章
- [欧拉路]CF1152E Neko and Flashback
1152E - Neko and Flashback 题意:对于长为n的序列c和长为n - 1的排列p,我们可以按照如下方法得到长为n - 1的序列a,b,a',b'. ai = min(ci, ci ...
- Codeforces Round #554 (Div. 2) E Neko and Flashback (欧拉路径 邻接表实现(当前弧优化..))
就是一欧拉路径 贴出邻接表欧拉路径 CODE #include <bits/stdc++.h> using namespace std; const int MAXN = 100005; ...
- E. Neko and Flashback
传送门: 题意:假定我们已知a[]={3,4,6,5,7}, 那么b[]通过min(a[i],a[i+1])得到 那么b[]={3,4,5,5}, c[]通过max(a[i],a[i+1])得到 c ...
- CodeForces 1152E Neko and Flashback
题目链接:http://codeforces.com/problemset/problem/1152/E 题目大意 有一个 1~n-1 的排列p 和长度为 n 的数组 a,数组b,c定义如下: b:b ...
- Codeforces Round #554 ( div.2 ) 总结
应该经常需要锻炼一下英语阅读理解能力和代码能力,所以以后还是需要多打打CF. 今天大概就是水一水找找感觉. A. Neko Finds Grapes $n$个箱子,$m$个钥匙 ($n,m \leq ...
- 【USACO 3.3】Riding The Fences(欧拉路径)
题意: 给你每个fence连接的两个点的编号,输出编号序列的字典序最小的路径,满足每个fence必须走且最多走一次. 题解: 本题就是输出欧拉路径. 题目保证给出的图是一定存在欧拉路径,因此找到最小的 ...
- 【转】FlashBack总结之闪回查询与闪回表
本文主要介绍利用UNDO表空间的闪回技术,主要包括:闪回表,闪回版本查询,闪回事务查询,闪回查询.这些闪回技术实现从回滚段中读取表中一定时间内操作过的数据,可用来进行数据比对,或者修正意外提交造成的错 ...
- Flashback Data Archive ( Oracle Total Recall ) introduced in 11g
Flashback Data Archive feature is part of Oracle Total Recall technology. Flashback Data Archive fea ...
- Oracle Flashback 闪回
Oracle 的闪回技术是一种数据恢复技术,仅能对用户逻辑错误进行恢复, 闪回针对的是提交commit的事务,没有提交的事务,使用rollback 1.闪回版本查询 Flashback Version ...
随机推荐
- Dynamics 365-N:N Relationship的记录处理
在Dynamics CRM中,两个Entity之间是可以有N:N Relationship的,但是CRM实现N:N,是通过系统自创建一个中间表来实现的.这个中间表,如果环境是OnPremise,可以根 ...
- android添加阴影
android底部增加背景 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns: ...
- IDEA启动tomcat乱码
1.找到IDEA安装目录 2.找到2个文件 3.编辑,在最后一行加入 -Dfile.encoding=UTF-8 4.修改IDEA里tomcat内得编码 5.修改IDEA中tomcat中,startu ...
- 关于Java中基类构造器的调用问题
在<Java编程思想>第7章复用类中有这样一段话,值得深思.当子类继承了父类时,就涉及到了基类和导出类(子类)这两个类.从外部来看,导出类就像是一个与基类具有相同接口的新类,或许还会有一些 ...
- Postgres中文分词
环境 CentOS Linux release 7.2.1511 (Core) 安装Postgres 安装postgres很简单 yum安装 sudo yum install postgresql-s ...
- c++字节对齐编译器指令#pragma
第一种 #pragma pack(push, 1) // 先把当前对齐设置压栈,再设置为1字节对齐 struct S { char a; ]; }; #pragma pack(pop) // 恢复先前 ...
- 测试面试话题8:测试人员如何让开发少写bug?
在测试过程中和不同开发合作,往往会发现一些bug都是大多数开发人员常出现的错误,为了帮助开发人员,也减少测试的重复工作量,非常有必要将以往出现的bug做整理,分析原因,让开发知道这些bug, 避免再次 ...
- 【推荐】Pencil原型设计工具
官网:http://pencil.evolus.vn/ 试用了一下,确实感觉很好用,整体体验上是一种“舒畅”的感觉,真心点赞推荐.整体功能上没有任何多余的东西,让人感觉上手就能用.虽然个人英语水平不咋 ...
- python接口自动化-传 json 参数
一.post请求有两种方法传json参数: 1.传json参数(自动转 json ) 2.传data参数(需 json 转换) 代码参考: payload = { "Jodie": ...
- jupyter notebook修改默认路径和浏览器
1.jupyter notebook --generate-config 2.修改jupyter_notebook_config.py配置文件 3.修改默认路径: c.NotebookApp.note ...