Codeforces 454E. Little Pony and Summer Sun Celebration
题意:给n个点m条边的无向图,并给出每个点的访问次数奇偶,求构造一条满足条件的路径(点和边都可以走)。
解法:这道题还蛮有意思的。首先我们可以发现在一棵树上每个儿子的访问次数的奇偶是可以被它的父亲控制的,因为可以x->fa->x这样的话x和父亲的访问次数都+1了,同样道理x的父亲的访问次数奇偶也可以被其父亲的父亲控制。由此可得,只有根节点不能被别人控制,但是因为回溯的缘故,我们可以通过控制最后回溯到根节点的这一步走不走来控制根节点的奇偶。这道题就解了。
这里有一个细节:因为图不一定连通,不能只把1作为起点,应该随便找一个奇数点作为起点。
细节详见代码:
#pragma comment(linker,"/STACK:102400000,102400000")
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+;
int n,m,rt,Size,cnt[N],jo[N];
vector<int> ans,G[N];
bool vis[N]; void dfs(int x,int fa) {
vis[x]=rt;
ans.push_back(x); cnt[x]++;
for (int i=;i<G[x].size();i++) {
int y=G[x][i];
if (vis[y]) continue;
dfs(y,x);
ans.push_back(x); cnt[x]++;
}
if (x!=rt)
if (cnt[x]%!=jo[x]) {
ans.push_back(fa); cnt[fa]++;
ans.push_back(x); cnt[x]++;
}
} int main()
{
cin>>n>>m;
for (int i=;i<=m;i++) {
int x,y; scanf("%d%d",&x,&y);
G[x].push_back(y);
G[y].push_back(x);
}
for (int i=;i<=n;i++) scanf("%d",&jo[i]); rt=; for (int i=;i<=n;i++) if (jo[i]) rt=i;
dfs(rt,); int Size=ans.size();
if (cnt[rt]%!=jo[rt]) cnt[rt]--,Size--;
for (int i=;i<=n;i++)
if (cnt[i]%!=jo[i]) { puts("-1"); return ; }
cout<<Size<<endl;
for (int i=;i<Size;i++) printf("%d ",ans[i]);
return ;
}
Codeforces 454E. Little Pony and Summer Sun Celebration的更多相关文章
- codeforces 453C Little Pony and Summer Sun Celebration
codeforces 453C Little Pony and Summer Sun Celebration 这道题很有意思,虽然网上题解很多了,但是我还是想存档一下我的理解. 题意可以这样转换:初始 ...
- CF453C Little Pony and Summer Sun Celebration (DFS)
http://codeforces.com/contest/456 CF454E Codeforces Round #259 (Div. 1) C Codeforces Round #259 (Di ...
- CF 453C. Little Pony and Summer Sun Celebration
CF 453C. Little Pony and Summer Sun Celebration 构造题. 题目大意,给定一个无向图,每个点必须被指定的奇数或者偶数次,求一条满足条件的路径(长度不超\( ...
- CF453C Little Pony and Summer Sun Celebration(构造、贪心(?))
CF453C Little Pony and Summer Sun Celebration 题解 这道题要求输出任意解,并且路径长度不超过4n就行,所以给了我们乱搞构造的机会. 我这里给出一种构造思路 ...
- 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
如果一个点需要经过奇数次我们就称其为奇点,偶数次称其为偶点. 考虑不合法的情况,有任意两个奇点不连通(自己想想为什么). 那么需要处理的部分就是包含奇点的唯一一个连通块.先随意撸出一棵生成树,然后正常 ...
- CF453C-Little Pony and Summer Sun Celebration【构造】
正题 题目链接:https://www.luogu.com.cn/problem/CF453C 题目大意 \(n\)个点\(m\)条边的一张无向图,每个节点有一个\(w_i\)表示该点需要经过奇数/偶 ...
- [CF453C] Little Poney and Summer Sun Celebration (思维)
[CF453C] Little Poney and Summer Sun Celebration (思维) 题面 给出一张N个点M条边的无向图,有些点要求经过奇数次,有些点要求经过偶数次,要求寻找一条 ...
- CodeForces 454C Little Pony and Expected Maximum
Little Pony and Expected Maximum Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I6 ...
随机推荐
- 深入学习Redis主从复制
一.主从复制概述 主从复制,是指将一台Redis服务器的数据,复制到其他的Redis服务器.前者称为主节点(master),后者称为从节点(slave):数据的复制是单向的,只能由主节点到从节点. 默 ...
- config.properties
# 数据库配置db.host=10.100.2.50db.port=3306db.database=paycoredb.username=rootdb.password=mysql@123db.ini ...
- 浏览器CSS兼容
一.<important 在IE6及FF中的使用>.box1 {width:150px !important;} .box1 {width:250px;} !important是说这个设置 ...
- Pycharm文档模板变量
点击这里查看JetBrains官方英文源文件 本篇Blog只是搬运外加大概翻译一下. File template variables A file template can contain varia ...
- 新建maven项目index.jsp文件报错处理
最近用eclipse新建了一个maven项目,结果刚新建完成index.jsp页面就报错了,先把错误信息贴出来看看 后来就找资料,结果发现两种解决办法,希望可以帮助用得上的人! 第一种:直接在pom. ...
- 部署core
https://www.cnblogs.com/jasonduan/p/9193702.html
- 重磅发布!阿里云推PostgreSQL 10 高可用版
摘要: 近日,阿里云重磅发布PostgreSQL 10 高可用本地SSD盘版,相比原 9.4 版本又新增了JSONB.BRIN索引.GROUPING SETS/CUBE/ROLLUP.UPSERT等多 ...
- Tomcat负载均衡图片显示不正常解决方法
在部署一个Tomcat玩玩的时候,发现在做nginx负载均衡时,网站显示不正常,图片会变得很大.测试了半天都没成功,最后查找资料,才发现Tomcat负载均衡时Session处理有问题,Session是 ...
- 饿了么 <el-input></el-input>输入框获取与失去焦点事件
//1.定义focus事,绑定属性 <el-input v-model="headerInput" @focus="onInputFocus" @blur ...
- if(!confirm("您确定删除吗?")){return;}
if(!confirm("您确定删除吗?")){return;}