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. Precision和Recall

    学习自: http://blog.csdn.net/wangran51/article/details/7579100

  2. iOS小技巧:用runtime 解决UIButton 重复点击问题

    http://www.cocoachina.com/ios/20150911/13260.html 作者:uxyheaven 授权本站转载. 什么是这个问题 我们的按钮是点击一次响应一次, 即使频繁的 ...

  3. C++返回值优化

    返回值优化(Return Value Optimization,简称RVO)是一种编译器优化机制:当函数需要返回一个对象的时候,如果自己创建一个临时对象用于返回,那么这个临时对象会消耗一个构造函数(C ...

  4. asp 与php 的不同之处

    1.asp语句完成后不用加逗号:php要: 2.asp连接字符窜是用&:php用.; 3.asp需要用dim声明变量才能使用:php不用: 4.asp数组用(),php用[]; 5.asp的条 ...

  5. 原生JS使用Blob导出csv文件

    最近在做关于文件下载的需求:前端调用接口,然后对返回数据进行过滤.格式化,然后按表格内容拼接生成csv文件,让用户下载. 具体实现方式如下:let sourceData = { head: [ '时间 ...

  6. Linux 网络原理及基础设置

    临时配置网络(ip,网关,dns)+永久配置 设置IP和掩码 ifconfig eth0 192.168.2.2 netmask 255.255.255.0 设置网关route add default ...

  7. jmter正则表达式提取器

    1.若返回的body内容为空,仅有 header值,则: \s代表为空 2.使用Debug来调试

  8. OpenStack☞网关协议

    一 动态页面与静态页面区别 静态页面:每一个网页都有一个固定的URL,且网页的URL以.html..htm..shtml等常见的形式为后缀. 网页内容已经发布到网站服务器上,无论是否有用户访问,每个静 ...

  9. uva 11806 Cheerleaders (容斥)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  10. Open Source Software List: The Ultimate List

    http://www.datamation.com/open-source/ Accessibility 1. The Accessibility Project The Business Value ...