CF 453C. Little Pony and Summer Sun Celebration
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的更多相关文章
- codeforces 453C Little Pony and Summer Sun Celebration
codeforces 453C Little Pony and Summer Sun Celebration 这道题很有意思,虽然网上题解很多了,但是我还是想存档一下我的理解. 题意可以这样转换:初始 ...
- CF453C Little Pony and Summer Sun Celebration (DFS)
http://codeforces.com/contest/456 CF454E Codeforces Round #259 (Div. 1) C Codeforces Round #259 (Di ...
- CF453C Little Pony and Summer Sun Celebration(构造、贪心(?))
CF453C Little Pony and Summer Sun Celebration 题解 这道题要求输出任意解,并且路径长度不超过4n就行,所以给了我们乱搞构造的机会. 我这里给出一种构造思路 ...
- codeforces 454 E. Little Pony and Summer Sun Celebration(构造+思维)
题目链接:http://codeforces.com/contest/454/problem/E 题意:给出n个点和m条边,要求每一个点要走指定的奇数次或者是偶数次. 构造出一种走法. 题解:可能一开 ...
- Codeforces 454E. Little Pony and Summer Sun Celebration
题意:给n个点m条边的无向图,并给出每个点的访问次数奇偶,求构造一条满足条件的路径(点和边都可以走). 解法:这道题还蛮有意思的.首先我们可以发现在一棵树上每个儿子的访问次数的奇偶是可以被它的父亲控制 ...
- CF453C Little Pony and Summer Sun Celebration
如果一个点需要经过奇数次我们就称其为奇点,偶数次称其为偶点. 考虑不合法的情况,有任意两个奇点不连通(自己想想为什么). 那么需要处理的部分就是包含奇点的唯一一个连通块.先随意撸出一棵生成树,然后正常 ...
- CF453C-Little Pony and Summer Sun Celebration【构造】
正题 题目链接:https://www.luogu.com.cn/problem/CF453C 题目大意 \(n\)个点\(m\)条边的一张无向图,每个节点有一个\(w_i\)表示该点需要经过奇数/偶 ...
- [CF453C] Little Poney and Summer Sun Celebration (思维)
[CF453C] Little Poney and Summer Sun Celebration (思维) 题面 给出一张N个点M条边的无向图,有些点要求经过奇数次,有些点要求经过偶数次,要求寻找一条 ...
- CF 435B Little Pony and Harmony Chest
Little Pony and Harmony Chest 题解: 因为 1 <= ai <= 30 所以 1 <= bi <= 58, 因为 59 和 1 等效, 所以不需 ...
随机推荐
- Python中的动态继承
所谓动态继承,是指代码运行时再决定某个类的父类.某些场景下会用到,比如threading.Thread和multiprocessing.Process这两个类有很多同名的接口,可以实现某个子类动态继承 ...
- List容器案例
案例讲解 迭代模式 不暴露集合的内部结构,又让外部访问集合中的数据 package com.day1; public interface Iterator <T>{ public bool ...
- Java练习 SDUT-3081_谁是最强女汉子
谁是最强的女汉子 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 众所周知,一年一度的女汉子大赛又来啦.由于最近女汉子比 ...
- phpexcel使用说明5----ThinkPHP+PHPExcel[导入][导出]实现方法
转自:http://www.thinkphp.cn/code/403.html实现步骤: 注意:phpexcel必须是1.78版本的,不能用1.8以上的 一:去官网http://phpexcel.co ...
- jQuery 图片跟着鼠标动
html默认鼠标样式改成图片时格式为 .ani 图片跟随鼠标挪动 html <div id="mouseImg"> <img src="images/问 ...
- LeetCode91 Decode Ways
题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...
- python 列表长度
- 从 Spark 到 Kubernetes — MaxCompute 的云原生开源生态实践之路
2019年5月14日,喜提浙江省科学技术进步一等奖的 MaxCompute 是阿里巴巴自研的 EB 级大数据计算平台.该平台依托阿里云飞天基础架构,是阿里巴巴在10年前做飞天系统的三大件之分布式计算部 ...
- oracle函数 power(x,y)
[功能]返回x的y次幂 [参数]x,y 数字型表达式 [返回]数字 [示例] select power(2.5,2),power(1.5,0),power(20,-1) from dual; 返回:6 ...
- 【tensorflow】】模型优化(一)指数衰减学习率
指数衰减学习率是先使用较大的学习率来快速得到一个较优的解,然后随着迭代的继续,逐步减小学习率,使得模型在训练后期更加稳定.在训练神经网络时,需要设置学习率(learning rate)控制参数的更新速 ...