# pta 习题集5-17 家谱处理

```John
Robert
Frank
Andrew
Nancy
David```

```John is the parent of Robert
Robert is a sibling of Nancy
David is a descendant of Robert```

### 输入格式:

```X is a child of Y
X is the parent of Y
X is a sibling of Y
X is a descendant of Y
X is an ancestor of Y```

### 输入样例:

```6 5
John
Robert
Frank
Andrew
Nancy
David
Robert is a child of John
Robert is an ancestor of Andrew
Robert is a sibling of Nancy
Nancy is the parent of Frank
John is a descendant of Andrew```

### 输出样例:

```True
True
True
False
False```
```#include <iostream>
#include <string.h>
#include <string>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#include <map>

using namespace std;
int n,m;
char b[105];
map<string,int> a;
int f[105];
string s;
int num[105];
int main()
{
scanf("%d%d\n",&n,&m);
int cnt=0;
memset(num,0,sizeof(num));
memset(f,0,sizeof(f));
for(int i=1;i<=n;i++)
{
gets(b);
s="";
for(int j=0;b[j]!='\0';j++)
{
if(b[j]==' ')
num[i]++;
else
s+=b[j];
}
a[s]=++cnt;
for(int k=i-1;k>=1;k--)
{
if(num[i]==num[k])
{
f[i]=f[k];
break;
}

if(num[i]==(num[k]+2))
{
f[i]=k;
break;
}
}
}
string x="",y="",z="";
for(int i=1;i<=m;i++)
{
x="";y="";z="";
gets(b);
int tag=1;
for(int j=0;b[j]!='\0';j++)
{
if(b[j]==' ')
{
tag++;
continue;
}
if(tag==1)
x+=b[j];
else if(tag==6)
y+=b[j];
else if(tag==4)
{
z+=b[j];
}
}
int ans=0;
int fx=a[x];int fy=a[y];
if(z.find("c")==0)
{
if(f[fx]==fy)
ans=1;
}
else if(z.find("a")==0)
{
while(fy!=0)
{
if(fx==f[fy])
{
ans=1;
break;
}
fy=f[fy];
}

}
else if(z.find("s")==0)
{
if(f[fx]==f[fy])
ans=1;
}
else if(z.find("p")==0)
{
if(f[fy]==fx)
ans=1;
}
else if(z.find("d")==0)
{
while(fx!=0)
{
if(fy==f[fx])
{
ans=1;
break;
}
fx=f[fx];
}

}
if(!ans)
printf("False\n");
else
printf("True\n");

}
return 0;
}```

500 篇文章45 人订阅

0 条评论

## 相关文章

51290

11710

360130

26180

34050

19240

23890

### 1163 访问艺术馆

时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解  查看运行结果 题目描述 Description     皮尔...

28970

18120

35850