传统弱校HFUT的蒟蒻,真相只有一个

2015安徽省赛 J.镜像树

http://xcacm.hfut.edu.cn/problem.php?id=1214

乱搞题

数组+结构体 递归遍历

#include<iostream>
#include<cstdio>
#include<cstring>
#define within(x,a,b) ((unsigned)((x)-(a))<=((b)-(a)))
using namespace std;
int readint(int *p)
{
    int ch;
    while(!within(ch=getchar(),'0','9'))
        if(ch == EOF) return EOF;
    int rslt = 0;
    do
        rslt=rslt*10+(ch-'0');
    while(within(ch=getchar(),'0','9'));
    *p = rslt;
    return 1;
}
int n,just=0;
struct donser
{
    int data;
    int left;
    int right;
};
struct donser d[1005];

void judge(int i,int j)
{
    if(d[i].data==0&&d[j].data==0) return;
    if(d[i].data!=d[j].data){just=1;return;}
    int x,y;
    x=d[i].left;
    y=d[j].right;
    judge(x,y);
    x=d[i].right;
    y=d[j].left;
    judge(x,y);
    return;
}

int main()
{
    int i,k;
    readint(&k);
    while(k--)
    {
        readint(&n);
        int ro,le,re;
        for(i=1;i<=n;i++)
        {
            readint(&ro);
            readint(&le);
            readint(&re);
            d[ro].left=le;
            d[ro].right=re;
        }
        for(i=1;i<=n;i++)
        {
            readint(&ro);
            d[i].data=ro;
        }
        judge(1,1);
        if(just==1){just=0;cout<<"No"<<endl;continue;}
        cout<<"Yes"<<endl;
        memset(d,0,sizeof(d));
    }
    return 0;
}

 

posted @ 2016-05-04 20:53  未名亚柳  阅读(156)  评论(0编辑  收藏  举报