SSL2829 2017年11月2日提高组T1 Tree

来源:互联网 发布:apache cgi bin 编辑:程序博客网 时间:2024/05/19 00:47

2017年11月2日提高组T1 Tree

Description

这里写图片描述

Input

这里写图片描述

Output

这里写图片描述

Sample Input

10
1 1 0 0 1 0 0 0 0 0
1 2
2 3
2 4
4 5
2 6
6 7
7 8
7 9
4 10
Sample Output

1 3 4 5 6
Hint

这里写图片描述

分析:如果一个点的颜色和父节点不同就要改变这个点的颜色,因为只有这样才能使它的颜色与父亲相同,所以标记一下就好啦。

代码

#include <cstdio>#define maxn 600000using namespace std;int a[maxn],n;bool f[maxn];int main(){//  freopen("tree.in","r",stdin);//  freopen("tree.out","w",stdout);    scanf("%d",&n);    for (int i=1;i<=n;i++)        scanf("%d",&a[i]);    if (a[1]==1) f[1]=true;    for (int i=1;i<n;i++)    {        int x,y;        scanf("%d%d",&x,&y);        if (a[x]!=a[y]) f[y]=true;    }    for (int i=1;i<=n;i++)        if (f[i]) printf("%d ",i);    fclose(stdin);    fclose(stdout);}