toposort 并查集 优先队列 (Hash Function)(2021-08-12)

题目
Chiaki has just learned hash in today’s lesson. A hash function is any function that can be used to map data of arbitrary size to data of fixed size. As a beginner, Chiaki simply chooses a hash table of size n with hash function h(x) = x \bmod nh(x)=xmodn.
Unfortunately, the hash function may map two distinct values to the same hash value. For example, when n = 9 we have h(7) = h(16) = 7. It will cause a failure in the procession of insertion. In this case, Chiaki will check whether the next position (h(x) + 1) \bmod n(h(x)+1)modn is available or not. This task will not be finished until an available position is found. If we insert {7, 8, 16} into a hash table of size 9, we will finally get {16, -1, -1, -1, -1, -1, -1, 7, 8}. Available positions are marked as -1.
After done all the exercises, Chiaki became curious to the inverse problem. Can we rebuild the insertion sequence from a hash table? If there are multiple available insertion sequences, Chiaki would like to find the smallest one under lexicographical order.
Sequence a1, a2, …, an is lexicographically smaller than sequence b1, b2, …, bn if and only if there exists i (1 ≤ i ≤ n) satisfy that ai < bi and aj = bj for all 1 ≤ j < i.
输入:
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line of each case contains a positive integer n (1 ≤ n ≤ 2 x 105) – the length of the hash table.
The second line contains exactly n integers a1,a2,…,an (-1 ≤ ai ≤ 109).
It is guaranteed that the sum of all n does not exceed 2 x 106.
输出:
For each case, please output smallest available insertion sequence in a single line. Print an empty line when the available insertion sequence is empty. If there’s no such available insertion sequence, just output -1 in a single line.
示例1
输入
3
9
16 -1 -1 -1 -1 -1 -1 7 8
4
8 5 2 3
10
8 10 -1 -1 34 75 86 55 88 18
输出
7 8 16
2 3 5 8
34 75 86 55 88 18 8 10

参考:大佬详解
并查集 + toposort
查:
(1)没冲突的元素直接放
(2)有冲突的只有在他本该放的位置和现在位置之间充满元素才能放
妙:
1.把安放处的位置的父亲设为下一位置,判断本该位置和当前位置是否一个父亲
是则入队列
2.权值从小到大,编号从大到小 借助优先队
注:
访问过的点要标记

#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
const int maxn=2e5+9;
int p[maxn];//父节点 
int vis[maxn];//访问过的一定要标记 
int cnt;
int ans[maxn];
int a[maxn];
struct Edge{
	int val;
	int pos;
	Edge(int val_,int pos_){
		val=val_;
		pos=pos_;
	}
	bool operator < (Edge right) const{
		return val>right.val; 
	}
};
priority_queue<Edge> q;
void init(int n){//初始化 n个集合 
	for(int i=0;i<n;i++){
		p[i]=i;
	} 
}
int find(int x){
	return x==p[x]? x:p[x]=find(p[x]);
}
void toposort(int n){
	while(!q.empty()){
		int top=q.top().pos;
		ans[++cnt]=q.top().val;
		q.pop();
		p[find(top)]=find((top+1)%n);//把一串连起来的数的父亲置为下一没访问过的位置 
		int np=find(top);
		if(vis[np]||a[np]==-1||find(a[np]%n)!=np)//他本该放的位置的父亲是不是他现在的位置 
			continue;
		q.push(Edge(a[np],np));
		vis[np]=1;//记得标记 
	}
}
int main(){
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		int num=0;
		cnt=0;
		memset(vis,0,sizeof(vis));
		for(int i=0;i<n;i++){
			scanf("%d",&a[i]); 
			if(a[i]!=-1){//一共有几个数需要散列 
				num++;
			}
			if(a[i]%n==i){//没冲突的元素从小到大放 
				q.push(Edge(a[i],i));
				vis[i]=1;//该点已经访问 
			}
		}
		init(n);
		toposort(n);
		if(cnt<num){//给的hash表是错的 
			cout<<"-1"<<endl;
		}
		else if(num==0){
			cout<<"\n";
		}
		else{
			for(int i=1;i<=cnt;i++){
			if(i==1)
				cout<<ans[i];
			else
				cout<<" "<<ans[i];
			} 
			cout<<endl;
		}
	}
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值