P2256 一中校运会之百米跑(map+并查集)

时间:2022-05-12 17:01:54

思路:首先处理名字,让字符串直接映射唯一一个数字,这就用map<string, int>即可。

   然后,直接用并查集

#include<iostream>
#include<map>
#include<string>
using namespace std; const int maxn = 2e4 + ;
map<string, int>mm;
int fa[maxn]; int find(int x){
if (fa[x] == x)return x;
return fa[x] = find(fa[x]);
} int n, m;
string name;
string xx, yy; int main(){
cin >> n >> m;
for (int i = ; i <= n; ++i)fa[i] = i;
for (int i = ; i <= n; ++i)
{
cin >> name; mm[name] = i;
}
for (int i = ; i <= m; ++i)
{
cin >> xx >> yy;
int x = find(mm[xx]);
int y = find(mm[yy]);
if (x != y){ fa[x] = y; }
}
cin >> n;
for (int i = ; i < n; ++i)
{
cin >> xx >> yy;;
int x = find(mm[xx]);
int y = find(mm[yy]);
if (x == y)cout << "Yes." << endl;
else cout << "No." << endl;
}
}