Detailed disjoint-set easy to understand humor (+ revolution change)

In order to explain the principle of check and set, I will give an interesting example.

 Littered with all kinds of heroes on the words of the political arena, there are many thousands. They no proper job, walking around all day carrying a sword on the outside, not the way people meet and themselves, it is inevitably a fight. But heroes has the advantage that sense of obligation, absolutely not to fight his friend. And they believe in "friend of a friend is my friend," as long as the relationship through a friend in series, no matter how many turn turn, are considered to be one of us. As a result, the rivers and lakes on the formation of a one gang, linked together by friendship between the two by two. But not in the same gang who, in any case can not be put together by friendship, so you can rest assured to play dead. But the two original people know each other, how to determine whether it belongs to a circle of friends?

We can choose a relatively prestigious people in each circle of friends, as a representative of the circle. Thus, each circle can be so named "Chinese compatriots team" fellow American team "...... as long as two to each other about their captain is not the same person, you can determine the relationship between friend and foe.

But there is a problem, ah, heroes who only know their own immediate friends is that many people simply do not know the captain to determine who their leader is and can only aimlessly through a friend of a friend relationship going to ask: "Are you not the captain? you are not the captain? "so, want a fight must first ask a few decades, hungry have starved to death, I can not stand. As a result, the captain of the face is also embarrassing, not only inefficient, but also may fall into an infinite loop. Then the captain ordered regroup. Everyone within the team to implement hierarchical system, form a tree structure, I was captain of the root node, the following are the two players, three players. Just remember that everyone will come to their superiors is the line. Judging friends and foes encountered, as long as the layers asked up until the highest level, in a short time can determine who is the captain. Since we only care about whether it is between two people of a gang, as to how they are related by friendship, and the internal structure of each circle is how even the captain who, not important. So we can indulge freely again captain the team, as long as the relationship between friend and foe is not wrong enough. Thus, martial produced.

Here we look at and check the realization sets. int pre [1000]; this array, each of who recorded the heroes superior Yes. Heroes from 1 or 0 start number (according to the meaning of the questions may be), pre [15] = 3 says that 15 heroes superiors are 3 heroes. If a man is his own superiors, it shows that he is the head, and to find so far. There is also a separate faction loner, such as Ouyang Feng, he is his own superiors. Everyone recognized only his superiors. For example, Hu Qingniu students know that they will report to the Yang left. Who Zhang Wuji that? do not know! To know who their head is only one level check up. 
findr This function is used to find the head, meaning clear enough

 

int findr ( int root) // Find supremo 
{
     int Son, tmp; 
    Son = root;
     the while (! root = pre [root]) // my superiors is not the head of 
        root = pre [root];
     the while (Son! root =) // I will find his superiors, until the head appears 
    { 
        tmp = pre [Son]; 
        pre [Son] = root; 
        Son = tmp; 
    } 
    return root; // head to drive ~~ 
}

 

Let's look unite function, that is not even a line between two points, so that all points of the previous two plates on which they can exchange up. It's good to do on the map, draw a line on the line. But now we are using disjoint-set to describe the situation in the martial arts, a total of only a pre [] array, how to achieve it? For example, lakes or, in martial arts Suppose now the situation illustrated in FIG. Xu Zhu handsome pot and Zhou Zhiruo MM is my favorite two characters, their ultimate boss are Xuanci abbot and nun killer, and that obviously the two camps. I do not want them to fight each other, on the right They said: "The two of you hook Lara, good friends." They look on my face, I agreed. This consent may no trivial matter, the entire Shaolin and Emei who can not fight. Such a major change in how it can be to change how many places? In fact, very simple, I said to Xuanci abbot:. "Master, could you put your superiors changed extinction Shitai it this way, the two factions of the original ultimate boss of all personnel are Shitai, that also hit the ball !. ah does not matter anyway, we only care about the structure of connectivity, internal martial art "Xuanci one certainly well done:" I rely on, I became her hands what is it, how not to turn my protest "?! Thus, the two meet a war, is incredibly hard to kill, the situation whom color ah, but ah, eventually there will be the outcome of this war, winner takes all. Weak was annexed. Anyway, who joined who effect is the same, a martial art to become a two. This function is meant to understand it?

 

void umite ( int the root1, int root2) // Xu Zhu and Zhou Zhiruo friends 
{
     int X, Y; 
    X = unionsearch (the root1); // my boss is Xuanci 
    Y = unionsearch (root2); // my boss extinct 
    iF (= the X-! the y-) 
        pre [the X-] = the y-; // a fight, who would win when the other party boss, who won here are the same, pre [y] = x is also OK 
}

 

As a result, if the relationship feels too long, the efficiency will be greatly reduced

 

 Imagine this scenario: Two strangers meet heroes, and want to know can not do a. Then quickly called to ask their superiors: "You are not the head?" Superior, he said:. "I'm not like that, so and so who are my superiors, you ask him to look at" all the way down to ask, had two final boss They are Dongchang Caogong Gong. "Oops, it turned out to be one of us, polite and courteous, the next six groups of three battalions flour gourd doll!" "Nice to meet you, the next set of nine battalions eighteen fairy dog ​​tail flower!" They happily drink to go hand in hand a. "Etc., etc., two heroes Please stay, there are things not finished yet!" They stopped me. "Oh, yes, we need to do path compression." They wake up. Flour gourd doll called his superiors six Leader: "Leader ah, I checked, in fact, the head of the crash Caogong Gong is not as good as we crash together in Caogong Gong sworn men, to save level is too low, the future. Find head trouble. "" Well, it makes sense. "flour gourd doll then phoned just visited three battalion commander ...... fairy tail flower dog did the same thing. In this way, all the characters involved in the query are gathered under the direct leadership of Caogong Gong. Every query processing is optimized, so the entire tree of martial layers will be maintained at a relatively low level. Path compression codes, can read well, can not read their own analog look, a very simple recursive only. In short it implements function is such a meaning.

As a result, it is not able to more quickly find their head of it

Of course, the code can also be simpler

Head findr query function of x

 

int findr ( int X) // find head 
{
     IF (PAR [X] == X) // find, head emergence 
        return X;
     the else 
        return PAR [X] = findr (PAR [X]); // my head is not a superior 
}

 

unite function have to be changed to change

 

void unite(int x,int y)//x和y想打架
{
    x=findr(x);//我老大是x
    y=findr(y);//我老大是y
    if(x==y)//我们老大相同,不打了
        return;
    par[y]=x;//老大不同,打一架,y或x谁当老大都行,反正最后是合并 y并入x或x并入y都行
    sum[x]+=sum[y];//y并入x,则x的小兵个数等于x的小兵个数+y的小兵个数
}

 

OK 你已经学会如何处理帮派之间的纷争了(并查集)  恭喜出师 

可以试试这题https://www.cnblogs.com/sky-stars/p/11222823.html

点个关注呗

 

Guess you like

Origin www.cnblogs.com/sky-stars/p/11222999.html