这是一道递归构造的题
先构造完子树,然后可以通过走father来调整自己
因为1没有father 需要特判 走一个儿子来调整
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2){ p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}
inline void read(int &x){
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
const int N=200005;
struct edge{
int u,v,next;
}G[N<<1];
int head[N],inum;
inline void add(int u,int v,int p){
G[p].u=u; G[p].v=v; G[p].next=head[u]; head[u]=p;
}
int n;
int tag[N],clr[N];
#define V G[p].v
inline void dfs(int u,int fa){
tag[u]=clr[u];
for (int p=head[u];p;p=G[p].next)
if (V!=fa)
dfs(V,u),tag[u]|=tag[V];
}
inline void solve(int u,int fa){
if (!tag[u]) return;
printf("%d ",u); clr[u]^=1;
for (int p=head[u];p;p=G[p].next)
if (V!=fa)
solve(V,u);
if (fa){
if (clr[u])
printf("%d %d %d ",fa,u,fa),clr[u]^=1;
else
printf("%d ",fa),clr[fa]^=1;
}
}
int main(){
int iu,iv;
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
read(n);
for (int i=1;i<=n;i++) read(clr[i]),clr[i]=(clr[i]==-1)?1:0;
for (int i=1;i<n;i++){
read(iu); read(iv);
add(iu,iv,++inum); add(iv,iu,++inum);
}
if (clr[1]==1){
int flag=0;
for (int i=2;i<=n;i++)
flag|=clr[i];
if (!flag)
return printf("%d %d %d %d ",1,G[head[1]].v,1,G[head[1]].v),0;
}
clr[1]^=1;
dfs(1,0);
solve(1,0);
if (clr[1]){
iv=G[head[1]].v;
printf("%d %d %d ",iv,1,iv);
}
return 0;
}