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 ...
随机推荐
- C# 交集 差集 并集
Intersect 交集,Except 差集,Union 并集 , , , , }; , , , , , }; var jiaoJi = oldArray.Intersect(newArray).To ...
- 【记录】Redis 基础
Redis可以存放五种类型 1:String(字符串) 2:List(列表) 3:Hash(字典) 4:Set(集合) 5:ZSet(有序集合) String (字符串) redis 127.0.0. ...
- 五、bootstrap-Table Treegrid
一.bootstrap-Table Treegrid <!DOCTYPE HTML> <html lang="zh-cn"> <head> &l ...
- Codeforces 356D 倍增优化背包
题目链接:http://codeforces.com/contest/356/problem/D 思路(官方题解):http://codeforces.com/blog/entry/9210 此题需要 ...
- Python中的try-finally
>>> try: ... raise KeyboardInterrupt ... finally: ... print('Goodbye, world!') ... Goodbye, ...
- leetcode-165周赛-1276-不浪费原料的汉堡制作方案
题目描述: 自己的提交: class Solution: def numOfBurgers(self, tomatoSlices: int, cheeseSlices: int) -> List ...
- Linux文件归档工具——tar
Linux打包压缩命令——tar 一tar工具的介绍 Tar(Tape ARchive,磁带归档的缩写) NAME tar - manual page for tar 1.26 SYNOPSIS ta ...
- Python--前端之HTML
html概述和基本结构 HTML概述 HTML是 HyperText Mark-up Language 的首字母简写,意思是超文本标记语言,超文本指的是超链接,标记指的是标签,是一种用来制作网页的语言 ...
- Druid动态数据源配置
上文已经讲了单个数据源的Druid的配置(http://www.cnblogs.com/nbfujx/p/7686634.html) Druid动态数据源配置 主要是继承AbstractRouting ...
- 用 Flask 来写个轻博客 (8) — (M)VC_Alembic 管理数据库结构的升级和降级
Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 扩展阅读 Alembic 查看指令 manager db 的可用选项 ...