CF 453C. Little Pony and Summer Sun Celebration

构造题。

题目大意,给定一个无向图,每个点必须被指定的奇数或者偶数次,求一条满足条件的路径(长度不超\(4n\)).没有输出-1

首先我们应该判断掉-1的情况

图不连通且所有的奇数点不在同一个联通块内

发现只有上述情况可以

为什么

我们发现图不连通但所有的奇数点在同一个联通块内和图连通在本质上是一种情况.

我们在这里就只考虑图连通该怎么办.

首先,我们对于这张图求出他任意一个生成树

之后我们进行dfs,在dfs再次回到\(x\)点时(即我们已经处理玩了\(x\)的所有子树),我们就要求他合法.但是,万一他不合法怎么办?

很简单,我们就利用他的父亲.即\(x->f->x\)这样的话,虽然改变了\(f\),但是我们完成了将\(x\)的子树全部合法的任务

其他的所有点都以此类推

但是,万一我们求完整棵树,发现根不合法怎么办?

不慌,我们先随便找到\(root\)的一个儿子,记为\(s\)

那么直接\(root->s-root->s\)即可(注意我们此时在\(root\),结束在\(s\))

发现\(root\)变了,但是\(s\)经过了两次,相当于没改变,由上可知,肯定合法.

至于\(4n\)的限制

我们发现每个点最多下1步,上1步,来回判不合法2步.但是有叶子的存在,答案是小于\(4n\)的

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cctype>
#include<vector>
using namespace std;
const int N = 1e5 + 3;
int tot = 1;
int n,m;
struct edhe{
int to;
int nxt;
}e[N << 1];
int head[N];
int s[N];
bool vis[N];
int now[N];
int size,num1,root;
vector <int> G[N];
vector <int> ans;
inline int read(){
int v = 0,c = 1;char ch = getchar();
while(!isdigit(ch)){
if(ch == '-') c = -1;
ch = getchar();
}
while(isdigit(ch)){
v = v * 10 + ch - 48;
ch = getchar();
}
return v * c;
}
inline void add(int x,int y){
e[++tot].to = y;
e[tot].nxt = head[x];
head[x] = tot;
}
inline void dfs(int x){
vis[x] = 1;
size++;num1 += (s[x] == 1);
for(int i = head[x];i;i = e[i].nxt){
int y = e[i].to;
if(!vis[y]) {G[x].push_back(y);dfs(y);}
}
}
inline void dfs2(int x,int f){
ans.push_back(x);
now[x] ^= 1;
for(int i = 0;i < (int)G[x].size();++i){
int y = G[x][i];
dfs2(y,x);
ans.push_back(x);
now[x] ^= 1;
}
if(now[x] != s[x] && x != root){
ans.push_back(f);
ans.push_back(x);
// ans.push_back(f);
now[x] ^= 1;
now[f] ^= 1;
}
if(now[x] != s[x] && x == root){
ans.push_back(G[x][0]);
ans.push_back(root);
ans.push_back(G[x][0]);
now[x] ^= 1;
}
return ;
}
int main(){
n = read(),m = read();
for(int i = 1;i <= m;++i){
int x = read(),y = read();
add(x,y);add(y,x);
}
for(int i = 1;i <= n;++i) scanf("%d",&s[i]);
int g = 0;
for(int i = 1;i <= n;++i) if(s[i] == 1) g++;
if(g == 0){puts("0");return 0;}
if(m == 0){
if(g != 1){puts("-1");return 0;}
else{
for(int i = 1;i <= n;++i) if(s[i] == 1) {printf("1\n%d\n",i);return 0;}
}
}
for(int i = 1;i <= n;++i){
if(!vis[i]){
size = 0,num1 = 0;
dfs(i);
if(num1) root = i;
}
}
if(size != n && num1 != g){puts("-1");return 0;}
dfs2(root,0);
printf("%d\n",ans.size());
for(int i = 0;i < (int)ans.size();++i) printf("%d ",ans[i]);
return 0;
}

CF 453C. 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. CF453C Little Pony and Summer Sun Celebration(构造、贪心(?))

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

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

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

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

    题意:给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. CF 435B Little Pony and Harmony Chest

    Little Pony and Harmony Chest 题解: 因为 1 <= ai <= 30 所以  1 <= bi <= 58, 因为 59 和 1 等效, 所以不需 ...

随机推荐

  1. pl/sql进阶一控制结构

    在任何计算机语言(c,java,c#,c++)都有各种控制语句(条件语句,循环结构,顺序控制结构…),在pl/sql中也存在这样的控制结构. 在本部分学校完毕后,希望大家达到: 1)使用各种if语句 ...

  2. More Effective C++: 06杂项讨论

    32:在未来时态下发展程序 世事永远在变,好的软件对于变化有良好的适应能力:可以容纳新的性质,可以移植到新的平台,可以适应新的需求,可以掌握新的输入.所谓在未来时态下设计程序,就是接受“事情总会改变” ...

  3. 在MaxCompute中配置Policy策略遇到结果不一致的问题

    背景信息: 本文以如下场景为基准进行编写,如下: 用户通过DataWorks-简单模式使用MaxCompute: 用户具有DataWorks默认角色,如DataWorks开发者角色: 用户通过cons ...

  4. PHPExcel 去掉错误提示 保护表格

    $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);

  5. C++中用stringstream类进行数据类型的转换

    我们在进行C++编程过程中,经常需要进行数据类型的转换. stringstream 类的作用就是进行数据类型转换.要想在程序中使用 stringstream 类,我们需要在源程序文件中包含头文件inc ...

  6. 模板—树上倍增LCA

    int LCA(int x,int y) { if(x==y)return x; if(dep[x]>dep[y])swap(x,y); while(dep[x]<dep[y]) ;;i+ ...

  7. eBPF Tracing 入门教程与实例

    原文链接 Learn eBPF Tracing: Tutorial and Examples译者 弃余 在 LPC'18(Linux Plumber's conference) 会议上,至少有24个关 ...

  8. TAE words

    love handle   pang   carbohydrate   podiatry   splay out   Cinderella   liposuction   mingle   fly t ...

  9. JavaScript 拖曳和居中问题

    今天遇到了一个问题,是这样的,有一个div盒子,实现盒子居中,居中的样式是这样的见下 #box{ width:300px; height:150px; position:absolute; left: ...

  10. windows/Linux/Mac下安装maven,maven作用

    Linux下安装maven 1.首先到Maven官网下载安装文件,目前最新版本为3.0.3,下载文件为apache-maven-3.3.9-bin.tar.gz,下载可以使用wget命令: 2.进入下 ...