And search the most easy to understand at present. https://www.cnblogs.com/xzxl/p/7226557.html
First intercept a paragraph. You can see how much it tells and searches the collection through the method of story. It is easy for young and old to understand and known by women and children.
The parallel search set consists of an integer array and two functions. The array pre [] records the leading point of each point. The function find is to find and the function join is to merge.
There are thousands of heroes scattered in the Jianghu. They have no legitimate occupation. They walk around with swords on their backs all day. If they meet someone who is not the same as themselves, they will inevitably have a fight. But one of the great Xia's advantages is that they are loyal and never beat their friends. Moreover, they believe that "a friend of a friend is my friend". As long as they can be connected through a friend relationship, they think they are their own people no matter how many turns they take. In this way, the Jianghu has formed a community one by one, which is connected through the friendship between two people. People who are not in the same community can't be connected through friends anyway, so they can rest assured to die. But how do two people who don't know each other judge whether they belong to a circle of friends? We can select a well-known person in each circle of friends as the representative of the circle. In this way, each circle can be named "Zidane's friend team" and "Ronaldo's friend team"... As long as they check with each other whether their captain is the same person, they can determine the relationship between enemies and friends. But there are still problems. Heroes only know who their direct friends are. Many people don't know the captain at all. To judge who their captain is, they can only ask aimlessly through their friend relationship: "are you the captain? Are you the captain?" In this way, the captain can't hang on his face, and the efficiency is too low, and he may fall into an infinite cycle. So the captain ordered a new team. Everyone in the team implements a hierarchical system to form a tree structure. My team leader is the root node. Below are level 2 and level 3 players. Everyone just needs to remember who their superiors are. When judging enemies and friends, as long as you ask up layer by layer until the highest level, you can determine who the captain is in a short time. Since we only care about whether two people are connected, it doesn't matter how they are connected, what the internal structure of each circle is, or even who the team leader is. So we can let the captain regroup at will, as long as we don't make mistakes in the relationship between enemies and friends. So the sect came into being.

Let's look at the implementation of parallel query set. int pre[1000]; This array records who is the superior of each great Xia. Heroes start numbering from 1 or 0 (depending on the meaning of the question), pre[15]=3 means that the superior of great Xia 15 is great Xia 3. If a person's superior is himself, he is the leader. That's all. There are also those who are alone, such as Ouyang Feng, whose superior is himself. Everyone recognizes only his superiors. For example, Hu qingniu only knew that his superior was Yang Zuoshi. Who is Zhang Wuji? incognizance! If you want to know who your leader is, you can only check it level by level. The find function is used to find the leader, and the meaning can't be clearer (the path compression algorithm doesn't matter first, but later)
OK, try it again and understand the optimization algorithm of search set. It's better to talk about lanterns.
Simply don't fight.
Go directly to the advanced joint search set, or put it in the kruskar algorithm; Inside
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; int n,m,tot=0,k=0;//n total number of endpoints, m number of sides, tot record the final answer, and k how many sides have been connected int fat[200010],rank[234124];//Record collective boss struct node { int from,to,dis;//Structure storage edge }edge[200010]; bool cmp(const node &a,const node &b)//Sort sort (of course, you can also sort quickly) { return a.dis<b.dis; } int father(int x)//Find the collective boss and check part of the collection { if(fat[x]!=x) //At first, I thought recursion was the most primitive, while using while was slow.. return father(fat[x]); return x; } void unionn(int x,int y)//Join the group and look up part of the collection { x=father(x); y=father(y); if(x==y) return ; if(rank[x]>rank[y]) fat[y]=x; else { if(rank[x]==rank[y]) rank[y]++; fat[x]=y; } } int main() { scanf("%d%d",&n,&m);//Enter number of points, number of sides for(int i=1;i<=m;i++) { scanf("%d%d%d",&edge[i].from,&edge[i].to,&edge[i].dis);//Enter information for the edge } for(int i=1;i<=n;i++) { fat[i]=i; rank[i]=1;//I was my boss in the beginning (initialization) } sort(edge+1,edge+1+m,cmp);//Sort by weight (embodiment of kruskal) for(int i=1;i<=m;i++)//Traversal from small to large { if(k==n-1) break;//N points need n-1 edges to connect if(father(edge[i].from)!=father(edge[i].to))//If not in a group { unionn(edge[i].from,edge[i].to);//join tot+=edge[i].dis;//Record edge weight k++;//Number of connected edges + 1 } } printf("%d",tot); return 0; }
In fact, this is still the beginning. The next step is prim algorithm. First go through all the algorithms, and then brush the topics one by one to make strange and advanced.
If you have readers, you might as well pay attention to it. I'll take you a quick introduction to an algorithm.