void AddEdge(int ex) { int i, j; E[ex].selected = 1; i = FindRoot(E[ex].Vx); j = FindRoot(E[ex].Vy); Vertex[i] = j; //合成一集合 }
void UnionFind::EnumerateSet(int i,std::vector<int>& s) const { int root = FindRoot(i); s.resize(0); for(size_t i=0;i<parents.size();i++) if(root == FindRoot(i)) s.push_back(i); }
vector<Edge*> Kruskal(int numVertices, vector<Edge> &edges) { auto minSpanForest = vector<Edge*>(); for( int i=0; i<numVertices; i++) { Parents.push_back(i); } sort(edges.begin(), edges.end()); for( auto pE = edges.begin(); pE != edges.end(); pE++ ) { auto rootS = FindRoot(pE->Start); auto rootE = FindRoot(pE->End); if( rootE == rootS ) { continue; } if( rootS == pE->Start ) { SetRoot(pE->Start, rootE); } else { SetRoot(pE->End, rootS); } minSpanForest.push_back(&(*pE)); } return minSpanForest; }
double FindRoot(Ffuncclbc *f, double x1, double x2, double prec) { double x = -f(x1)*(x2-x1) / (f(x2)-f(x1)) + x1; if (Abs(f(x))<prec) return x; else if (Sgn(x)==Sgn(x1)) return FindRoot(f, x, x2, prec); else return FindRoot(f, x, x1, prec); return 0; }
char Cycle(int ex) { int i, j; i = FindRoot(E[ex].Vx); j = FindRoot(E[ex].Vy); if (i == j) { // 屬於同一集合 return (1); } return (0); }
// 将集合root2合并到集合root1中 void Union(int x1, int x2) { int root1 = FindRoot(x1); int root2 = FindRoot(x2); if (root1 != root2) { _parent[root1] += _parent[root2]; _parent[root2] = root1; } }
void RadialTreeLayout::call(GraphAttributes &AG) { const Graph &tree = AG.constGraph(); if(tree.numberOfNodes() == 0) return; if (!isArborescence(tree)) OGDF_THROW_PARAM(PreconditionViolatedException, pvcForest); OGDF_ASSERT(m_levelDistance > 0); // determine root of tree (m_root) FindRoot(tree); // compute m_level[v], m_parent[v], m_leaves[v], m_numLevels ComputeLevels(tree); // computes diameter of each node ComputeDiameters(AG); // computes m_angle[v] and m_wedge[v] ComputeAngles(tree); // computes final coordinates of nodes ComputeCoordinates(AG); }
void CRocket::OnPrepare() { // perform collision detection from this entity with all other objects in world if (!isExplosion) ProcessCollisions(FindRoot()); if (isExplosion) { // Phase 16 - Take out for now // Phase 19 - Uncomment the following if (explosion->IsDead()) { explosion->KillSystem(); delete explosion; explosion = NULL; isExplosion = false; isDead = true; ///// -- Adam ///// -- Adam } } }
void CEntity::OnPrepare() { /* Frame# Action ---------------- 0-39 idle 40-46 running 47-60 getting shot but not falling (back bending) 61-66 getting shot in shoulder 67-73 jumping 74-95 idle 96-112 getting shot and falling down 113-122 idle 123-135 idle 136-154 crouch 155-161 crouch crawl 162-169 crouch adjust weapon (idle) 170-177 kneeling dying 178-185 falling back dying 186-190 falling forward dying 191-198 falling back slow dying */ switch (modelState) { case MODEL_IDLE: stateStart = 0; stateEnd = 39; break; case MODEL_CROUCH: break; /****************Faith Satterthwaite 12/1/2012***********************/ case MODEL_WALK: stateStart = 42; stateEnd = 44; velocity = CVector(-15.0, -15.0, -15.0); break; /*******************************************************************/ case MODEL_RUN: stateStart = 40; stateEnd = 46; velocity = CVector(0.0, 0.0, 15.0); break; case MODEL_JUMP: stateStart = 67; stateEnd = 73; break; case MODEL_DIE: stateStart = 178; stateEnd = 184; break; default: stateStart = 0; stateEnd = 1; break; } // perform collision detection from this entity with all other objects in world ProcessCollisions(FindRoot()); }
template<> void PAlgebraModTmpl<zz_pX,vec_zz_pX,zz_pXModulus>::mapToFt(zz_pX& r, const zz_pX& G,unsigned t,const zz_pX* rF1) const { int i = zmStar.indexOfRep(t); if (i < 0) { r=zz_pX::zero(); return; } if (rF1==NULL) { // Compute the representation "from scratch" zz_pE::init(factors[i]); // work with the extension field GF_2[X]/Ft(X) zz_pEX Ga=to_zz_pEX((zz_pX&)G);// G is polynomial over the extension field r=rep(FindRoot(Ga)); // Find a root of G in this field return; } // if rF1 is set, then use it instead, setting r = rF1(X^t) mod Ft(X) zz_pXModulus Ft(factors[i]); // long tInv = InvMod(t,m); zz_pX X2t = PowerXMod(t,Ft); // X2t = X^t mod Ft r = CompMod(*rF1,X2t,Ft); // r = F1(X2t) mod Ft /* Debugging sanity-check: G(r)=0 in the extension field (Z/2Z)[X]/Ft(X) zz_pE::init(factors[i]); zz_pEX Ga=to_zz_pEX((zz_pX&)G);// G as a polynomial over the extension field zz_pE ra =to_zz_pE(r); // r is an element in the extension field eval(ra,Ga,ra); // ra = Ga(ra) if (!IsZero(ra)) {// check that Ga(r)=0 in this extension field cout << "rF1(X^t) mod Ft(X) != root of G mod Ft, t=" << t << endl; exit(0); }*******************************************************************/ }
//unions the sets to which i and j belong int UnionFind::Union(const int i,const int j) { int root_i=FindSet(i),root_j=FindRoot(j); PathCompress(j,root_i); if(root_i!=root_j) parents[root_j]=root_i; return root_i; }
divisor& divisor::random(divdeg_t dgr){ // Underlying curve is not touched assert(s_hcurve.is_valid_curve()); // A random valid divisor is generated by the following algorithm: // generate a degree 1 divisor [x - a1, b1] by choosing a1 by random // then trying to solve quadratic equation // x^2 + h(a1)*x - f(a1) for b1. // Note that finding a root of an equation by calling routine // FindRoot(root, poly) may go into an infinite loop if poly does // not split completely. We avoid this by calling irreducibility // test routine DetIrredTest(poly). After a degree 1 divisor is // found, this divisor is doubled by calling add_cantor() to return // a degree 2 polynomial. field_t a1, b1, f_of_a1, h_of_a1; poly_t poly; // polynomial x^2 + h(a1)*x - f(a1) SetCoeff(poly, 2); // set degree 2 leading term to 1 do { do{ NTL_NNS random(a1); eval(f_of_a1, s_hcurve.get_f(), a1); eval(h_of_a1, s_hcurve.get_h(), a1); SetCoeff(poly, 1, h_of_a1); SetCoeff(poly, 0, - f_of_a1); } while ( DetIrredTest(poly) ); FindRoot(b1, poly); // Set upoly = x - a1 SetX(upoly); SetCoeff(upoly, 0, -a1); // Set vpoly = b1 vpoly = b1; update(); } while (*this == -*this); // Avoid getting unit after doubling // return a degree one divisor if dgr = 1 if (dgr == DEGREE_1) return *this; // Double the degree 1 divisor to get a degree 2 divisor, otherwise add_cantor(*this, *this, *this); if (is_valid_divisor()) return *this; cerr << "Random divisor failed to generate" << endl; abort(); }
void Zig(Vertex<T>* d) { Vertex<T>* b(d->father); if (b->right_son == d) { RightRotate(d); } else if (b->left_son == d) { LeftRotate(d); } root = FindRoot(root); }
DisjointNode *Union(DisjointNode *x, DisjointNode *y) { DisjointNode * xRoot = FindRoot(x); DisjointNode * yRoot = FindRoot(y); if(xRoot == yRoot) return xRoot; if(xRoot->rank < yRoot->rank) xRoot->parent = yRoot; else if(xRoot->rank > yRoot->rank) yRoot->parent = xRoot; else { yRoot->parent = xRoot; xRoot->rank = xRoot->rank + 1; } }
void UnionFind::EnumerateSets(std::vector<std::vector<int> >& sets) const { std::vector<int> roots; GetRoots(roots); std::map<int,size_t> rootMap; for(size_t i=0;i<roots.size();i++) rootMap[roots[i]] = i; sets.resize(roots.size()); for(size_t i=0;i<parents.size();i++) sets[rootMap[FindRoot((int)i)]].push_back((int)i); }
// // 小米的2016的面试题==具体描述参见课件 // int Friends(int n, int m, int r[][2]) { assert(r); // 初始创建一个并查集 const int N = n+1; int* unionSet = new int[N]; memset(unionSet, -1, N*sizeof(int)); for (int i = 0; i < m; ++i) { int first = r[i][0]; int second = r[i][1]; int parent1 = FindRoot(unionSet, first); int parent2 = FindRoot(unionSet, second); if (parent1 != parent2) { UnionSet(unionSet, parent1, parent2); } } int count = 0; for (int i = 1; i < N; ++i) { if (unionSet[i] < 0) { ++count; } } delete[] unionSet; return count; }
void BessRoots() { char filename[256]; int idx, root_idx; FILE *fil_ptr = 0; double curr_root, curr_x; printf("\nComputation of Roots for Bessel functions\n\nOut File (0 for screen):"); scanf("%s", filename); if(strcmp(filename, "0")) { if((fil_ptr = fopen(filename, "w")) == 0) { printf("\nCannot open file %s\n", filename); return; } } for(idx = 0; idx <= MAX_BESSEL_IDX; idx++) { curr_x = 0; for (root_idx = 0; root_idx < NUM_ROOTS; root_idx++) { if(idx > 0 && root_idx == 0) curr_root = 0; else curr_root = FindRoot(idx, &curr_x); if(fil_ptr) { fprintf(fil_ptr, "%4.8f", curr_root); if(root_idx < NUM_ROOTS - 1) fprintf(fil_ptr, ", "); else fprintf(fil_ptr, "\n"); } else { printf("%4.8f", curr_root); if(root_idx < NUM_ROOTS - 1) printf(", "); else printf("\n"); } } } }
void get_modulus(zz_pX& pi_1, zz_pX& pi_2, zz_pX& a, int p, int d1, int d2) { get_modulus(pi_1, p, d1); get_modulus(pi_2, p, d1*d2); // find alpha zz_pE::init(pi_2); zz_pEX pi; conv(pi, pi_1); zz_pE zero; FindRoot(zero, pi); a = rep(zero); }
int main(int argc, char** argv) { if(argc < 3) { printf("Not enough arguments.\n"); return EXIT_FAILURE; } int numrecs; int length; Node* tree = LoadFile(argv[1], &length, &numrecs); if(tree == NULL) { return EXIT_FAILURE; } clock_t pack_start = clock(); int root = FindRoot(tree, length); Pack(tree, root); clock_t pack_end = clock(); double packtime = (double) (pack_end - pack_start) / CLOCKS_PER_SEC; printf("Width: %le\n", GETN(tree, root).width); printf("Height: %le\n\n", GETN(tree, root).height); printf("X-coordinate: %le\n", GETN(tree, numrecs).xcoord); printf("Y-coordinate: %le\n\n", GETN(tree, numrecs).ycoord); printf("Elapsed time: %le\n", packtime); printf("\n"); Box bests = { GETN(tree, root).width, GETN(tree, root).height }; clock_t reroot_start = clock(); // void ReRoot(Node* tree, int root, Box* min, int nogo_root) ReRoot(tree, root, &bests, GETN(tree, root).right); // deal with root's left child ReRoot(tree, root, &bests, GETN(tree, root).left); // deal with root's right child clock_t reroot_end = clock(); double reroottime = (double) (reroot_end - reroot_start) / CLOCKS_PER_SEC; // Re Rooting printf("Best width: %le\n", bests.width); printf("Best height: %le\n\n", bests.height); printf("Elapsed time for re-rooting: %le\n", reroottime); int error_code = SaveFile(argv[2], tree, numrecs); return error_code; }
int Roots(Ffuncclbc *f, int max_roots, double res, double prec, double x_min, double x_max, double roots[]) { int fnd = 0; // finded roots for (double x = x_min; x<=x_max-res; x += res) { int sgn1 = Sgn(f(x)); int sgn2 = Sgn(f(x+res)); if (sgn1 == 0) continue; // this root has already been calculated if (sgn1 != sgn2) { if (sgn2 == 0) roots[fnd++] = x+res; else roots[fnd++] = FindRoot(f, x, x+res, prec); if (fnd == max_roots) goto end; } } end: roots[fnd] = 0; return fnd; }
//------------------------------------------------------------------------------ Real EstimationRootFinder::Locate(ObjectArray &whichOnes) { #ifdef DEBUG_ROOT_SEARCH MessageInterface::ShowMessage("EstimationRootFinder::Locate called with %d " "events\n", whichOnes.size()); #endif Real rootEpoch = -1.0; events = (std::vector<Event*>*)(&whichOnes); for (UnsignedInt i = 0; i < whichOnes.size(); ++i) { Real foundEpoch = FindRoot(i); if (foundEpoch > 0.0) { rootEpoch = (rootEpoch == -1.0 ? foundEpoch : (rootEpoch > foundEpoch ? foundEpoch : rootEpoch)); } } return rootEpoch; }
int main(int argc, const char** argv) { InitStatics(); Context context; Res res; res = context.ProcessArgs(argc, argv); ExitIfError(res, context); string absolute_root; string canonical_currdir; res = FindRoot(&absolute_root, &canonical_currdir); ExitIfError(res, context); context.LogVerbose(StringPrintf("absolute_root = %s\n", absolute_root.c_str())); context.LogVerbose(StringPrintf("canonical_currdir = %s\n", canonical_currdir.c_str())); res = context.Init(absolute_root, canonical_currdir); ExitIfError(res, context); assert(context.GetConfig()); context.Log(StringPrintf("dmb config: %s\n", context.GetConfig()->name().c_str())); res = context.Resolve(); ExitIfError(res, context); res = context.ProcessTargets(); ExitIfError(res, context); context.Log("dmb OK\n"); return 0; }
void GameObject::OnPrepare() { ProcessCollision(FindRoot()); }
//enumerates the sets to which the items belong void UnionFind::EnumerateSets(std::vector<int>& sets) { CompressAll(); sets.resize(parents.size()); for(int i=0;i<(int)parents.size();i++) sets[i]=FindRoot(i); }
//since on a merge, we don't go through all the children of a set //to change their roots, we have to do some path compression void UnionFind::CompressAll() { for(int i=0;i<(int)parents.size();i++) PathCompress(i,FindRoot(i)); }
UInt32 CDataNode::GetNrOfTreeNodes( void ) const {GUCEF_TRACE; return FindRoot()->GetNrOfChildNodes()+1; }
int UnionFind::FindSet(const int i) { int root=FindRoot(i); PathCompress(i,root); return root; }
DisjointNode *FindRoot(DisjointNode *curNode) { if(curNode->parent != curNode) curNode->parent = FindRoot(curNode->parent); return curNode->parent; }
int main(int argc, char *argv[]){ int p_order = 2; int n_threads = 0; TPZGeoMesh * gmesh = ReadGeometry(); #ifdef PZDEBUG PrintGeometry(gmesh); #endif TPZCompMesh *cmesh = CMeshFooting2D(gmesh, p_order); TPZAnalysis *analysis = Analysis(cmesh,n_threads); #ifdef USING_BOOST boost::posix_time::ptime post_proc_t1 = boost::posix_time::microsec_clock::local_time(); #endif /// Creation of a post-process analysis TPZPostProcAnalysis * post_processor = new TPZPostProcAnalysis; post_processor->SetCompMesh(cmesh); int n_regions = 1; TPZManVector<int,10> post_mat_id(n_regions); post_mat_id[0] = ERock; TPZStack<std::string> scalar_names,vector_names, tensor_names; vector_names.Push("Displacement"); tensor_names.Push("Strain"); tensor_names.Push("StrainPlastic"); tensor_names.Push("Stress"); TPZStack<std::string> var_names; for (auto i : scalar_names) { var_names.Push(i); } for (auto i : vector_names) { var_names.Push(i); } for (auto i : tensor_names) { var_names.Push(i); } post_processor->SetPostProcessVariables(post_mat_id, var_names); TPZFStructMatrix structmatrix(post_processor->Mesh()); structmatrix.SetNumThreads(n_threads); post_processor->SetStructuralMatrix(structmatrix); #ifdef USING_BOOST boost::posix_time::ptime post_proc_t2 = boost::posix_time::microsec_clock::local_time(); #endif #ifdef USING_BOOST REAL case_solving_time = boost::numeric_cast<double>((post_proc_t2-post_proc_t1).total_milliseconds()); std::cout << "Created post-processing in :" << setw(10) << case_solving_time/1000.0 << setw(5) << " seconds." << std::endl; std::cout << std::endl; #endif #ifdef USING_BOOST boost::posix_time::ptime case_t1 = boost::posix_time::microsec_clock::local_time(); #endif std::string plotfile = "footing_elast.vtk"; // For a single step REAL dt = 0.1; int n_steps = 10; for (int it = 1; it <= n_steps; it++) { REAL t = it*dt; LoadingRamp(t,cmesh); FindRoot(analysis); AcceptSolution(cmesh, analysis); #ifdef USING_BOOST boost::posix_time::ptime post_l2_projection_proc_t1 = boost::posix_time::microsec_clock::local_time(); #endif post_processor->TransferSolution(); #ifdef USING_BOOST boost::posix_time::ptime post_l2_projection_proc_t2 = boost::posix_time::microsec_clock::local_time(); REAL l2_projection_time = boost::numeric_cast<double>((post_l2_projection_proc_t2-post_l2_projection_proc_t1).total_milliseconds()); std::cout << "L2 projection post-processing in :" << setw(10) << l2_projection_time/1000.0 << setw(5) << " seconds." << std::endl; std::cout << std::endl; #endif #ifdef USING_BOOST boost::posix_time::ptime post_vtk_proc_t1 = boost::posix_time::microsec_clock::local_time(); #endif PostProcess(post_processor,scalar_names,vector_names,tensor_names,plotfile); #ifdef USING_BOOST boost::posix_time::ptime post_vtk_proc_t2 = boost::posix_time::microsec_clock::local_time(); REAL vtk_drawing_time = boost::numeric_cast<double>((post_vtk_proc_t2-post_vtk_proc_t1).total_milliseconds()); std::cout << "Drawing vtk file in :" << setw(10) << vtk_drawing_time/1000.0 << setw(5) << " seconds." << std::endl; std::cout << std::endl; #endif } #ifdef USING_BOOST boost::posix_time::ptime case_t2 = boost::posix_time::microsec_clock::local_time(); REAL case_time = boost::numeric_cast<double>((case_t2-case_t1).total_milliseconds()); std::cout << "Execution complete in :" << setw(10) << case_time/1000.0 << setw(5) << " seconds." << std::endl; std::cout << std::endl; #endif std::cout << "Execution complete." << std::endl; return 0; }