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--欧拉路径的更多相关文章

  1. [欧拉路]CF1152E Neko and Flashback

    1152E - Neko and Flashback 题意:对于长为n的序列c和长为n - 1的排列p,我们可以按照如下方法得到长为n - 1的序列a,b,a',b'. ai = min(ci, ci ...

  2. Codeforces Round #554 (Div. 2) E Neko and Flashback (欧拉路径 邻接表实现(当前弧优化..))

    就是一欧拉路径 贴出邻接表欧拉路径 CODE #include <bits/stdc++.h> using namespace std; const int MAXN = 100005; ...

  3. 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 ...

  4. CodeForces 1152E Neko and Flashback

    题目链接:http://codeforces.com/problemset/problem/1152/E 题目大意 有一个 1~n-1 的排列p 和长度为 n 的数组 a,数组b,c定义如下: b:b ...

  5. Codeforces Round #554 ( div.2 ) 总结

    应该经常需要锻炼一下英语阅读理解能力和代码能力,所以以后还是需要多打打CF. 今天大概就是水一水找找感觉. A. Neko Finds Grapes $n$个箱子,$m$个钥匙 ($n,m \leq ...

  6. 【USACO 3.3】Riding The Fences(欧拉路径)

    题意: 给你每个fence连接的两个点的编号,输出编号序列的字典序最小的路径,满足每个fence必须走且最多走一次. 题解: 本题就是输出欧拉路径. 题目保证给出的图是一定存在欧拉路径,因此找到最小的 ...

  7. 【转】FlashBack总结之闪回查询与闪回表

    本文主要介绍利用UNDO表空间的闪回技术,主要包括:闪回表,闪回版本查询,闪回事务查询,闪回查询.这些闪回技术实现从回滚段中读取表中一定时间内操作过的数据,可用来进行数据比对,或者修正意外提交造成的错 ...

  8. Flashback Data Archive ( Oracle Total Recall ) introduced in 11g

    Flashback Data Archive feature is part of Oracle Total Recall technology. Flashback Data Archive fea ...

  9. Oracle Flashback 闪回

    Oracle 的闪回技术是一种数据恢复技术,仅能对用户逻辑错误进行恢复, 闪回针对的是提交commit的事务,没有提交的事务,使用rollback 1.闪回版本查询 Flashback Version ...

随机推荐

  1. int 与 Integer 的区别

    int和Integer的区别 Integer是int的包装类,int则是java的一种基本数据类型 Integer变量必须实例化后才能使用,而int变量不需要 Integer实际是对象的引用,当new ...

  2. python的学习笔记01_5文件操作

    一,文件操作基本流程. 计算机系统分为:计算机硬件,操作系统,应用程序三部分. 我们用python或其他语言编写的应用程序若想要把数据永久保存下来,必须要保存于硬盘中,这就涉及到应用程序要操作硬件,众 ...

  3. MyDAL - .Where() & .And() & .Or() 使用

    索引: 目录索引 一.API 列表 1.Where .Where(Func<M, bool> func) 如: .Where( it => (it.Prop1>=条件1 &am ...

  4. java加载properties文件的六中基本方式实现

    java加载properties文件的方式主要分为两大类:一种是通过import java.util.Properties类中的load(InputStream in)方法加载: 另一种是通过impo ...

  5. RocketMQ三主三从二命名服务平滑版本升级实操

    ​本文介绍本次进行RocketMQ平滑过渡升级的实际操作 前文已经介绍过了升级基本原理,主要思想就是先升级NameSrv(命名服务)然后在升级broker节点.broker节点先升级master节点然 ...

  6. 从零开始制作 Hexo 主题

    原文地址:从零开始制作 Hexo 主题 · Ahonn 写在前面 本文将会从零开始开发一个简单的博客主题.样式主要参考 Hexo theme 中的 Noise 主题. 开始之前你需要了解: 模板引擎  ...

  7. WPF C# 命令的运行机制

    1.概述 1.1 WPF C# 命令的本质 命令是 WPF 中的输入机制,它提供的输入处理比设备输入具有更高的语义级别. 例如,在许多应用程序中都能找到的“复制”.“剪切”和“粘贴”操作就是命令. W ...

  8. SQL AVG 函数

    定义和用法 AVG 函数返回数值列的平均值.NULL 值不包括在计算中. SQL AVG() 语法 SELECT AVG(column_name) FROM table_name SQL AVG() ...

  9. [十二省联考2019]D2T2春节十二响

    嘟嘟嘟 这题真没想到这么简单-- 首先有60分大礼:\(O(n ^ 2logn)\)贪心.(我也不知道为啥就是对的) 然后又送15分链:维护两个堆,每次去堆顶的最大值. 这时候得到75分已经很开心了, ...

  10. 从Java小白到阿里巴巴工程师,回顾我两年来的学习经历

    添加描述