Mu class (5)

tree:

Heap:
maximum heap: the value of each node is greater than or equal to its child nodes.
Minimum stack: the value of each node is less than or equal to its child nodes.

Heap memory:
can be understood as a binary tree is a complete binary tree orderly relations between nodes, so you can use an array to represent.
For the labeled node i, the index of its left subtree node is 2i, the right node 2i + 1, father node subscript i / 2 (rounded down).
In programming, the bit operation used instead of 2 * can directly increase the operating speed. -
Certain specific compiler will rewrite the bit multiplication operation.

Huffman trees and Huffman coding:

Also known as the optimal Huffman tree tree is a kind of weighted shortest path tree.

Path: from the tree to a node between the other branch nodes constitute paths between the two nodes.
Path Length: the number of branches on path length of the path is referred.
Tree path length: the path from the root to each node, and.
Right: a given amount of an entity, is a value or a description of some attributes of an entity. In the data structure, the node entities (elements) and side (relationships) into two categories, so there is a corresponding edge weights and the weights of the nodes.
Weighted path length junction: Right from the node to the length of the path between the root node and multiplied.
Weighted length path tree: weighted path tree all leaf nodes and long only.
Huffman: Suppose that there are m weights {w1, w2, w3, ... , wn}, n may be configured to contain a binary tree leaf nodes, each leaf node weight as Wi, wherein the weighted WPL minimum path length called optimal binary or binary Huffman tree.

Huffman tree is:
since there is no degree of Huffman tree node 1, then there are a total of 2n -1 Huffman tree of n nodes of the leaf node, may be stored in a size of 2n -1 one-dimensional array.

typedef structint weight; //结点的权重
    int parent,lchild,rchild;//结点的双亲、左孩子、右孩子的下标
}HTNode,*HuffmanTree

Initialization: First, dynamic application units 2n, 2n-1 and cycle times, from unit number 1 starts, in turn, to a 2n-1 in all cells parent, left child and right child index is initialized to 0, and finally recycled n times, the weight before entering the n units leaves.
Create a tree: cycle times n-1, n-1 by selecting times, delete and merge to create a Huffman tree. Is selected from the current selection in the forest of parents 0 and the minimum weight tree root two s1 and s2. It refers to the deleted node s1 and s2 to the parent non-zero. Combined is the weight of s1 and s2 and as the weight of a new node successively stored in the unit after the n + 1 array while the record of the new node left child subscript s1, right child subscript s2 .

void SelectMin(HuffmanTree hT, int n, int &s1, int &s2)
{
    s1 = s2 = 0;

    int i;
    for(i = 1; i < n; ++ i){
        if(0 == hT[i].parent){
            if(0 == s1){
                s1 = i;
            }
            else{
                s2 = i;
                break;
            }
        }
    }
    if(hT[s1].weight > hT[s2].weight){
        int t = s1;
        s1 = s2;
        s2 = t;
    }

    for(i += 1; i < n; ++ i){
        if(0 == hT[i].parent){
            if(hT[i].weight < hT[s1].weight){
                s2 = s1;
                s1 = i;
            }else if(hT[i].weight < hT[s2].weight){
                s2 = i;
            }
        }
    }
}
// 构造有n个权值(叶子节点)的哈夫曼树 
void CreateHufmanTree(HuffmanTree &hT)
{
    int n, m;
    cin >> n;
    m = 2*n - 1;

    hT = new HTNode[m + 1];    // 0号节点不使用 
    for(int i = 1; i <= m; ++ i){
        hT[i].parent = hT[i].lChild = hT[i].rChild = 0;
    }
    for(int i = 1; i <= n; ++ i){
        cin >> hT[i].weight;    // 输入前n个单元中叶子结点的权值 
    }
    hT[0].weight = m;    // 用0号节点保存节点数量 

    /****** 初始化完毕, 创建哈夫曼树 ******/
    for(int i = n + 1; i <= m; ++ i){
          //通过n-1次的选择、删除、合并来创建二叉树
        int s1, s2;
        SelectMin(hT, i, s1, s2);

        hT[s1].parent = hT[s2].parent = i;
        hT[i].lChild = s1; hT[i].rChild = s2;    // 作为新节点的孩子 
        hT[i].weight = hT[s1].weight + hT[s2].weight;    // 新节点为左右孩子节点权值之和 
    }
}

