【NOIP2017提高A组冲刺11.3】机房比教室好多了

来源:互联网 发布:sqlserver 按月统计 编辑:程序博客网 时间:2024/06/05 23:47

题目

Description

这里有一个N 个点的树, 节点从1 到N 编号, 第i 条边连接了ai 和bi.
一开始第i 个点上有Ai 个石头. Takahashi 和Aoki 会玩一个游戏.
首先, Takahashi 会选择一个出发点; 然后, 从Takahashi 开始, 他们会轮流进行如下操作:
• 首先, 从当前的点上拿走一个石头.
• 然后, 走到一个相邻的点上.
当一个玩家无法执行操作时, 他就输了. 请你找到所有可以让Takahashi 获胜的出发点.

Input

N
A1 A2 ::: AN
a1 b1 …
aN−1 bN−1

Output

按升序输出一行表示可以让Takahashi 获胜的出发点.

Sample Input

输入1:
3
1 2 3
1 2
2 3
输入2:
5
5 4 1 2 3
1 2
1 3
2 4
2 5
输入3:
3
1 1 1
1 2
2 3

Sample Output

输出1:
2
输出2:
1 2
输出3:
(输出为空)

Data Constraint

对于30% 的数据, N <=7;Ai <= 6;
对于60% 的数据, N <= 2000;
对于100% 的数据, 1 <= N <= 10^6, 1 <= ai, bi <= N; 0 <= Ai <= 10^9, 保证输入的是一棵树.

题解

这道题目的性质在仔细分析一下还是比较明显的
对于在某个局面下的先手,他显然不会往比当前点权值大的点走(因为对手可以走回来,他血亏)
那么这道题目就解决了,对于一个起始点,它只能够往比自己权值小的子树走,如果这些子树中有一个局面是先手必败的,那么这个起始点就是先手必胜的
在具体的实现中,我们可以直接从点1开始做一次dfs,因为只能往比自己小的点走,所以说实际上这个树的边是一些单向边,那么我们直接打个标记就好了、

#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<cmath>#define fo(i,a,b) for(i=a;i<=b;i++)using namespace std;const int maxn=1e6+5;int fi[maxn],ne[maxn*2],dui[maxn*2],qc[maxn],fa[maxn];int c[maxn];bool bt[maxn],bq[maxn];int i,j,k,l,m,n,x,y,z,now;void add(int x,int y){    if (fi[x]==0) fi[x]=++now; else ne[qc[x]]=++now;    dui[now]=y; qc[x]=now;}void dfs(int x){    int i=fi[x];    bq[x]=true;    bool bz=false;    while (i){        if (c[dui[i]]<c[x]){            bz=true;            if (bq[dui[i]]==false){                fa[dui[i]]=x;                dfs(dui[i]);            }            if (bt[dui[i]]==false) bt[x]=true;        }        i=ne[i];    }    if (bz==false) bt[x]=false;}int main(){    freopen("c.in","r",stdin);    freopen("c.out","w",stdout);    scanf("%d",&n);    fo(i,1,n) scanf("%d",&c[i]);    fo(i,1,n-1){        scanf("%d%d",&x,&y);        add(x,y); add(y,x);    }    fo(i,1,n){        if (bq[i]==false) dfs(i);        if (bt[i]==true) printf("%d ",i);    }    return 0;}