In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to achieve and search the collection of Java". In the daily operation, I believe that many people have doubts about how to achieve and check the collection of Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to achieve and check the collection of Java"! Next, please follow the editor to study!
Title
Topic background
If a family member is too large, it is really not easy to judge whether two people are relatives. Now give a kinship chart and ask whether the two people given at will are related.
Train of thought
For this question, what is examined is to check and collect, that is, the idea that the little monster finds the superior leader one by one to guide the final Boss to stop. If the two monsters want to fight, they need to ask their superior leader, the leader asks the leader again, step by step up, and finally find that they belong to the same Boss deployment, then they can no longer fight. If Bai Chenxiang in Doluo mainland did not know that Tang San was related at the beginning, they would first ask their grandparents, and Bai Chenxiang learned through her grandfather that she had relatives with Tang San, then they would not fight again, but would form an alliance to fight other enemies. Similarly, my relative is you, your relative is her, then she and I will be relatives, just like your cousin in my hometown is your relative. Your cousin is related to his grandfather, so you are related to his grandfather.
Find implementation
This find is used to find their superiors. During initialization, the little monster was overjoyed and thought that he was his boss. I was Wang and I was Boss. There was nothing wrong with this. After all, he was at the bottom of the well, so he used an array of pre [] to store their superior level, for example: pre [10] = 6. Then I think that the superior of 10 is the superior of 6, and the superior of 6 is assumed that the superior of 4, 4, joins himself, that is, pre [4] = 4. At this time, the monster of 4 understood that his superior was himself, and he was the king. Then he found boss. Recall what I just said. If the two monsters want to fight, they need to ask their superiors first. If the superiors ask the superiors again until they ask them that they belong to the same boss, then they will smile and say, "originally we belong to the same leader." So find is used to find the ultimate leader of the monster of the final two teams.
Public static int find (int x) {if (prex] = = x) return x leadership / if the superior leader is himself, return his own return pre [x] = find (prex]); / / if not, visit the superior one by one until boss is found, which is equivalent to finally finding boss and assigning boss to the implementation of prex} join
What is join used for? It is used to merge, and there are two kings who occupy the mountain as kings, their forces are equal, and their strength is equal. One day when something happens, King 1 wants to merge with King 2. The second King is thinking about who will be king after the merger. Can not let it become a king, King 2 then said that after the merger, I will be king, the king will obviously disagree, the second king said you came to me to merge, do not agree to agree, so the second king became a king, then the second king took control of all the troops, including the first king, then join is used to choose one of the two kings as the general king.
Public static void join (int xjinint y) {int xx=find (x); / find the first king int yy=find (y) with find; / / find the second king if (xxquaryy) {/ / if it is not equal to pre [xx] = yy;//, one of the kings will command all the armies of the first king} overall code import java.util.Scanner Public class test1 {static int [] pre=new int [10000]; / / pay attention to the scope of the title public static void main (String [] args) {int nremeng mLINGY; Scanner sc=new Scanner (System.in); n=sc.nextInt (); / / n personal m=sc.nextInt (); / / m pair relation p=sc.nextInt (); / / p query relation for (int I = 0; I < n) For +) {pre [I] = iPendacer / initialization, my ancestor is my own} for (int I = 0; I < m; iSue +) {x=sc.nextInt (); y = sc.nextInt (); join (xMagery); / / merge} for (int I = 0; I)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.