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
The first time that you run, the display uses more memory than the limit. The reason for this is that there is no flag set in the check function, so that the number of previous push-to-queue re-push has wasted memory (although there will not be a dead loop because the small number can no longer be larger than the large number, but the overhead is greatly increased).
#include <unordered_map>classCMP { Public:
Determine if G2 is behind G1 based on the adjacency arrayBOOLCheckintG1,intG2, unordered_map<int, vector<int>>tu) {cout<<"**********************************"<<Endl; if(Tu[g1].empty ())return false; Queue<int>inch; Map<int,BOOL>Monitor; inch. push (G1); while(!inch. Empty ()) { intQ =inch. Front (); inch. Pop (); MONITOR[Q]=true; cout<<"Q is:"<< Q <<Endl; if(q = = g2)return true; for(inti =0; I < tu[q].size (); i++){ if(Tu[q][i] = = G2)return true; if(!Monitor[tu[q][i]])inch. Push (Tu[q][i]); } } return false;}
CMP function, which represents the records with an adjacency arrayintcmpintG1,intG2, vector<vector<int> > Records,intN) {//Write code hereunordered_map<int, vector<int>>Tu; for(inti =0; I < records.size (); i++) {tu[records[i][0]].push_back (records[i][1]); } if(Check (G1, G2, tu))return 1; Else if(Check (G2, G1, tu))return-1; Else return 0;}};
NetEase programming Problem: Algorithm problem of graph represented by adjacency array