void merge(int a, int b){ int fa=getFather(a); int fb=getFather(b); if(fa==fb) return; classof[fb]=fa; }
void ada_to_asa(char *array, int productions[]){ int next = 0, i, j = 0, max = 0; for(i=0; productions[j] != -1 ; i++){ switch(array[next]){ case 'E': //The leftmost node will receive production applyProduction(array,productions[j++],next); if(array[2*next+1] == 'E'){ next = 2*next+1; }else if(array[2*next+2] == 'E'){ next = 2*next+2; }else{ next = getFather(next); } break; case 'A': applyProduction(array,productions[j++],next); if(array[2*next+1] == 'E'){ next = 2*next+1; }else if(array[2*next+2] == 'E'){ next = 2*next+2; }else{ next = getFather(next); } break; default: next = getFather(next); } if(next > max){ max = next; // Catch last node position in array } } array[max + 1] = '\0'; }
void unions(int a, int b)//并 { a = getFather(a); b = getFather(b); if(a != b) father[a] = b; }
int main(int argc, char* argv[]) { int N, M; scanf("%d%d", &N, &M); int i, j, k; int _x, _y, _w; for (i=0; i<M; i++) { scanf("%d%d%d", &_x, &_y, &_w); x[i] = _x; y[i] = _y; w[i] = _w; } qsort(0, M-1); for (i=0; i<N; i++) { fa[i] = i; } int p = 0; int result = 0; for (k=1; k<N; k++) { while (p < M && getFather(x[p]) == getFather(y[p])) p++; result += w[p]; fa[getFather(x[p])] = getFather(y[p]); } printf("%d\n", result); return 0; }
int main (int argc, const char * argv[]) { int i; int n,m; int q; while(scanf("%d %d",&n,&m)!=EOF) { int num1,num2; //memset(father,0,sizeof(father)); for (i=1; i<=n; i++) { father[i]=i; } for (i=0; i<m; i++) { scanf("%d %d",&num1,&num2); father[getFather(num1)]=father[getFather(num2)]; } scanf("%d",&q); for (i=0;i<q ;i++ ) { scanf("%d %d",&num1,&num2); if (getFather(num1)==getFather(num2)) printf("YES\n"); else printf("NO\n"); } } return 0; }
//Merging and judging if the 2 points are linked int merge(int x , int y) { int tempX = getFather(x); int tempY = getFather(y); if (tempX != tempY) { bcj[tempY] = tempX; getFather(y); return 1; } else return 0; }
bool validTree(int n, vector<pair<int, int>>& edges) { if (edges.size() != n-1) return false; father = new int [n]; rank = new int [n]; memset(rank, 0, n * sizeof(int)); for (int i = 0; i < n; i++) father[i] = i; for (auto e = edges.begin(); e != edges.end(); e++) { int fu = getFather(e->first); int fv = getFather(e->second); if (fu == fv) return false; unionIt(fu, fv); } return true; }
bool unionIt(int a, int b) { int fa = getFather(a); int fb = getFather(b); if (fa == fb) return false; if (rank[fa] < rank[fb]) father[fa] = fb; else if (rank[fb] < rank[fa]) father[fb] = fa; else { father[fb] = fa; rank[fa]++; } return true; }
int getFather(int value) { if (value == bcj[value]) return value; else { bcj[value] = getFather(bcj[value]); return bcj[value]; } }
int getFather(int num) { if (father[num]==num) return num; father[num]=getFather(father[num]); return father[num]; }
int getFather(int k) { if (fa[k] == k) { return k; } else { int _fa = getFather(fa[k]); fa[k] = _fa; return _fa; } }
void StateList::printPath(IfConfig* s) { StateNode node; bool found; do { found = getFather(s, node); if (found) { s = node.state; } } while ((node.father != -1) && (found)); }
bool Module::isExtension(const ObjectType& child, const ObjectType& parent) const { if(child.extendsDirectly(parent)) return true; ObjectType father = getFather(child); if(!father.isNull()) return isExtension(father, parent); return false; }
int64_t Module::getFixedSize(const ObjectType &type) const { const Module* module = handler(type); if(module == nullptr) return HM_UNKNOWN_SIZE; int64_t size = module->doGetFixedSize(type, *this); if(size == HM_PARENT_SIZE) return getFixedSize(getFather(type)); return size; }
/* * Textual contents setting */ __INLINE void ElementRef::setText (const char* text, DomTextSize textSize) { AssertBug(isText() || isPI() || isComment(), "Not a text element ! id=%llx, path=%s\n", getElementId(), generateVersatileXPath().c_str()); ElementSegment* me = getMe<Write>(); AssertBug(me->flags & ElementFlag_HasTextualContents, "Invalid flags %x\n", me->flags); if (me->textualContents.size > me->textualContents.shortFormatSize) { // We shall reuse a bit here... Nevermind... getDocumentAllocator().freeSegment(me->textualContents.contentsPtr, me->textualContents.size); } if (textSize <= me->textualContents.shortFormatSize) { getDocumentAllocator().alter(me); me->textualContents.size = textSize; memcpy(me->textualContents.contents, text, textSize); getDocumentAllocator().protect(me); } else { SegmentPtr contentsPtr = getDocumentAllocator().getFreeSegmentPtr(textSize, getAllocationProfile()); char* textSegment = getDocumentAllocator().getSegment<char, Write>(contentsPtr, textSize); getDocumentAllocator().alter(textSegment, textSize); memcpy(textSegment, text, textSize); getDocumentAllocator().protect(textSegment, textSize); getDocumentAllocator().alter(me); me->textualContents.size = textSize; me->textualContents.contentsPtr = contentsPtr; getDocumentAllocator().protect(me); } if ( getFather() ) { getDocument().appendJournal(*this, JournalOperation_UpdateTextNode, *this, 0); } }
vector<IfLabel *> StateList::getPath(StateNode state_node) { StateNode nod, node; bool found; vector<IfLabel *> lst; int maxDepth = 0; if (state_node.father == -1){ return lst; }else { nod = state_node; do { found = getFather(nod, node); if (found) { //print_aldebaran_suite(n.father, n.label_id, n.pos); if (nod.label != NULL) lst.insert(lst.begin(), nod.label); //testCase.insert(testCase.end(), n.testCase.begin(), n.testCase.end()); nod = node; } } while ((node.father != -1) && (found)); } return lst; }
int main() { FILE *fp = fopen("input.txt", "r"); FILE *fp2 = fopen("output.txt", "w"); int n, m; memset(father, 0, sizeof(father)); fscanf(fp, "%d%d", &n, &m); int a, b; bool flag = true; while(m--) { fscanf(fp, "%d%d", &a, &b); unions(a, b + n);//a与b标记为不同集合 unions(b, a + n); if(getFather(a) == getFather(a+n) || getFather(b) == getFather(b+n)) { flag = false;//出现矛盾情况 } } if(flag) { fprintf(fp2, "Yes\n"); int temp = getFather(1); for(int i = 1; i <= n; i++) if(getFather(i) == temp) fprintf(fp2, "1"); else fprintf(fp2, "0"); printf("\n"); } else fprintf(fp2, "No\n"); pclose(fp); pclose(fp2); return 0; }
int isSame(int a, int b){ return getFather(a)==getFather(b); }
bool gmshLocalNetworkClient::receiveMessage(gmshLocalNetworkClient *master) { // receive a message on the associated GmshServer; 'master' is only used when // creating subclients with GMSH_CONNECT. double timer = GetTimeInSeconds(); if(!getGmshServer()){ Msg::Error("Abnormal server termination (no valid server)"); return false; } int type, length, swap; if(!getGmshServer()->ReceiveHeader(&type, &length, &swap)){ Msg::Error("Abnormal server termination (did not receive message header)"); return false; } std::string message(length, ' '), blank = message; if(!getGmshServer()->ReceiveMessage(length, &message[0])){ Msg::Error("Abnormal server termination (did not receive message body)"); return false; } if(message == blank && !(type == GmshSocket::GMSH_PROGRESS || type == GmshSocket::GMSH_INFO || type == GmshSocket::GMSH_WARNING || type == GmshSocket::GMSH_ERROR)){ // we should still allow blank msg strings to be sent Msg::Error("Abnormal server termination (blank message: client not stopped?)"); return false; } switch (type) { case GmshSocket::GMSH_START: setPid(atoi(message.c_str())); break; case GmshSocket::GMSH_STOP: setPid(-1); if(getFather()){ std::string reply = getName(); // reply is dummy getFather()->getGmshServer()->SendMessage (GmshSocket::GMSH_STOP, reply.size(), &reply[0]); } break; case GmshSocket::GMSH_PARAMETER: case GmshSocket::GMSH_PARAMETER_UPDATE: { std::string version, ptype, name; onelab::parameter::getInfoFromChar(message, version, ptype, name); if(onelab::parameter::version() != version){ Msg::Error("ONELAB version mismatch (server: %s / client: %s)", onelab::parameter::version().c_str(), version.c_str()); } else if(ptype == "number"){ onelab::number p; p.fromChar(message); if(type == GmshSocket::GMSH_PARAMETER_UPDATE){ std::vector<onelab::number> par; get(par, name); if(par.size()) { onelab::number y = p; p = par[0]; onelabUtils::updateNumber(p, y); } } set(p); if(p.getName() == getName() + "/Progress"){ #if defined(HAVE_FLTK) if(FlGui::available()) FlGui::instance()->setProgress(p.getLabel().c_str(), p.getValue(), p.getMin(), p.getMax()); #endif } } else if(ptype == "string"){ onelab::string p; p.fromChar(message); if(type == GmshSocket::GMSH_PARAMETER_UPDATE){ std::vector<onelab::string> par; get(par, name); if(par.size()){ onelab::string y = p; p = par[0]; onelabUtils::updateString(p,y); } } set(p); } else if(ptype == "region"){ onelab::region p; p.fromChar(message); set(p); } else if(ptype == "function"){ onelab::function p; p.fromChar(message); set(p); } else Msg::Error("Unknown ONELAB parameter type: %s", ptype.c_str()); } break; case GmshSocket::GMSH_PARAMETER_QUERY: { std::string version, ptype, name, reply; onelab::parameter::getInfoFromChar(message, version, ptype, name); if(onelab::parameter::version() != version){ Msg::Error("ONELAB version mismatch (server: %s / client: %s)", onelab::parameter::version().c_str(), version.c_str()); } else if(ptype == "number"){ std::vector<onelab::number> par; get(par, name); if(par.size() == 1) reply = par[0].toChar(); } else if(ptype == "string"){ std::vector<onelab::string> par; get(par, name); if(par.size() == 1) reply = par[0].toChar(); } else if(ptype == "region"){ std::vector<onelab::region> par; get(par, name); if(par.size() == 1) reply = par[0].toChar(); } else if(ptype == "function"){ std::vector<onelab::function> par; get(par, name); if(par.size() == 1) reply = par[0].toChar(); } else Msg::Error("Unknown ONELAB parameter type in query: %s", ptype.c_str()); if(reply.size()){ getGmshServer()->SendMessage (GmshSocket::GMSH_PARAMETER, reply.size(), &reply[0]); } else{ reply = name; getGmshServer()->SendMessage (GmshSocket::GMSH_PARAMETER_NOT_FOUND, reply.size(), &reply[0]); } } break; case GmshSocket::GMSH_PARAMETER_QUERY_ALL: { std::string version, ptype, name, reply; std::vector<std::string> replies; onelab::parameter::getInfoFromChar(message, version, ptype, name); if(onelab::parameter::version() != version){ Msg::Error("ONELAB version mismatch (server: %s / client: %s)", onelab::parameter::version().c_str(), version.c_str()); } else if(ptype == "number"){ std::vector<onelab::number> numbers; get(numbers); for(std::vector<onelab::number>::iterator it = numbers.begin(); it != numbers.end(); it++) replies.push_back((*it).toChar()); } else if(ptype == "string"){ std::vector<onelab::string> strings; get(strings); for(std::vector<onelab::string>::iterator it = strings.begin(); it != strings.end(); it++) replies.push_back((*it).toChar()); } else if(ptype == "region"){ std::vector<onelab::region> regions; get(regions); for(std::vector<onelab::region>::iterator it = regions.begin(); it != regions.end(); it++) replies.push_back((*it).toChar()); } else if(ptype == "function"){ std::vector<onelab::function> functions; get(functions); for(std::vector<onelab::function>::iterator it = functions.begin(); it != functions.end(); it++) replies.push_back((*it).toChar()); } else Msg::Error("Unknown ONELAB parameter type in query: %s", ptype.c_str()); for(unsigned int i = 0; i < replies.size(); i++) getGmshServer()->SendMessage (GmshSocket::GMSH_PARAMETER_QUERY_ALL, replies[i].size(), &replies[i][0]); reply = "Sent all ONELAB " + ptype + "s"; getGmshServer()->SendMessage (GmshSocket::GMSH_PARAMETER_QUERY_END, reply.size(), &reply[0]); } break; case GmshSocket::GMSH_PARAMETER_CLEAR: clear(message == "*" ? "" : message); break; case GmshSocket::GMSH_PROGRESS: Msg::StatusBar(false, "%s %s", _name.c_str(), message.c_str()); break; case GmshSocket::GMSH_INFO: Msg::Direct("Info : %s - %s", _name.c_str(), message.c_str()); break; case GmshSocket::GMSH_WARNING: Msg::Warning("%s - %s", _name.c_str(), message.c_str()); break; case GmshSocket::GMSH_ERROR: Msg::Error("%s - %s", _name.c_str(), message.c_str()); break; case GmshSocket::GMSH_MERGE_FILE: if(CTX::instance()->solver.autoMergeFile){ unsigned int n = PView::list.size(); MergePostProcessingFile(message, CTX::instance()->solver.autoShowViews, CTX::instance()->solver.autoShowLastStep, true); #if defined(HAVE_FLTK) drawContext::global()->draw(); if(FlGui::available() && n != PView::list.size()){ FlGui::instance()->rebuildTree(true); FlGui::instance()->openModule("Post-processing"); } #endif } break; case GmshSocket::GMSH_OPEN_PROJECT: OpenProject(message); #if defined(HAVE_FLTK) drawContext::global()->draw(); #endif break; case GmshSocket::GMSH_PARSE_STRING: ParseString(message, true); #if defined(HAVE_FLTK) drawContext::global()->draw(); #endif break; case GmshSocket::GMSH_SPEED_TEST: Msg::Info("got %d Mb message in %g seconds", length / 1024 / 1024, GetTimeInSeconds() - timer); break; case GmshSocket::GMSH_VERTEX_ARRAY: { int n = PView::list.size(); PView::fillVertexArray(this, length, &message[0], swap); #if defined(HAVE_FLTK) if(FlGui::available()) FlGui::instance()->updateViews(n != (int)PView::list.size(), true); drawContext::global()->draw(); #endif } break; case GmshSocket::GMSH_CONNECT: { std::string::size_type first = 0; std::string clientName = onelab::parameter::getNextToken(message, first); std::string command = onelab::parameter::getNextToken(message, first); gmshLocalNetworkClient* subClient = new gmshLocalNetworkClient(clientName, command, "", true); onelabGmshServer *server = new onelabGmshServer(subClient); subClient->setPid(0); onelab::string o(subClient->getName() + "/Action", "compute"); o.setVisible(false); o.setNeverChanged(true); onelab::server::instance()->set(o); int sock = server->LaunchClient(); if(sock < 0){ // could not establish the connection: aborting server->Shutdown(); delete server; Msg::Error("Could not connect client '%s'", subClient->getName().c_str()); } else{ Msg::StatusBar(true, "Running '%s'...", subClient->getName().c_str()); subClient->setGmshServer(server); subClient->setFather(this); master->addClient(subClient); } } break; case GmshSocket::GMSH_OLPARSE: { std::string reply = "unavailable"; #if defined(HAVE_ONELAB_METAMODEL) std::string::size_type first = 0; std::string clientName = onelab::parameter::getNextToken(message, first); std::string fullName = onelab::parameter::getNextToken(message, first); preProcess(clientName, fullName); // contrib/onelab/OnelabParser.cpp Msg::Info("Done preprocessing file '%s'", fullName.c_str()); reply = onelab::server::instance()->getChanged(clientName) ? "true" : "false"; #endif getGmshServer()->SendMessage (GmshSocket::GMSH_OLPARSE, reply.size(), &reply[0]); } break; case GmshSocket::GMSH_CLIENT_CHANGED: { std::string::size_type first = 0; std::string command = onelab::parameter::getNextToken(message, first); std::string name = onelab::parameter::getNextToken(message, first); if(command == "get"){ std::string reply = onelab::server::instance()->getChanged(name) ? "true" : "false"; getGmshServer()->SendMessage (GmshSocket::GMSH_CLIENT_CHANGED, reply.size(), &reply[0]); } else if(command == "set"){ std::string changed = onelab::parameter::getNextToken(message, first); onelab::server::instance()->setChanged(changed=="true"?true:false,name); } } break; default: Msg::Warning("Received unknown message type (%d)", type); break; } return true; }
//并查集 int getFather(int n)//查 { if(father[n] == 0) return n; return father[n] = getFather(father[n]); }
int getFather(int c){ if(classof[c]!=c){ classof[c] = getFather(classof[c]); } return classof[c]; }
int getFather (int x) { if (father[x] != x) father[x] = getFather(father[x]); return father[x]; }