CF453C Little Pony and Summer Sun Celebration

题解

这道题要求输出任意解,并且路径长度不超过4n就行,所以给了我们乱搞构造的机会。

我这里给出一种构造思路:

首先一个连通块如果没有要求奇数次的点,那么就可以不管他,如果超过一个连通块内有要求奇数次的点,那么无解。

然后在那个唯一需要走的连通块里,我们随便抠一个生成树出来,从根遍历


首先把每次向下走以及回溯的路径记录到序列的新一位(并不需要一开始就把根节点加入,反正最后会回溯到根,于是最后一个元素添加为根),顺便记录奇偶性的变化,

这个过程增加的序列长度 <= 2n

然后在①的过程中,对于任意非根的点 i,当他要回溯到 fa[i] 时,如果他的次数奇偶性与我们想要的不同,那么在序列中加入一段“fa[i],i”,这样就把 i 和 i 的子树都能调对,回溯后再考虑fa[i],

这个过程增加的序列长度 <= 2n

在②的最后,回溯到根时,如果他的次数奇偶性不正确,就把序列最后一个元素(恰好就为根了吧)删掉,如果对于只有一个节点的情况,就在序列头加一个。

设个过程增加的序列长度在 -1 到 1 之间


可以证明,最终的序列长度不会超过4n。

C O D E

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<algorithm>
#define MAXN 100005
#define ENDL putchar('\n')
#define LL long long
#define lowbit(x) ((-x)&(x))
using namespace std;
inline LL read() {
LL f = 1,x = 0;char s = getchar();
while(s < '0' || s > '9') {if(s == '-')f = -1;s = getchar();}
while(s >= '0' && s <= '9') {x = x * 10 + (s - '0');s = getchar();}
return x * f;
}
int n,m,i,j,s,o,k,root;
vector<int> g[MAXN];
int fa[MAXN];
int findf(int x) {return fa[x] == x ? x:(fa[x] = findf(fa[x]));}
void unionSet(int x,int y) {fa[findf(x)] = findf(y);}
int c[MAXN],cnt;
bool f[MAXN];
int pr[MAXN*4],cnp;
int dfs(int x,int fa) {
int res = c[x];
f[x] = 1;
for(int i = 0; i < g[x].size();i ++) {
if(g[x][i] != fa) {
res = max(res,dfs(g[x][i],x));
}
}
return res;
}
void dfs2(int x,int fa) {
for(int i = 0; i < g[x].size();i ++) {
if(g[x][i] != fa) {
pr[++ cnp] = g[x][i];
c[g[x][i]] ^= 1;
dfs2(g[x][i],x);
pr[++ cnp] = x;
c[x] ^= 1;
}
}
if(fa) {
if(c[x]) {
pr[++ cnp] = fa;
pr[++ cnp] = x;
c[fa] ^= 1;
c[x] ^= 1;
}
}
else if(c[x]) {
if(cnp > 0) cnp --;
else pr[++ cnp] = x,c[x] ^= 1;
}
return ;
}
int main() {
n = read();m = read();
for(int i = 1;i <= n;i ++) fa[i] = i;
for(int i = 1;i <= m;i ++) {
s = read();o = read();
if(findf(s) ^ findf(o))
g[s].push_back(o),g[o].push_back(s),unionSet(s,o);
}
for(int i = 1;i <= n;i ++) c[i] = read();
for(int i = 1;i <= n;i ++) {
if(!f[i]) {
int dt;
cnt += (dt = dfs(i,0));
if(dt) root = i;
}
}
if(cnt > 1) {
printf("-1\n");
return 0;
}
// printf("cnt & root:%d %d\n",cnt,root);
if(root) dfs2(root,0);
printf("%d\n",cnp);
for(int i = 1;i <= cnp;i ++) {
printf("%d ",pr[i]);
}ENDL;
return 0;
}

