And check-defined set of

Disjoint-set: A method of maintaining a set of data structures

A merger onion

Second, find find

Initialization, find (path compression), merger

Heap: essentially a binary tree

// disjoint-set review
// heap review
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stack>
#include<cstdio>
#include<queue>
#include<map>
#include<vector>
#include<set>
using namespace std;
const int maxn=1010;
const int INF=0x3fffffff;
// disjoint-set data structures: an array
int fa [maxn];
//: Find merger
// Find the optimization of: compression path
int findfa(int x){
	if(x!=fa[x]) fa[x]=findfa(fa[x]);
	return fa[x];
}
// In fact, the merger has also optimized, and sometimes when the check is entitled to set the value of the merger there will be selective, based on the size of the combined weights 
void ini(int a,int b){
	int fa1=findfa(a);
	int fa2=findfa(b);
	if(fa1!=fa2) fa[fa2]=fa1;
}
// generated set of disjoint-set: a tree

//stack:
// a heap is a specialized tree-based data structure (tree), and check the set, in fact, the essence of the heap can be a tree 
// General rapid data structures: the priority queue:
// default priority queue: Big pile top (do not write their own functions) 
priority_queue<int> q1;
// small pile top
priority_queue<int,vector<int>,greater<int> > q2;
// with general data structure (that need to write your own functions) 

// need to write your own functions 
int heap[maxn];  //just like堆 
// But the master stack of top-up and bottom-up Two wording is very important
// stack building process: since adjustment downward, but requires enumeration backwards adjustment, the adjustment of all non-leaf nodes, from n / 2 to 1 and backwards adjustment, but the adjustment is a top-down
// delete the node comprising: Remove the top stack element, with the last element of the first cover, and then adjust downjust (1, n)
void downadjust (int low, int high) {// downward adjustment: delete, built heap, heap sort 
	int i=low,j=2*i;
	while(j<=high){
		// If there are children present, and with children is greater than the value of left child, just like after the shift, and then talk to i compare the size
		if(j+1<=high&&heap[j+1]>heap[j]) j++;
		if(heap[j]>heap[i]){
			// If the child is larger than the father, it is necessary to exchange
			swap(heap[j],heap[i]);
			i=j;
			j=2*i; 
		} 
		else break; // otherwise exit 
	}
} 
void create(){
	for(int i=1;i<=n;i++) cin>>heap[i];
	for(int i=n/2;i>=1;i--) downadjust(i,n);
} // backwards adjustment: in order to meet the maximum value of each node are based on his parent node 
// delete the top of the heap element
void del(){
	heap[1]=heap[n--];
	downadjust (1, n); // adjust the top of the stack 
} 
// add an element when: rearmost added again, then adjusted upwards from the bottom 
void upadjust(int high,int low){
	int i=high,j=high/2;
	while(j>=low){
		// always compare the words of his father, there is no j ++ or j-- a direct comparison on it
		if(heap[j]<heap[i]){
			swap(heap[j],heap[i]); 
			i = j; // if the father than the small, exchanged 
			j/=2;
		} 
		else break;
	}
}
void insert(int x){
	heap [++ n] = x; // then added at the end 
	upadjust(n,1);
}
// delete the top of the heap, added at the end
 
// heapsort
// Since the top of the stack the stack elements are determined maximum, so that every element to the top of the stack and the rearmost exchange element, then to adjust the top-down from the top of the stack
// are removed so that each maximum top of the stack elements, but also reduce the range of
void heapsort(){
	create (); // build the first reactor 
	for(int i=n;i>1;i--) {
		swap (a [i], a [1]); // exchange 
		downadjust (1, i-1); // range is further reduced 
	}
	for(int i=1;i<=n;i++) cout<<heap[i]<<" ";
	// childhood to large sequence 
} 

  

 

Guess you like

Origin www.cnblogs.com/shirlybaby/p/12274055.html