并查集板子题,利用map容器储存字符串,注意路径压缩 代码如下:

#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
typedef pair<int,int> PII;
map<string,string> mp;
string find(string s){
	if(mp[s]!=s){
		mp[s]=find(mp[s]);
	}
	return mp[s];
}
void solve(){
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		string s;
		cin>>s;
		mp[s]=s;
	}
	while(m--){
		int x;
		string s,s1;
		cin>>x>>s>>s1;
		if(x==1){
			mp[find(s)]=find(s1);
		}
		else{
			if(find(s)==find(s1)){
				cout<<1<<endl;
			}
			else cout<<0<<endl;
		}
		
	}
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int t=1;
//	cin>>t;
	while(t--){
		solve();
	}
}