CF453C Little Pony and Summer Sun Celebration(构造、贪心(?))的更多相关文章

  1. CF453C Little Pony and Summer Sun Celebration (DFS)

    http://codeforces.com/contest/456  CF454E Codeforces Round #259 (Div. 1) C Codeforces Round #259 (Di ...

  2. CF453C Little Pony and Summer Sun Celebration

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

  3. CF 453C. Little Pony and Summer Sun Celebration

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

  4. codeforces 453C Little Pony and Summer Sun Celebration

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

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

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

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

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

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

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

  8. Codeforces 454E. Little Pony and Summer Sun Celebration

    题意:给n个点m条边的无向图,并给出每个点的访问次数奇偶,求构造一条满足条件的路径(点和边都可以走). 解法:这道题还蛮有意思的.首先我们可以发现在一棵树上每个儿子的访问次数的奇偶是可以被它的父亲控制 ...

  9. BZOJ 1124: [POI2008]枪战Maf(构造 + 贪心)

    题意 有 \(n\) 个人,每个人手里有一把手枪.一开始所有人都选定一个人瞄准(有可能瞄准自己).然后他们按某个顺序开枪,且任意时刻只有一个人开枪. 因此,对于不同的开枪顺序,最后死的人也不同. 问最 ...

随机推荐

  1. AtCoder ABC 242 题解

    AtCoder ABC 242 题解 A T-shirt 排名前 \(A\) 可得 T-shirt 排名 \([A+1,B]\) 中随机选 \(C\) 个得 T-shirt 给出排名 \(X\) ,求 ...

  2. 五分钟搞懂POM设计模式

    转载请注明出处️ 作者:IT小学生蔡坨坨 原文链接:五分钟搞懂POM设计模式 大家好,我是IT小学生蔡坨坨. 今天,我们来聊聊Web UI自动化测试中的POM设计模式. 为什么要用POM设计模式 前期 ...

  3. vue海康视频播放组件

    海康视频插件web文档 渲染组件后,调用initPlugin函数,传入一个code数组 <template> <div :title="name" :id=&qu ...

  4. Java使用类-String

    String,StringBuffer,StringBuild 大佬的理解-><深入理解Java中的String> 1.String 1.1 String 实例化 String st ...

  5. VueX的热更替你知道多少?

    前言 我们在使用Vuex的时候,会时不时的更改Vuex内的数据,但是页面不会随之更新,如果数据量大,一个数据依赖另一个数据的话,这样我们要是再刷新页面的话会把以前依赖的数据清空,效率特别低.所以,今天 ...

  6. 记一次 .NET 某物管后台服务 卡死分析

    一:背景 1. 讲故事 这几个月经常被朋友问,为什么不更新这个系列了,哈哈,确实停了好久,主要还是打基础去了,分析 dump 的能力不在于会灵活使用 windbg,而是对底层知识有一个深厚的理解,比如 ...

  7. 配置svn,httpd启动报错 Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details.

    查看httpd的状态,发现80端口被占用,因为我的nginx的80端口. systemctl status httpd.service  解决: 把Apache的端口该成别的端口 vi /etc/ht ...

  8. List集合五种遍历方式

    一.使用Iterator接口遍历 二.普通for循环遍历 三.增强for循环遍历 四.List集合自带迭代器 五.Lambda(JDK8新增特性) //使用多态方式创建对象 List<Strin ...

  9. windows10 安装MySQL ZIP版本

    今天重新学习了一下MySQL,但是呢刷题网站不知道为什么很卡,输入容易卡死崩溃,于是乎想在win10上面进行安装进行练习. 发现电脑里面没有,于是进行安装. 1.首先下载MySQL https://d ...

  10. Tapdata 实时数据融合平台解决方案(三):数据中台的技术需求

    作者介绍:TJ,唐建法,Tapdata 钛铂数据 CTO,MongoDB中文社区主席,原MongoDB大中华区  首席架构师,极客时间MongoDB视频课程讲师. 我们讲完了这个中台的一个架构和它的逻 ...