题目
https://pintia.cn/problem-sets/988034414048743424/problems/988040973986308096
思路
并查集,主要是STL容器的合理使用
①处理输入的空格
②如何查找存储字符串,并转换
代码
#include <iostream>
#include <cstring>
#include <stack>
#include <algorithm>
#include <queue>
#include <map>
#define MAXX 101
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
getchar();
string s,s1;
vector<string> v[MAXX];
map<string ,string> fs;
for (int i = 0; i < n; ++i)
{
getline(cin,s);
int len = s.size();
int cnt = 0;
s1.clear();
for (int j = 0; j < len; ++j)
{
if (s[j]==' ')
cnt++;
else
s1+=s[j];
}
// cout<<s1<<endl;
if (cnt == 0)
{
v[cnt].push_back(s1);
fs[s1] = "0";
}
else
{
fs[s1] = v[cnt/2-1].back();
v[cnt/2].push_back(s1);
}
}
while (m--)
{
string a,b,c,d,e,f;
cin>>a>>b>>c>>d>>e>>f;
//cout<<fs[a]<<endl;
//cout<<f<<endl;
if (d[0] == 'c')
{
if (fs[a] == f)
cout<<"True"<<endl;
else
cout<<"False"<<endl;
}
else if (d[0] == 'p')
{
if (fs[f] == a)
cout<<"True"<<endl;
else
cout<<"False"<<endl;
}
else if (d[0]=='s')
{
if (fs[a] == fs[f])
cout<<"True"<<endl;
else
cout<<"False"<<endl;
}
else if (d[0] == 'd')
{
while (fs[a] != "0" && fs[a]!=f)
a = fs[a];
if (fs[a] == "0")
cout<<"False"<<endl;
else
cout<<"True"<<endl;
}
else
{
while (fs[f] != "0" && fs[f]!=a)
f = fs[f];
if (fs[f] == "0")
cout<<"False"<<endl;
else
cout<<"True"<<endl;
}
}
return 0;
}