[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 ...
随机推荐
- Python之列表、元组、字典、集合及字符串的详细使用
1.列表 列表相当与C++中的数组,是有序的项目, 通过索引进行查找,但使用起来却方便很多,具体的操作看代码,自己实践一次就非常简单了. 注:列表一般用中括号“[ ]” #列表(数组) name_li ...
- FreeSWITCH Explained / Configuration / Proxy Media
FreeSWITCH has 3 media handling modes: Default: media flows through FS, full processing options - RT ...
- 一、JQJson数组
叙述:常用的数据格式无非三种(组装数据,传参传值) 一.数组 : 1.定义 var select = []; //或 var select = new Array(); 2.JS给一个数组赋值 sel ...
- uwsgi配置cheaper模式进行自动弹性
[uwsgi] socket = 0.0.0.0:8080 protocol = http master = true hara-kiri = 60 chdir = /home/test/projec ...
- eclipse没有Web项目和Server选项
(1)在Eclipse中菜单help选项中选择install new software选项 (2)在work with 栏中输入 http://download.eclipse.org/release ...
- Linux 普通用户自动修改密码
在大量服务器运维中,维护服务器账号就让人头痛,对账号密码策略要求,现写了一个shell脚本来完成账号密码的修改,当然这个不是最好的方法,只是在没有其它辅助服务时使用,最好还是使用账户统一管理服务来维护 ...
- 占卜DIY
题目地址 Code #include<iostream> #include<vector> #include<map> using namespace std; s ...
- MAN PVCREATE
PVCREATE(8) PVCREATE(8) NAME/名称 pvcreat ...
- 【leetcode】778. Swim in Rising Water
题目如下: 解题思路:本题题干中提到了一个非常重要的前提:"You can swim infinite distance in zero time",同时也给了一个干扰条件,那就是 ...
- 【ElicitSearch】启动流程
一.集群启动流程 1.选举主节点 许多节点启动,集群干的第一件事儿就是选主,之后的的流程由主节点触发. 先确定唯一的.大家公认的主节点:再想办法把最新的及其原数据复制到选举的主节点上. 选主是对Bul ...