DongDong认亲戚
new : 因为输入的字符串, 为了减少处理, 我们选择用 map<sting , int> 来映射, 算是一个小技巧, 让代码实现更加方便
#include <bits/stdc++.h>
using namespace std;
const int N = 2e4 + 10;
int fa[N];
int n, m;
map<string, int> q; // 非常好的离散化的方法
int find(int x)
{
return x == fa[x] ? x : fa[x] = find(fa[x]); // 非常简洁的写法
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++ )
{
fa[i] = i; // 注意要初始化
string t;
cin >> t;
q[t] = i;
}
while(m -- )
{
int x;
string s1, s2;
cin >> x >> s1 >> s2;
if(x == 1) fa[find(q[s1])] = find(q[s2]);
else
{
if(find(q[s1]) != find(q[s2])) cout << '0' << endl;
else cout << '1' << endl;
}
}
}