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;
}
原文地址:https://www.cnblogs.com/dzzy/p/5459744.html