Disjoint-set:
In some applications the set of N elements of a problem, we usually make at the beginning of each set of elements that make up a single element, then a certain order to merge elements of the same group belong to the set where, during to Find a repeated element in which the collection. In recent years this type of problem recurring theme in domestic and international competition in informatics, which is characterized does not seem complicated, but the amount of data is great, if your normal data structure to describe it, often in the space is too large , the computer can not afford; even in the space of barely passed, the time complexity of the operation is also very high, simply can not calculate the results of questions need to run the game within the stipulated time (1 to 3 seconds), and can only be used to check set to describe.

   并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题。常常在使用中以tree来表示。

Uses:
maintenance of a connected undirected graph, m n points determined edges increase the number of edges may happened when all communication points
determined in a undirected graph, add edges between two points is generated if ring (minimum spanning tree Crewe to be useful in Pascal)
maintenance operations such as the collection.

Operation:
(. 1) of Union (Root1, Root2): a subset of the set Root2 incorporated in Root1. This requires two sets disjoint, or not to perform the merge.
(2) Find (x): a collection of search where the single element x, and returns the name of the collection.
(3) UnionFindSets (s): constructor, and the focus search is initialized to s-th element of s only a single element of the subset.

The main code:

long long int chazhao(long long int root)
{
        long long int son,tmp;
        son=root;
        while(root!=a[root])
        {
            root=a[root];
        }
        while(son != root)
	   {
		  tmp = a[son];
		  a[son] = root;
		  son = tmp;
	   }
	     return root;
}

There is also a piece of code to shorten the path to facilitate directly to the supreme leader.

Put examples:
Description
has n individual, numbered 1-n.
There is now a party, at the party, we will introduce their friends to each other.
That is: If a know b, b know c. Then at the ball, a c will be appreciated by b.
Now, given the relationship of m
for each relationship Description:
ab &
expressed as a number of people and number b is a friend.

Format
input format
input m and n
Subsequently m rows, each behavior ab

Output format
finally asked, how many circle of friends there.

Sample
Sample input the Copy
. 5. 3
. 1 2
2. 3
. 4. 5
sample output the Copy
2

This is a simple disjoint-set will soon be able to do it:

#include<stdio.h>
long long int a[100000],b[100000]={0},t=0;
long long int chazhao(long long int root)
{
        long long int son,tmp;
        son=root;
        while(root!=a[root])
        {
            root=a[root];
        }
        while(son != root)
	   {
		  tmp = a[son];
		  a[son] = root;
		  son = tmp;
	   }
	     return root;
}
int main()
{
    long long int n,m,i,total,x,y,p,q;
     scanf("%lld %lld",&n,&m);
      total=n-1;
    for(i=1;i<=n;i++)
        a[i]=i;
    for(i=0;i<m;i++)
    {
        scanf("%lld %lld",&q,&p);
        x=chazhao(q);
        y=chazhao(p);
        if(x!=y)
        {
            a[y]=x;
            total--;
        }
    }
     for(i=1;i<=n;i++)
     {
        chazhao(i);
     }
     for(i=1;i<=n;i++)
        printf("%d ",a[i]);
     printf("\n");
     for(i=1;i<=n;i++)
     {
         b[a[i]]++;
     }
     for(i=1;i<=n;i++)
        if(b[i]!=0)
        t++;
     printf("%lld",t);
    return 0;
}

Published 13 original articles · won praise 0 · Views 342

Guess you like

Origin blog.csdn.net/JSUChengyuezhen/article/details/104408042