NetEase Development Engineer Programming problem comparison weight Java

Source: Internet
Author: User

Compare weight

Xiao Ming accompanied Xiao Red to see the diamonds, they from a bunch of diamonds randomly extracted two and compare their weight. The weight of these diamonds varies. After they had been comparing for some time, they had a fancy for two diamonds G1 and G2. Now, please judge which of the two diamonds is heavier according to the information you have previously compared.

Given the number of two diamonds g1,g2, the number starts at 1, given the relationship array vector, where the elements are some binary groups, the first element is the number of the heavier diamond in a comparison, and the second element is the number of the lighter diamond. The last number of comparisons before given N. Please return the relationship between the two diamonds, if G1 more return 1,g2 more return-1, can not be judged to return 0. The input data is guaranteed to be legal and there will be no contradiction.

Test examples:
2,3,[[1,2],[2,4],[1,3],[4,3]],4

Returns: 1

Solution
1 ImportJava.util.*;2 3  Public classCMP {4      Public intcmpintG1,intG2,int[] Records,intN) {5Map<integer,list<integer>> map=NewHashmap<integer,list<integer>>();6         intMax=0;7          for(int[] pair:records) {8             if(!map.containskey (pair[0])){9List<integer> list=NewArraylist<integer>();TenList.add (pair[1]); OneMap.put (pair[0],list); A             } -             ElseMap.get (Pair[0]). Add (pair[1]); -             intTemp= (pair[0]>pair[1])? Pair[0]:p air[1]; themax= (max>temp)?max:temp; -         } -         Boolean[] isvisited1=New Boolean[++Max]; -         Boolean[] isvisited2=New Boolean[Max]; +         if(Isreachable (g1,g2,map,isvisited1))return1; -         Else if(Isreachable (G2,G1,MAP,ISVISITED2))return-1; +         Else return0; A          at     } -     Private BooleanIsreachable (intNowintTarget,map<integer,list<integer>> Map,Boolean[] isvisited) { -         if(Now==target)return true; -isvisited[now]=true; -         if(Map.get (now) = =NULL)return false; -         intSize=Map.get (now). Size (); in          for(inti=0;i<size;i++){ -             intnext=Map.get (now). get (i); to             if(Isvisited[next])Continue; +             Else if(Isreachable (next,target,map,isvisited))return true; -         } the         return false; *     } $}

NetEase Development Engineer Programming problem comparison weight Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.