这道题属于是那种看上去很有思路,然后无从下手,写了个dfs感觉实在是不行。

后面仔细看了一下,这个题是用的构造dfs,基本是树上dfs,时间复杂度是O(V+E)

新构造的一个参数作为根,整个dfs表示这个根的子树已经全部变为黑色(除了根以外)

刚开始是一直搜,搜到最后如果是粉的,递归以后再单独处理一次

其实当时如果能想到直接从每个结点的根结点入手,这个题应该就不在话下了

只能说写着写着心乱了

#include<iostream>
#include<utility>
#include<vector>
using namespace std;
typedef long long ll;
#define fi(i,a,b) for(int i = a; i <= b; ++i)
#define fr(i,a,b) for(int i = a; i >= b; --i)
#define x first
#define y second
#define sz(x) ((int)(x).size())
#define pb push_back
using pii = pair<int,int>;
//#define DEBUG
#define int long long
const int N = 2e5 + 5;
vector<int> tre[N];
int vex[N];
void dfs(int x,int y){
fi(i,0,sz(tre[x])-1){
int v = tre[x][i];
if(v == y) continue;
vex[v] ^= 1;
cout << v << " ";
dfs(v,x);
cout << x << " ";
vex[x] ^= 1;
if(vex[v] == 0)
cout << v << " " << x << " ",vex[v] ^=1 ,vex[x] ^= 1;
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
fi(i,1,n) cin >> vex[i],vex[i] = max(vex[i],(ll)0);
fi(i,1,n-1){
int a,b;
cin >> a >> b;
tre[a].pb(b);
tre[b].pb(a);
}
cout << 1 << " ";
dfs(1,0);
if(vex[1] == 0)
cout << tre[1][0] << " " << 1 << " " << tre[1][0];
#ifdef DEBUG
//freopen(D:\in.txt,r,stdin);
#endif
return 0;
}

随机推荐

  1. 题解:CF1956A Nene's Game

    这道题其实挺有意思,多测里面还套了个多测. 思路就是用向量模拟删除过程,具体请看代码里的注释. #include <bits/stdc++.h> using namespace std; ...

  2. 将自己喜欢的网页保存为单个文件包括图片(mhtml文件)

    from selenium import webdriver driver = webdriver.Chrome(r'C:\chromedriver_win32\chromedriver.exe') ...

  3. kubernets之横向伸缩pod与集群节点

    一  pod的自动伸缩容的应用背景 在面对负载并发过高的时候,我们或许希望能够提高RS,RC以及Deployment等的replicas的参数来增加pod的cpu,mem等,或者是通过提高每个容器的r ...

  4. vue-element-admin 运行踩坑笔记

      npm WARN deprecated svgo@1.3.2: This SVGO version is no longer supported. Upgrade to v2.x.x. npm E ...

  5. C# wpf 实现截屏框实时截屏功能

    wpf截屏系列第一章 使用GDI+实现截屏第二章 使用DockPanel制作截屏框第三章 实现截屏框实时截屏(本章)第四章 使用ffmpeg命令行实现录屏 文章目录wpf截屏系列前言一.实现步骤1.获 ...

  6. FreeSWITCH使用soundtouch进行变声

    操作系统 :CentOS 7.6_x64 FreeSWITCH版本 :1.10.9   FreeSWITCH里面有个mod_soundtouch模块,支持通话实时变声,今天整理下CentOS 7环境下 ...

  7. k8s集群下node节点使用kubectl命令

    问题描述:The connection to the server localhost:8080 was refused - did you specify the right host or por ...

  8. 腾讯redis2.3集群搭建

    环境规划: IP 主机名 组件 配置 备注 192.168.3.81 node1 8核16G 磁盘X2-50G 没有8核16G无法添加服务器 192.168.3.82 node2 cache,prox ...

  9. centos7.x开机启动流程centos6.x开机启动流程

    centos6.x开机启动流程 开机加电bios自检 MBR引导将启动权交给硬盘 硬盘 0 柱面0磁道 1扇区512字节,其中 前466字节为引导 后 64字节分区表 2字节为分区结束标志 加载gru ...

  10. 【论文笔记】R-CNN系列之代码实现

    代码源码 前情回顾:[论文笔记]R-CNN系列之论文理解 整体架构 由三部分组成 (1)提取特征的卷积网络extractor (2)输入特征获得建议框rois的rpn网络 (3)传入rois和特征图, ...