Codeforces 1152E Neko and Flashback 欧拉路径

Neko and Flashback

把a[ i ] - b[ i ] 看成边, 就是求一遍欧拉路径就好了。

注意图不连通的情况。。

#include<bits/stdc++.h>
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
using namespace std;

const int N = (int)2e5 + 7;

int n, a[N], b[N], deg[N];
int hs[N], hs_cnt;
bool ban[N];
vector<PII> G[N];
vector<int> ans;

void dfs(int u) {
    while(G[u].size()) {
        int v = G[u].back().se;
        int id = G[u].back().fi;
        G[u].pop_back();
        if(!ban[id]) {
            ban[id] = true;
            dfs(v);
        }
    }
    ans.push_back(hs[u]);
}

int main() {
    scanf("%d", &n);
    for(int i = 1; i < n; i++) {
        scanf("%d", &a[i]);
        hs[++hs_cnt] = a[i];
    }
    for(int i = 1; i < n; i++) {
        scanf("%d", &b[i]);
        hs[++hs_cnt] = b[i];
    }
    sort(hs + 1, hs + 1 + hs_cnt);
    hs_cnt = unique(hs + 1, hs + 1 + hs_cnt) - hs - 1;
    for(int i = 1; i < n; i++) {
        a[i] = lower_bound(hs + 1, hs + 1 + hs_cnt, a[i]) - hs;
        b[i] = lower_bound(hs + 1, hs + 1 + hs_cnt, b[i]) - hs;
        deg[a[i]]++;
        deg[b[i]]++;
        G[a[i]].push_back(mk(i, b[i]));
        G[b[i]].push_back(mk(i, a[i]));
    }
    for(int i = 1; i < n; i++) {
        if(a[i] > b[i]) {
            puts("-1");
            return 0;
        }
    }
    int be = 1, cnt = 0;
    for(int i = 1; i <= hs_cnt; i++) {
        if(deg[i] & 1) {
            be = i;
            cnt++;
        }
    }
    if(cnt != 0 && cnt != 2) puts("-1");
    else {
        dfs(be);
        if(ans.size() != n) return puts("-1"), 0;
        for(int i = (int)ans.size() - 1; i >= 0; i--) {
            printf("%d%c", ans[i], " \n"[i == 0]);
        }
    }
    return 0;
}

/*
*/

原文地址:https://www.cnblogs.com/CJLHY/p/11615194.html

时间: 2024-08-30 17:36:39

Codeforces 1152E Neko and Flashback 欧拉路径的相关文章

CodeForces 1292A NEKO&#39;s Maze Game(思维)

1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <string> 5 #include <math.h> 6 #include <algorithm> 7 #include <vector> 8 #include <stack> 9 #include <queue> 10 #include <

Codeforces 1152E(欧拉路径)

看样例然后发现只要求一个一笔画即可,用板子. #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> using namespace std; const int maxn = 1e5 + 5; int n, b[maxn], c[maxn], _b[maxn], _c[maxn]; int d[maxn <

codeforces 547D Mike and Fish 欧拉路径

题目链接:点击打开链接 题意: 给定二维平面上的n个点的坐标 问: 把每个点用红色或蓝色染色, 使得 水平共线(或者垂直共线)的 点 中红色与蓝色数量差不超过1. 思路: 我们建一个二部图,X集是x轴,Y集是y轴 那么点(1,5)就是 x集的 1向 y集的 5连一条边. 此时点就是用边来表示的,那我们的任务就是给边染色. 使得: 对于二部图中任意一个点, 点所连接的红边和蓝边数量差不超过1. 那么我们可以认为这个点的入边就是红色,出边就是蓝色.显然这就是一个欧拉路径. 所以爆搜欧拉路径即可. #

CodeForces 1152F1 Neko Rules the Catniverse (Small Version)

题目链接:http://codeforces.com/problemset/problem/1152/F1 题目大意 有 n 个星球,给定限制 m,从 x 星球走到 y 星球的条件是,$1 \leq y \leq x + m$,且 y 不能被访问过. 求游玩其中 k 个星球有多少种不同的方案? 分析 一开始我的想法是二维 dp,dp[i][j] 表示前 i 个星球,访问其中 j 个,一共的方案种数,然后在遍历到第 i + 1 个星球的时候,很明显有访问和不访问两种操作,不访问好算,直接赋值即可,

CodeForces 1152F2 Neko Rules the Catniverse (Large Version)

题目链接:http://codeforces.com/problemset/problem/1152/F2 题目大意 见http://codeforces.com/problemset/problem/1152/F1,此题 n 最大能到 109. 分析 在 F1 的基础上, 代码如下 时间复杂度:$O(\log n*(k*2^m)^3)$ 原文地址:https://www.cnblogs.com/zaq19970105/p/10886953.html

Codeforces . C.Neko does Maths

题目描述: C. Neko does Maths time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. N

CodeForces 789D 欧拉路径计数,思维

CodeForces 789D 题意:n个点m条边的无向图,求经过其中m-2条边两次,剩下2条边一次的方案数有几种,如果剩下两条边的集合一样算同一种. tags: 选出两条边,其它m-2条边假想复制成两条,这样就是要求欧拉路径是否存在,即奇点个数是否为0或2. 所以该怎么选这两条边呢? 先把边分为自环边和普通边. 1.选取两条不相邻普通边,图中存在4个奇点,不满足欧拉路径条件: 2.选取两条相邻普通边,图中存在2个奇点,满足欧拉路径条件: 3.选取一条普通边一条自环,图中存在2个奇点,满足欧拉路

Neko and Aki&#39;s Prank CodeForces - 1152D (括号序列,dp)

大意: 将所有长度为2*n的合法括号序列建成一颗trie树, 求trie树上选出一个最大不相交的边集, 输出边集大小. 最大边集数一定不超过奇数层结点数. 这个上界可以通过从底层贪心达到, 所以就转化为求奇数层结点数. 然后就dp求出前$i$为'('比')'多j个的方案数, 奇数层且合法的时候统计一下贡献即可. #include <iostream> #include <iostream> #include <algorithm> #include <cstdio

Codeforces Round #554 (Div. 2) 1152C. Neko does Maths

学了这么久,来打一次CF看看自己学的怎么样吧 too young too simple 1152C. Neko does Maths 题目链接:"https://codeforces.com/contest/1152/problem/C" 题目大意:给你两个数a,b,现在要你找出一个数k使得(a+k)和(b+k)的最小公倍数最小. 题目思路:暴力(逃) 这题没得思路,想了想既然求LCM了那么和GCD说不定有点关系 然后就没有然后了 比赛的时候交了一发暴力上去,然并软 赛后补题,题解里面