[c++]代码库
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n, m, i, cn = 0;
cin>>n>>m;
getchar();
map<string, string> mf;
vector<string> last(110); //预先分配好110的大小
string name, a, b, c, d;;
for(i = 0; i < n; i++)
{
getline(cin, name);
cn = count(name.begin(), name.end(), ' '); //计算空格的数量
name = name.substr(cn);
if(cn)
{
mf[name] = last[cn / 2 - 1]; //记录上一个父节点
last[cn / 2] = name;
}
else
{
mf[name] = "root";
last[0] = name;
}
}
for(i = 0; i < m; i++)
{
cin >> a >> d >> d >> b >> d >> c;
if(b[0] == 'c')
{
if(mf[a] == c)
cout<<"True"<<endl;
else
cout<<"False"<<endl;
}
else if(b[0] == 'p')
{
if(mf[c] == a)
cout<<"True"<<endl;
else
cout<<"False"<<endl;
}
else if(b[0] == 's')
{
if(mf[c] == mf[a])
printf("True\n");
else
cout<<"False"<<endl;
}
else if(b[0] == 'a')
{
while(mf[c] != a && mf[c] != "root") c = mf[c];
if(mf[c] == "root")
cout<<"False"<<endl;
else
cout<<"True"<<endl;
}
else if(b[0] == 'd')
{
while(mf[a] != c && mf[a] != "root") a = mf[a];
if(mf[a] == "root")
cout<<"False"<<endl;
else
cout<<"True"<<endl;
}
}
return 0;
}
by: 发表于:2017-12-13 10:32:49 顶(0) | 踩(0) 回复
??
回复评论