[CF453C] Little Poney and Summer Sun Celebration (思维)

题面

给出一张N个点M条边的无向图,有些点要求经过奇数次,有些点要求经过偶数次,要求寻找一条满足要求的路径,且该路径长度不超过点数的四倍。

N, M≤100000

分析

如果将图整个遍历一遍再回到起点,每个点都被访问了偶数(2)次。对于那些奇数点x,我们先从x走到x的父亲fa,再从fa走回x,然后再继续回溯。这样fa被多访问了2次,奇偶性不变。而x被多访问了一次,访问次数从偶数变为奇数。最后根节点特判一下即可。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define maxn 100000
using namespace std;
int n,m;
vector<int>E[maxn+5];
vector<int>ans;
bool vis[maxn+5];
int tim[maxn+5];
int cnt[maxn+5];//记录访问次数,一开始等于输入,dfs时不断^1
//如果有解的话cnt会都变成0(奇数+奇数=偶数,偶数+偶数=偶数)
void dfs(int x,int fa){
ans.push_back(x);
vis[x]=1;
cnt[x]^=1;
for(int y : E[x]){
if(y!=fa&&!vis[y]){
dfs(y,x);
ans.push_back(x);
cnt[x]^=1;
}
}
if(fa!=0&&cnt[x]==1){
ans.push_back(fa);
ans.push_back(x);
cnt[fa]^=1;
cnt[x]^=1;
}
}
int main(){
int u,v;
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d %d",&u,&v);
E[u].push_back(v);
E[v].push_back(u);
}
for(int i=1;i<=n;i++) scanf("%d",&cnt[i]);
int st=1;
for(int i=1;i<=n;i++){
if(cnt[i]==1) st=i;
}
dfs(st,0);
for(int i=1;i<=n;i++){
if(!vis[i]&&cnt[i]){
printf("-1\n");
return 0;
}
}
int sz=ans.size();
if(cnt[st]) sz--;//如果回到起点会使起点不满足,就不回起点,去掉最后一个点(sz--)
printf("%d\n",sz);
for(int i=0;i<sz;i++) printf("%d ",ans[i]);
}

[CF453C] Little Poney and Summer Sun Celebration (思维)的更多相关文章

  1. CF453C Little Pony and Summer Sun Celebration(构造、贪心(?))

    CF453C Little Pony and Summer Sun Celebration 题解 这道题要求输出任意解,并且路径长度不超过4n就行,所以给了我们乱搞构造的机会. 我这里给出一种构造思路 ...

  2. CF453C Little Pony and Summer Sun Celebration (DFS)

    http://codeforces.com/contest/456  CF454E Codeforces Round #259 (Div. 1) C Codeforces Round #259 (Di ...

  3. CF453C Little Pony and Summer Sun Celebration

    如果一个点需要经过奇数次我们就称其为奇点,偶数次称其为偶点. 考虑不合法的情况,有任意两个奇点不连通(自己想想为什么). 那么需要处理的部分就是包含奇点的唯一一个连通块.先随意撸出一棵生成树,然后正常 ...

  4. codeforces 453C Little Pony and Summer Sun Celebration

    codeforces 453C Little Pony and Summer Sun Celebration 这道题很有意思,虽然网上题解很多了,但是我还是想存档一下我的理解. 题意可以这样转换:初始 ...

  5. CF 453C. Little Pony and Summer Sun Celebration

    CF 453C. Little Pony and Summer Sun Celebration 构造题. 题目大意,给定一个无向图,每个点必须被指定的奇数或者偶数次,求一条满足条件的路径(长度不超\( ...

  6. codeforces 454 E. Little Pony and Summer Sun Celebration(构造+思维)

    题目链接:http://codeforces.com/contest/454/problem/E 题意:给出n个点和m条边,要求每一个点要走指定的奇数次或者是偶数次. 构造出一种走法. 题解:可能一开 ...

  7. CF453C-Little Pony and Summer Sun Celebration【构造】

    正题 题目链接:https://www.luogu.com.cn/problem/CF453C 题目大意 \(n\)个点\(m\)条边的一张无向图,每个节点有一个\(w_i\)表示该点需要经过奇数/偶 ...

  8. Codeforces 454E. Little Pony and Summer Sun Celebration

    题意:给n个点m条边的无向图,并给出每个点的访问次数奇偶,求构造一条满足条件的路径(点和边都可以走). 解法:这道题还蛮有意思的.首先我们可以发现在一棵树上每个儿子的访问次数的奇偶是可以被它的父亲控制 ...

  9. codeforces Round #259(div2) E解决报告

    E. Little Pony and Summer Sun Celebration time limit per test 1 second memory limit per test 256 meg ...

随机推荐

  1. SwiftUI 里的 swift 闭包总结

    创建 UI 时的闭包使用 在 SwiftUI 里闭包出现的频率特别高,这里我重新梳理了下闭包的定义. 关于闭包 闭包表达式语法的一般形式如下: {(parameters) -> return t ...

  2. ReactiveCocoa详解

    最近看了大神的博客后,感觉该对ReactiveCocoa做一个了断了. 首先大致的对以下关于ReactiveCocoa内容做一个简单的总结,其他的后续更新 1.ReactiveCocoa的操作思想 2 ...

  3. Python2 安装教程

    目录 1. 推荐阅读 2. 安装包下载 3. 安装步骤 1. 推荐阅读 Python基础入门一文通 | Python2 与Python3及VSCode下载和安装.PyCharm破解与安装.Python ...

  4. VS #include 【bits/bstdc++.h】出错

    目录 1. 本文地址 2. 按 3. 操作步骤 1. 本文地址 博客园:https://www.cnblogs.com/coco56/p/11163142.html 简书:https://www.ji ...

  5. 【改】utf-8 的去掉BOM的方法

    最近在测试中发现,linux系统中导出的文件,有记事本打开另存为或者保存后,再次导入进linux系统,发现失败了,对比文件内容,没发现区别,打开二进制文件对比发现,文件头部多了三个字符:EF BB B ...

  6. maven 提取jar包 依赖及打包排除

    <properties> <project.targetDir>D:\jar</project.targetDir> <project.targetServe ...

  7. stream benchmark 介绍

    英文原版 https://www.cs.virginia.edu/stream/ref.html FAQ中有关于STREAM_ARRAY_SIZE NTIME OFFSET STREAM_TYPE的设 ...

  8. Flutter-stack層疊樣式

    alignment調整佈局 var stack = new Stack( alignment: Alignment.center,//元素居中 //alignment: Alignment (1,1) ...

  9. RestTemplate 发送post请求

    springboot使用restTemplate post提交值 restTemplate post值 post提交有 FormData和Payload 两种形式: 第一种是formdata形式,在h ...

  10. 【leetcode】1026. Maximum Difference Between Node and Ancestor

    题目如下: Given the root of a binary tree, find the maximum value V for which there exists different nod ...