[CF453C] Little Poney and Summer Sun Celebration (思维)
[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 (思维)的更多相关文章
- CF453C Little Pony and Summer Sun Celebration(构造、贪心(?))
CF453C Little Pony and Summer Sun Celebration 题解 这道题要求输出任意解,并且路径长度不超过4n就行,所以给了我们乱搞构造的机会. 我这里给出一种构造思路 ...
- CF453C Little Pony and Summer Sun Celebration (DFS)
http://codeforces.com/contest/456 CF454E Codeforces Round #259 (Div. 1) C Codeforces Round #259 (Di ...
- CF453C Little Pony and Summer Sun Celebration
如果一个点需要经过奇数次我们就称其为奇点,偶数次称其为偶点. 考虑不合法的情况,有任意两个奇点不连通(自己想想为什么). 那么需要处理的部分就是包含奇点的唯一一个连通块.先随意撸出一棵生成树,然后正常 ...
- codeforces 453C Little Pony and Summer Sun Celebration
codeforces 453C Little Pony and Summer Sun Celebration 这道题很有意思,虽然网上题解很多了,但是我还是想存档一下我的理解. 题意可以这样转换:初始 ...
- CF 453C. Little Pony and Summer Sun Celebration
CF 453C. Little Pony and Summer Sun Celebration 构造题. 题目大意,给定一个无向图,每个点必须被指定的奇数或者偶数次,求一条满足条件的路径(长度不超\( ...
- codeforces 454 E. Little Pony and Summer Sun Celebration(构造+思维)
题目链接:http://codeforces.com/contest/454/problem/E 题意:给出n个点和m条边,要求每一个点要走指定的奇数次或者是偶数次. 构造出一种走法. 题解:可能一开 ...
- CF453C-Little Pony and Summer Sun Celebration【构造】
正题 题目链接:https://www.luogu.com.cn/problem/CF453C 题目大意 \(n\)个点\(m\)条边的一张无向图,每个节点有一个\(w_i\)表示该点需要经过奇数/偶 ...
- Codeforces 454E. Little Pony and Summer Sun Celebration
题意:给n个点m条边的无向图,并给出每个点的访问次数奇偶,求构造一条满足条件的路径(点和边都可以走). 解法:这道题还蛮有意思的.首先我们可以发现在一棵树上每个儿子的访问次数的奇偶是可以被它的父亲控制 ...
- codeforces Round #259(div2) E解决报告
E. Little Pony and Summer Sun Celebration time limit per test 1 second memory limit per test 256 meg ...
随机推荐
- Sublime Text插件安装方法和常用插件
插件安装方法: 1.打开Sublime Text,按下Ctrl+Shift+P调出命令面板 ; 2.输入install 调出 Install Package Control选项并回车; 3.再次按下C ...
- python 中PIL.Image和OpenCV图像格式相互转换
PIL.Image转换成OpenCV格式: import cv2 from PIL import Image import numpy image = Image.open("plane ...
- 基于Redis做内存管理
1 Redis存储机制: redis存储的数据类型包括,String,Hash,List,Set,Sorted Set,它内部使用一个redisObject对象来表示所有的key和value,这个对象 ...
- ShellExecute指定IE浏览器打开
ShellExecute(NULL,L"open", L"iexplore.exe", L"www.baidu.com", NULL, SW ...
- Python随笔——Map之键对应多值的处理
在使用 Python 处理时,因为某些原因,可能遇到 Map 的键对应多个值的处理. 很常见的比如:查询某表的结果,对应了多条记录. 此时使用Python进行算法处理时,其中一种方式如下: 定义一个 ...
- UVa 10054 : The Necklace 【欧拉回路】
题目链接 题目大意:我的妹妹有一串由各种颜色组成的项链. 项链中两个连续珠子的接头处共享同一个颜色. 如上图, 第一个珠子是green+red, 那么接这个珠子的必须以red开头,如图的red+whi ...
- 【leetcode】1046. Last Stone Weight
题目如下: We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose t ...
- 【leetcode】1033. Moving Stones Until Consecutive
题目如下: Three stones are on a number line at positions a, b, and c. Each turn, you pick up a stone at ...
- ht-2 arrayList特性
一.arrayList对象创建 当调用无参构造方法来构造一个ArrayList对象时,它会在内部分配一个初始大小为10的一个Object类型数组, 当添加的数据容量超过数组大小的时候,会产生一个新的数 ...
- ruby中=>是什么意思
如果是对数组赋值,下标 => 值例如 a = {1 => "1",2 => "22"}a[1] "1"a[2] " ...