JZOJ 5438. 【NOIP2017提高A组集训10.31】Tree

来源:互联网 发布:揭阳有没有学淘宝培训 编辑:程序博客网 时间:2024/06/05 22:38

Description

Description

Input

Input

Output

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

Data Constraint

Data Constraint

Solution

  • 首先,答案一定是唯一的,一个点不可能被标记两次。

  • 考虑在树中由下至上处理。

  • 一个点的颜色如果和其父亲相同,那么就不可能标记这个点,不然就没有贡献。

  • 那么我们讨论与父亲颜色不相同的情况。

  • 如果自己是 黑色 ,父亲是 白色 ,那么显然标记这个点,不然之后就不能再改到了。

  • 如果自己是 白色 ,父亲是 黑色 ,那么显然也要标记这个点,不然之后就不能统一修改了。

  • 综上所述,一个点的颜色如果和其父亲相同,就直接标记这个点。

  • 注意特判根节点(我设为 1 号点),根节点为黑色的话也要标记它。

  • 于是我们把要标记的点统计在一个桶里,顺序输出即可(快排都不用)。

  • 时间复杂度是飞快的 O(N)

Code

#include<cstdio>using namespace std;const int N=5e5+1;bool a[N],ans[N];inline int read(){    int X=0,w=1; char ch=0;    while(ch<'0' || ch>'9') {if(ch=='-') w=-1;ch=getchar();}    while(ch>='0' && ch<='9') X=(X<<3)+(X<<1)+ch-'0',ch=getchar();    return X*w;}inline void write(int x){    if(x>9) write(x/10);    putchar(x%10+'0');}int main(){    int n=read();    for(int i=1;i<=n;i++) a[i]=read();    ans[1]=a[1];    for(int i=1;i<n;i++)    {        int x=read(),y=read();        if(a[x]!=a[y]) ans[y]=true;    }    for(int i=1;i<=n;i++)         if(ans[i]) write(i),putchar(' ');    return 0;}
阅读全文
1 0
原创粉丝点击