这道题属于是那种看上去很有思路,然后无从下手,写了个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. 如果win报错无法加载文件 C:\Users\xx\AppData\Roaming\npm\pnpm.ps1,因为在此系统上禁止运行脚本

    点击查看代码 Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

  2. ༺$Musique Collection 1$༻

    ~~头图~~ 自取捏 <\(Landslide\)>-\(Oh\,Wonder\) I know it hurts sometimes but You'll get over it You ...

  3. 阿里云sdk调用

    slb调用 环境包安装 pip install alibabacloud_credentials  --trusted-host mirrors.aliyun.com  -i  http://mirr ...

  4. 编译mmdetection3d时,无root权限下为虚拟环境单独创建CUDA版本

    在跑一些深度学习代码的时候,如果需要使用mmdetection3d框架,下载的pytorch的cudatoolkit最好需要和本机的cuda版本是一样的,即输入nvcc -V命令后显示的版本一样. 但 ...

  5. webapi中间件没有使用终结点中间件时的注意事项

    最小webapi 最小webapi默认的中间件配置是这样的 app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers( ...

  6. Spring Boot中的 6 种API请求参数读取方式

    使用Spring Boot开发API的时候,读取请求参数是服务端编码中最基本的一项操作,Spring Boot中也提供了多种机制来满足不同的API设计要求. 接下来,就通过本文,为大家总结6种常用的请 ...

  7. WampServer 的安装

    一, 下载   wampserver3.2.0_x64.exe  文件 二,在D盘新建wamp64文件 三,以管理员的方式运行安装文件 只有两种语方,选择 English 接受协议 下一步: 点击下一 ...

  8. 图解JDK7及其早期版本HashMap扩容死锁问题

    在JDK7及其早期版本中HashMap在多线程环境下会发生扩容死锁的问题. HashMap中在创建时默认会有16个桶,有一个默认加载因子0.75,如果Map中的Entry数量达到阈值(16*0.75) ...

  9. linux下 IPv6组播(C++)

      Server #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <s ...

  10. C#笔记 关于采集卡

    周更!节日快乐! 1. 参数 1.1 CAM file CAM file是文件扩展名为.cam的可读ASCII文件,包含了参数列表,比如:AcquisitionMode,TrigMode等.通过McS ...