题意:给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的更多相关文章

  1. codeforces 453C Little Pony and Summer Sun Celebration

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

  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. CF 453C. Little Pony and Summer Sun Celebration

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

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

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

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

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

  6. CF453C Little Pony and Summer Sun Celebration

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

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

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

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

    [CF453C] Little Poney and Summer Sun Celebration (思维) 题面 给出一张N个点M条边的无向图,有些点要求经过奇数次,有些点要求经过偶数次,要求寻找一条 ...

  9. CodeForces 454C Little Pony and Expected Maximum

    Little Pony and Expected Maximum Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I6 ...

随机推荐

  1. DNS安装配置主从

    准备环境  关闭防火墙 挂载一下 更改配置文件 安装dns服务 更改配置文件  先复制保存一份

  2. Linux下创建动态库与使用

    参考文章:dll和so文件区别与构成:http://www.cnblogs.com/likwo/archive/2012/05/09/2492225.html 动态库路径配置- /etc/ld.so. ...

  3. 【leetcode】925.Long Pressed Name

    题目如下: Your friend is typing his name into a keyboard.  Sometimes, when typing a character c, the key ...

  4. 【leetcode】757. Set Intersection Size At Least Two

    题目如下: 解题思路:贪心算法.首先把intervals按第二个元素从小到大排序,然后遍历intervals,如果集合S和intervals[i]没有交集,那么把intervals[i]的最大值和次大 ...

  5. java.sql.SQLException: Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_0900_ai_ci,IMPLICIT) for operation '='

    查询视图时报错:java.sql.SQLException: Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_ ...

  6. Python--模块之sys模块、logging模块、序列化json模块、序列化pickle模块

    sys模块 sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit() sys.path 返回模块的搜索路径,初始化时使用PYTHONPA ...

  7. Adobe 2019 全家桶 Mac 版(最新版)

    经过不断的改革换新,Adobe2019系列已经全面来袭.在软件更新的同时,激活工具也不断的完善!小编带来的是Adobe2019注册机专门为同系列软件量身定做的注册破解工具!   ▌软件下载-关注公众号 ...

  8. Linux CentOS-7.4-x86_64(原版) 百度网盘下载

    因为CentOS-7-x86_64-DVD-1804.iso 镜像文件4.16G,超出了上传百度网盘的单个文件大小限制(4G), 所以这里先现将ISO镜像文件压缩成RAR包,然后上传网盘. 使用的话, ...

  9. Docker Machine 管理-管理machine(17)

    用 docker-machine 创建 machine 的过程很简洁,非常适合多主机环境.除此之外,Docker Machine 也提供了一些子命令方便对 machine 进行管理.其中最常用的就是无 ...

  10. Java IO Demo

    //FileReader FileWriter 读写英文    public void FileReaderAndWriter1() throws Exception { File filePath ...