62分WA+TLE求调
查看原帖
62分WA+TLE求调
1235819
_std_xzh楼主2024/7/20 14:39
#include <bits/stdc++.h>
using namespace std;
map<string , string> f;
string a[100005][5] , b[100005][5];
int n , m , k , cur , cnt;
string s[1000005] , x , y , z;
string find(string x){
	if(x == f[x])return x;
	else {
		f[x] = find(f[x]);
		return f[x];
	}
}
void unionn(string i , string j){
	string i_f = find(i);
	string j_f = find(j);
	f[i_f] = j_f;
}
int main(){
	cin >> n;
	for(int i = 1;i <= n;i++){
		cin >> a[i][1] >> a[i][2];
		s[++cur] = a[i][1];
		s[++cur] = a[i][2];
	}
	cin >> m;
	for(int i = 1;i <= m;i++){
		cin >> b[i][1] >> b[i][2];
		s[++cur] = b[i][1];
		s[++cur] = b[i][2];
	}
	for(int i = 1;i <= cur;i++)f[s[i]] = s[i];
	cin >> k;
	while(k--){
		cin >> x >> y >> z;
		unionn(x , y);
		unionn(y , z);
	} 
	for(int i = 1;i <= n;i++){
		x = a[i][1] , y = a[i][2];
		if(find(x) != find(y))cnt++;
	}
	for(int i = 1;i <= m;i++){
		x = b[i][1] , y = b[i][2];
		if(find(x) == find(y))cnt++;
	}
	cout << cnt;
	return 0;
}
2024/7/20 14:39
加载中...