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. Leetcode812.Largest Triangle Area最大三角形面积

    给定包含多个点的集合,从其中取三个点组成三角形,返回能组成的最大三角形的面积. 示例: 输入: points = [[0,0],[0,1],[1,0],[0,2],[2,0]] 输出: 2 解释: 这 ...

  2. QT语言翻译

    QT中多语言的实现方式: 1.代码中tr运用 2.使用工具生成ts文件 3.翻译ts文件 4.生成qm文件 5.程序加载 以下内容程序加载时放入即可. QString appPath = QCoreA ...

  3. hdu5131 贪心

    #include<stdio.h> #include<string.h> #include<algorithm> #include<string> #i ...

  4. Revit安装失败怎样卸载重新安装Revit,解决Revit安装失败的方法总结

    技术帖:Revit没有按照正确方式卸载,导致Revit安装失败.楼主也查过网上关于如何解决Revit安装失败的一些文章,是说删除几个Revit文件和Revit软件注册表就可以解决Revit安装失败的问 ...

  5. form表单提交,后台怎么获取select的值?后台直接获取即可,和input方式一样。

    form表单提交,后台怎么获取select的值? 后台直接获取即可,和后台获取input的值方式一样. form提交后,后台直接根据select的name获取即可,即getPara("XXX ...

  6. jQuery 加法计算 使用+号即强转类型

    var value1 = $("#txt1").val(); var value2 = $("#txt2").val(); //数值前添加+号 number加号 ...

  7. Collections.sort list内部排序

    public class ComparatorUser implements Comparator{   public int compare(Object arg0, Object arg1) {  ...

  8. C# 局部函数与事件

    本文告诉大家使用局部函数可能遇到的坑. 在以前,如果有一个事件public event EventHandler Foo和一个函数private void Program_Foo(object sen ...

  9. oracle函数 TRANSLATE(c1,c2,c3)

    [功能]将字符表达式值中,指定字符替换为新字符 [说明]多字节符(汉字.全角符等),按1个字符计算 [参数] c1   希望被替换的字符或变量 c2   查询原始的字符集 c3   替换新的字符集,将 ...

  10. kubernetes API 访问控制在阿里云容器服务(ACK)上的实践

    提起K8s API的访问控制,很多同学应该都会想到RBAC,这是K8s用来做权限控制的方法,但是K8s对API的访问控制却不止于此,今天我们就来简单介绍下K8s的访问控制以及ACK如何利用这套方法提供 ...