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 ...
随机推荐
- unity transform 常用操作
1.寻找物体 1.1 寻找满足条件的子物体 ` public static Transform FindObj(Transform transform, Func<Transform, bool ...
- SAP MM 采购ERP顾问咨询费限制总金额的框架协议实现方案
SAP MM 采购ERP顾问咨询费限制总金额的框架协议实现方案 [业务场景] 采购部门与ERP咨询公司签订了一个框架协议,只规定不同级别顾问的人天费用,不限定这些不同级别咨询顾问的具体采购的人天数,但 ...
- 自定义修改Anaconda Jupyterlab Home目录
自定义修改Anaconda Jupyterlab Home目录 最近在使用Anaconda学习数据分析和机器学习,会使用到Jupyter,但是他默认目录是用户的目录,我并没有习惯将项目和资料放在C盘, ...
- android 权限库EasyPermissions
文章链接:https://mp.weixin.qq.com/s/H63Sn03xV0JoINXB4SWWKA 众所周知,在android 6.0之后,如果应用程序需要危险权限,则用户必须明确向应用授予 ...
- typescript中的泛型
泛型:软件工程中,我们不仅要创建一致的定义良好的API,同时也要考虑可重用性. 组件不仅能够支持当前的数据类型,同时也能支持未来的数据类型,这在创建大型系统时为你提供了十分灵活的功能. 在像C#和Ja ...
- 【Linux】【MySQL】CentOS7、MySQL8.0.13 骚操作速查笔记——专治各种忘词水土不服
1.前言 [Linux][MySQL]CentOS7安装最新版MySQL8.0.13(最新版MySQL从安装到运行) 专治各种忘词,各种水土不服. - -,就是一个健忘贵的速查表:(当然不包括SQL的 ...
- 逻辑回归&线性支持向量机
代码: # -*- coding: utf-8 -*- """ Created on Tue Jul 17 10:13:20 2018 @author: zhen &qu ...
- C学习笔记(自增)
自增 (1)后缀:与Turbo C相同,在语句结束之前或者说分号之前才会执行自增. (2)前缀: 前两个自增统一取值,后面的自增即为取值. int i=2,j; j=++i+(++i)+(++i); ...
- RubyGems系列之RubyGems初识
转载请标明来源:https://www.cnblogs.com/zhanggui/p/9719291.html 一. 基础理解 RubyGems简称gems,它是一个用于对Ruby组件进行打包的Rub ...
- mongoDB概述
mongoDB介绍 是一个开源的文档数据库,基于分布式文件存储的数据库.由 C++ 语言编写,并领先的 NoSQL 数据库(非关系型数据库)MongoDB 是由 c++语言编写. MongoDB 是一 ...