为啥TLE了???
查看原帖
为啥TLE了???
947029
Chen_Three楼主2024/7/31 13:28
#include<bits/stdc++.h>
using namespace std;
#define ll long long
template<typename T>inline void read(T &x){
	bool f = 1;
	x = 0;
	char ch = getchar();
	while(!isdigit(ch)){
		if(ch == '-'){
			f = -f;
			ch = getchar();
		}
	}
	while(isdigit(ch)){
		x = (x << 3) + (x << 1) + (ch ^ 48);
		ch = getchar();
	}
	x = (f ? x : -x);
	return;
} 
unordered_map<string, string> f;
string find(string x){
	if(f[x] == x){
		return x;
	}
	return f[x] = find(f[x]);
}
void join(string x, string y){
	f[find(x)] = find(y);
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, m;
	read(n), read(m);
	for(int i = 1; i <= n; i++){
		string s;
		cin >> s;
		f[s] = s;
	}
	for(int i = 1; i <= m; i++){
		string s1, s2;
		cin >> s1 >> s2;
		join(s1, s2);
	}
	int k;
	read(k);
	while(k--){
		string s1, s2;
		cin >> s1 >> s2;
		if(find(s1) == find(s2)){
			cout << "Yes.\n";
		}else{
			cout << "No.\n";
		}
	}
	return 0;
}

2024/7/31 13:28
加载中...