蒟蒻求助:使用自己+Stl编写的自动离散化并查集却re了四个点
  • 板块P1551 亲戚
  • 楼主AzusidNya
  • 当前回复6
  • 已保存回复6
  • 发布时间2021/5/13 13:22
  • 上次更新2023/11/4 23:20:01
查看原帖
蒟蒻求助:使用自己+Stl编写的自动离散化并查集却re了四个点
61158
AzusidNya楼主2021/5/13 13:22

rt。代码如下:

#include<iostream>
#include<fstream>
#include<deque>
#include<unordered_map>
#include<algorithm>
using namespace std;
template<typename Ty>
class disjoint_set{
    private:
        std::unordered_map<Ty,int> hash;
        std::deque<int> father;
        std::deque<Ty> real_to_Ty; 
        int x;
        int inside_get(int realnum){
//        	std::cout<<"50";
            return ((father[realnum]==realnum)?father[realnum]:father[realnum]=inside_get(father[realnum]));
        }
        int insert(Ty insertnum){
            father.push_back(x);
            real_to_Ty[x]=insertnum;
            hash[insertnum]=x++;
            return x-1;
        }
    public:
        int desjoint_set(){
            x=1;
            return 0;
        }
        Ty get_father(Ty c){
            int num;
            if(hash.find(c)==hash.end())
                num=insert(c);
            else
                num=hash[c];
            int answer=inside_get(num);
            return real_to_Ty[answer];
        }
        int merge(Ty num1,Ty num2){
            father[hash[get_father(num1)]]=hash[get_father(num2)];
            return 0;
        }
};
int m,c,n;
int a,b;
disjoint_set<int> fa;
int main(){
	cin>>n>>m>>c;
	for(int i=1; i<=m; i++)
		cin>>a>>b,fa.merge(a,b);
	for(int i=1; i<=c; i++){
		cin>>a>>b;
		if(fa.get_father(a)==fa.get_father(b))
			cout<<"Yes\n";
		else	
			cout<<"No\n";
	}
	return 0;
}

测试记录

2021/5/13 13:22
加载中...