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 等效, 所以不需 ...
随机推荐
- jq 添加内容
向页面动态添加内容,一般用于动态网页,需要即时请求数据,并更新在页面上,使用append()更多一些,empty() - 清空所有子元素,remove() - 清除自身所有子元素. append() ...
- 2019-4-10-VisualStudio-2019-尝试使用-C#-8.0-新的方式
title author date CreateTime categories VisualStudio 2019 尝试使用 C# 8.0 新的方式 lindexi 2019-04-10 10:41: ...
- 2019-6-23-开源项目使用-appveyor-自动构建
title author date CreateTime categories 开源项目使用 appveyor 自动构建 lindexi 2019-06-23 11:47:40 +0800 2019- ...
- QT 建立信号和槽的联系(事件处理)
Qt中事件处理机制叫做“信号”和“槽”signal &slot. 其模型为: 对象a中有一个信号signal:XXX(代表一个事件) 对象b中有一个槽slot:YYY(事件处理函数) 用con ...
- 快速完成智能数据构建,Dataphin公共云版本全面解读
公测两个月,Dataphin公共云版本已经受到了阿里云上众多轻量级用户的关注.事实上,Dataphin作为一款大数据智能构建与管理的产品,其核心功能是面向各行各业大数据建设.管理及应用诉求,一站式提供 ...
- 阿里云发布 Redis 5.0 缓存服务:全新 Stream 数据类型带来不一样缓存体验
4月24日,阿里云正式宣布推出全新 Redis 5.0 版本云数据库缓存服务,据悉该服务完全兼容 4.0 及早期版本,继承了其一贯的安全,稳定,高效等特点并带来了全新的 Stream 数据结构及多项优 ...
- Data Flow-File Read-网络距离
- spark sql thrift server
### create data ## cat ## echo "$(date ;echo ## cat }'";exit}' ..} do passwd) echo "$ ...
- 2019-8-31-PowerShell-通过-WMI-获取系统服务
title author date CreateTime categories PowerShell 通过 WMI 获取系统服务 lindexi 2019-08-31 16:55:58 +0800 2 ...
- SuperSocket进程级别隔离
在 SuperSocket 1.5 中, 我们增加了 AppDomain 级别隔离的功能,让你可以运行多个服务器实例在相互独立的 AppDomain 上. 此功能提供了较高级别的安全性和资源的隔离,并 ...