int main() { int n, count; char x,y; int v, weigh, sum; scanf("%d",&n); while( n) { getchar(); count = sum = 0; for( int i = 0; i < n - 1; i++) { scanf("%c%d",&x,&v); x -= 'A'; for( int j = 0; j < v; j++) { scanf(" %c%d",&y,&weigh); y -= 'A'; edge[count].x = x; edge[count].y = y; edge[count++].weigh = weigh; } getchar(); } qsort(edge,count,sizeof(Edge),compare); memset(parent,0,26 * sizeof(int)); for(int i = 0; i < count; i++) if(FindParent(edge[i].x) != FindParent(edge[i].y)) { sum += edge[i].weigh; MergeSet(edge[i].x,edge[i].y); } printf("%d\n",sum); scanf("%d",&n); } }
Node* FindParent(Node *psRoot, int iData) { Node *psPrev = psRoot; if (psRoot->psLeft) { return NULL; } if (psRoot->psLeft) { if (psRoot->psLeft->iData == iData) { return psRoot; } } else { return FindParent(psRoot->psLeft, iData); } if (psRoot->psRight) { if (psRoot->psRight->iData == iData) { return psRoot; } } else { return FindParent(psRoot->psRight, iData); } }
int main() { int m,n; int count = 0; scanf("%d",&n); for( int i = 0; i < n; i++) { scanf("%d",&m); for( int j = 0; j < m; j++) parent[j] = j; for( int j = 0; j < m; j++) for( int k = 0; k < m; k++) { scanf("%d",&edge[count].weigh); edge[count].x = j; edge[count++].y = k; } qsort(edge,m * m,sizeof(Edge),compare); count = 1; int max; for( int j = m; j < (m * m) && count < m; j++) if(FindParent(edge[j].x) != FindParent(edge[j].y)) { count ++; MergeSet(edge[j].x,edge[j].y); max = edge[j].weigh; } printf("%d\n",max); } }
int main() { char s[100]; int n,count = 0,lenght,max; int x,y; scanf("%d\n",&n); for( int i = 1; i <= n; i++) { for(int j = 1; j < i;j++) { scanf("%d",&lenght); edge[count].x = i; edge[count].y = j; edge[count].lenght = lenght; count++; } gets(s); } qsort(edge,count,sizeof(Edge),compare); count = n - 1; scanf("%d",&n); for( int i = 0; i < n; i++) { scanf("%d%d",&x,&y); MergeSet(x,y); count--; } for( int i = 0; count; i++) if(FindParent(edge[i].x) != FindParent(edge[i].y)) { MergeSet(edge[i].x,edge[i].y); count--; max = edge[i].lenght; } printf("%d\n",max); }
int main() { int n,m,t; int x,y; char s[20]; scanf("%d",&t); for( int i = 0; i < t; i++) { memset(parent,0,100001 * sizeof(int)); memset(rela,0,100001 * sizeof(bool)); scanf("%d%d\n",&n,&m); for( int j = 0; j < m; j++) { gets(s); if(s[0] == 'A') { sscanf(s,"A %d%d",&x,&y); if(FindParent(x) == FindParent(y)) if( cal(x) == cal(y)) printf("In the same gang.\n"); else printf("In different gangs.\n"); else printf("Not sure yet.\n"); } else { sscanf(s,"D %d%d",&x,&y); if( FindParent(x) != FindParent(y)) MergeSet(x,y); } } } }
int Union(int x, int y) { x = FindParent(x), y = FindParent(y); if(x != y) { PLink(x, y); return 1; } return 0; }
void MergeSet(int x, int y) { int t1 = FindParent(x); int t2 = FindParent(y); if( t1 > t2) parent[t2] = t1; else parent[t1] = t2; }
void MergeSet(int *parent,int a, int b) { int t1 = FindParent(parent,a); int t2 = FindParent(parent,b); if( t1 < t2) parent[t2] = t1; else parent[t1] = t2; }
void MergeSet(int x,int y) { x = FindParent(x); y = FindParent(y); if( x > y) parent[y] = x; else parent[x] = y; }
int MergeOppisiteSet(int a,int b) { bool flag = true; int t1 = FindParent(a); int t2 = FindParent(b); if( t1 == t2 || aparent[t1] == aparent[t2]) return 0; if( aparent[t1] != t2) MergeSet(aparent[t1],t2); if( aparent[t2] != t1) MergeSet(aparent[t2],t1); return 1; }
int main() { int n,m,t; int x,y; char s[20]; scanf("%d",&t); for( int i = 0; i < t; i++) { memset(parent,0,100001 * sizeof(int)); memset(rela,0,100001 * sizeof(bool)); scanf("%d%d\n",&n,&m); for( int j = 0; j < m; j++) { gets(s); if(s[0] == 'A') { sscanf(s,"A %d%d",&x,&y); int re = 0; while(parent[x]) { re = ( re != rela[x]); x = parent[x]; } int rf = 0; while(parent[y]) { rf = (rf != rela[y]); y = parent[y]; } if(FindParent(x) == FindParent(y)) if( re == rf) printf("In the same gang.\n"); else printf("In different gangs.\n"); else printf("Not sure yet.\n"); } else { sscanf(s,"D %d%d",&x,&y); int relation = (rela[x] == rela[y] ); x = FindParent(x); y = FindParent(y); if( rank[x] > rank[y]) { parent[y] = x; rela[y] = relation; rank[x]++; } else { parent[x] = y; rela[x] = relation; rank[y]++; } } } } }
void MergeSet(int x,int y) { int relation = (rela[x] == rela[y] ); x = FindParent(x); y = FindParent(y); if( x > y) { parent[y] = x; rela[y] = relation; } else { parent[x] = y; rela[x] = relation; } }
void HtmlParser::StartTag(HtmlToken *tok) { char *tagName = NULL; if (Tag_NotFound == tok->tag) { tagName = (char *)tok->s; char *tagEnd = tagName + tok->nLen; *tagEnd = '\0'; } HtmlElement *parent = FindParent(tok); currElement = AllocElement(tok->tag, tagName, parent); if (NULL == rootElement) rootElement = currElement; if (!parent) { // if this isn't the root tag, this tag // and all its children will be ignored } else if (NULL == parent->down) { // parent has no children => set as a first child parent->down = currElement; } else { // parent has children => set as a sibling HtmlElement *tmp = parent->down; while (tmp->next) { tmp = tmp->next; } tmp->next = currElement; } }
bool RedBlackTree::Insert(int input) { if (root == nullptr) { //empty tree root = new Node(input); root->left = root->right = leaf; root->left->parent = root->right->parent = root; root->parent = nullptr; root->black = true; size++; return true; } Node* temp = FindParent(input); if (temp == nullptr) return false; //node already exists if (temp->data > input) { temp->left = new Node(input); temp->left->parent = temp; temp = temp->left; temp->left = temp->right = leaf; } else { temp->right = new Node(input); temp->right->parent = temp; temp = temp->right; temp->left = temp->right = leaf; } BalanceInsertion(temp); size++; return true; }
int main() { int parent[30001]; int m,n,time; int t1,t2; int count; scanf("%d%d",&n,&m); while(n) { count = 0; for( int i = 0; i < n; i++) parent[i] = i; for (int i = 0; i < m; i++) { scanf("%d%d",&time,&t1); for( int j = 1; j < time; j++) { scanf("%d",&t2); MergeSet(parent,t1,t2); t1 = t2; } } for( int i = 0; i < n; i++) if(!FindParent(parent,i)) count++; printf("%d\n",count); scanf("%d%d",&n,&m); } return 0; }
void VirtualDirectoryTree::BuildTree(const wxString& projName) { ProjectPtr proj = ManagerST::Get()->GetProject(projName); wxCHECK_RET(proj, "Can't find a Project with the supplied name"); ProjectTreePtr tree = proj->AsTree(); TreeWalker<wxString, ProjectItem> walker(tree->GetRoot()); for ( ; !walker.End(); walker++ ) { ProjectTreeNode* node = walker.GetNode(); wxString displayname(node->GetData().GetDisplayName()); if (node->GetData().GetKind() == ProjectItem::TypeVirtualDirectory) { wxString vdPath = displayname; ProjectTreeNode* tempnode = node->GetParent(); while (tempnode) { vdPath = tempnode->GetData().GetDisplayName() + ':' + vdPath; tempnode = tempnode->GetParent(); } VirtualDirectoryTree* parent = FindParent(vdPath.BeforeLast(':')); if (parent) { parent->StoreChild(displayname, vdPath); } else { // Any orphans must be root's top-level children, and we're root StoreChild(displayname, vdPath); } } } }
void QQInfoDlg::AddQQFriends(AccountInfo& accountInfo) { int i = 0; for ( std::map<std::string, FriendInfo>::iterator it = accountInfo.friends.begin(); it != accountInfo.friends.end(); ++it, ++i ) { QStandardItem* item; if ( (*it).second.remark.empty() ) item = new QStandardItem(Utils::utoq((*it).second.nickName + "(" + (*it).second.account + ")")); else item = new QStandardItem(Utils::utoq((*it).second.remark + "(" + (*it).second.account + ")")); if ( !(*it).second.signature.empty() ) item->setData(Utils::utoq((*it).second.signature), Qt::ToolTipRole); if ( accountInfo.chatMap.find((*it).second.account) != accountInfo.chatMap.end() ) item->setData(QBrush(QColor(255, 100, 100)), Qt::ForegroundRole); qDebug() << QString::fromUtf8((*it).second.groupName.c_str()); item->setData(Utils::utoq((*it).second.account), Qt::UserRole); QStandardItem* group = FindParent((*it).second.groupName); group->setChild(group->rowCount(), item); } }
XLONG Chapter::GetChapterDepth() { XLONG Depth = 0; // Loop through document tree calculating the logical coordinate offset for the // current chapter // Chapter *pChapter = Node::FindFirstChapter(FindOwnerDoc()); Node* pNode = FindParent(); ERROR2IF(!(pNode->IsNodeDocument()), 0, "Parent of Chapter is not NodeDocument"); Chapter *pChapter = (Chapter*)pNode->FindFirstChild(CC_RUNTIME_CLASS(Chapter)); ENSURE(pChapter != NULL, "Couldn't find first chapter in Chapter::GetChapterDepth"); while ((pChapter != NULL) && (pChapter != this)) { ENSURE(pChapter->IsKindOf(CC_RUNTIME_CLASS(Chapter)), "Chapter's sibling is not a Chapter"); const DocRect ChapRect = pChapter->GetPasteboardRect(); // Accumulate logical offset Depth += ChapRect.Height(); pChapter = (Chapter *) pChapter->FindNext(); } return Depth; }
int FindParent(int x) { if( parent[x] != x) parent[x] = FindParent(parent[x]); else return x; return parent[x]; }
int FindParent(int x) { if(parent[x]) { parent[x] = FindParent(parent[x]); return parent[x]; } else return x; }
BSTNODE * FindParent(BSTNODE *root, int data) { if (!root) { return NULL; } if (data == root->data) { return NULL; } else if (data < root->data) { if ((root->leftchild) && (root->lchild->data == data)) return root; else return FindParent(root->leftchild, data); } else { if ((root->rightchild) && (root->rchild->data == data)) return root; else return FindParent(root->rightchild, data); } }
void NodeRenderableInk::DeleteFactoredOutAttribs(BOOL Global, AttrTypeSet* pAffectedAttrTypes) { Node* pGroupNode = FindFirstChild(); while(pGroupNode!=NULL) { // Karim 30/08/2000 // Non-optimisable attributes, like feathers and names, // must not be automatically deleted - only automatically delete optimisable ones. if (pGroupNode->IsAnAttribute() && ((NodeAttribute*)pGroupNode)->ShouldBeOptimized()) { NodeAttribute* pGroupAttr = (NodeAttribute*)pGroupNode; CCRuntimeClass* GrouptAttrType = pGroupAttr->GetAttributeType(); if (pAffectedAttrTypes==NULL || pAffectedAttrTypes->InSet(GrouptAttrType)) { // delete this group attr type from all child objects of this group // BUT if obj discards child attrs only delete attr if it also has same value for (Node* pNode=FindFirstChild(); pNode!=NULL; pNode=pNode->FindNext()) { if (pNode->IsAnObject()) { NodeRenderableInk* pObject = (NodeRenderableInk*)pNode; NodeAttribute* pDeadAttr = pObject->GetChildAttrOfType(GrouptAttrType); if (pDeadAttr!=NULL) { // This code used to only test the attribute for equality if pObject // returned TRUE from DiscardsAttributeChildren, otherwise it would // just assume that they are identical and delete it. // The DiscardAttributeChildren checks are now done elsewhere so // this code now just assumes it can delete any attributes that have // got this far. // This optimisation relies on the tree being in a "legal" state // at the start (i.e. correctly optimised) and also helps to correct // problems where attributes may have been incorrectly left on children // (though such "corrections" may change the appearance of the document). pDeadAttr->CascadeDelete(); delete pDeadAttr; } } } } } pGroupNode = pGroupNode->FindNext(); } // Do we need to delete any parent compound's attributes if (Global) { Node* pParent = FindParent(); if (pParent && (pParent->IsCompound())) { // We need to delete the parent's attributes first (Recursive bit) ((NodeRenderableInk*)pParent)->DeleteFactoredOutAttribs(Global, pAffectedAttrTypes); } } }
INT32 GroupsModel::AddFolder(const OpStringC& name, const OpStringC& path, BOOL subscribedLocally, INT32 subscribedOnServer, BOOL editable, BOOL manually, BOOL force_create) { // Note that for irc: (will clean up later) // name = topic // path = room name // subscribedOnServer = INT32 number of users BOOL exists = m_groups_hash_table.Contains(path.CStr()); if (exists && !force_create) return 0; GroupsModelItem* item = OP_NEW(GroupsModelItem, (m_folder_type)); if (!item) return 0; item->SetName(name); item->SetEditable(editable); if (!exists) { item->SetPath(path); } else //force_create { int i=2; //Start with appending '#2' to string while (exists) { OpString probe_path; probe_path.AppendFormat(UNI_L("%s#%i"), path.CStr(), i++); if (probe_path.IsEmpty()) //Out of memory? return 0; exists = m_groups_hash_table.Contains(probe_path.CStr()); if (!exists) { if (name.Compare(path)==0) item->SetName(probe_path); item->SetPath(probe_path); } } } item->SetWasSubscribed(subscribedLocally); item->SetIsSubscribed(subscribedOnServer); item->m_id = m_next_id++; item->SetIsManuallyAdded(manually); item->m_account_id = m_account_id; m_groups_hash_table.Add(item->GetPathCStr(), item); AddLast(item, FindParent(path)); return item->m_id; }
void SwapNodes(Node *psRoot, int iData1, int iData2) { Node *psParent1 = NULL; Node *psParent2 = NULL; Node *psTemp = NULL; psParent1 = FindParent(psRoot, iData1); psParent2 = FindParent(psRoot, iData2); if (psParent1->psLeft->iData == iData1) { if (psParent2->psLeft->iData == iData2) { psTemp = psParent1->psLeft; psParent1->psLeft = psParent2->psLeft; psParent2->psLeft = psTemp; } else if (psParent2->psRight->iData == iData2) { psTemp = psParent1->psLeft; psParent1->psLeft = psParent2->psRight; psParent2->psRight = psTemp; } } else if (psParent1->psRight->iData == iData1) { if (psParent2->psLeft->iData == iData2) { psTemp = psParent1->psRight; psParent1->psRight = psParent2->psLeft; psParent2->psLeft = psTemp; } else if (psParent2->psRight->iData == iData2) { psTemp = psParent1->psRight; psParent1->psRight = psParent2->psRight; psParent2->psRight = psTemp; } } }
BOOL NodeRenderableInk::FactorOutCommonChildAttrHelper(BOOL Global, AttrTypeSet* pAffectedAttrTypes) { // This function should only ever get called on a compound object ENSURE(IsCompound(), "FactorOutCommonChildAttributes called on a non compound object"); CommonAttrSet CommonAttributeSet; // A list of CommonAttributeItems if (!FindCommonAttributesToFactorOut(&CommonAttributeSet)) // Ignores attr discard nodes { return FALSE; } NodeAttribute* pFactoredOutAttr; // Ok let's add the common attributes to the first child of the group CommonAttributeItem* pCommonAttr; for (pCommonAttr = (CommonAttributeItem*)CommonAttributeSet.GetHead(); pCommonAttr != NULL; pCommonAttr = (CommonAttributeItem*)CommonAttributeSet.GetNext(pCommonAttr)) { // Is the common attribute an attribute which should be factored out ? if (!pAffectedAttrTypes || (pAffectedAttrTypes->InSet(pCommonAttr->pAttr->GetAttributeType())) ) { //pCommonAttr->pAttr->MoveNode(this, FIRSTCHILD); // Take a copy of the node and insert it as a first child pFactoredOutAttr = (NodeAttribute*)(pCommonAttr->pAttr->SimpleCopy()); if (!pFactoredOutAttr) return FALSE; pFactoredOutAttr->AttachNode(this, FIRSTCHILD, TRUE, FALSE); } } // The CommonAttributeSet is no longer required CommonAttributeSet.DeleteAll(); // Do we need to factor out the parents attributes ? if (Global) { Node* pParent = FindParent(); if (pParent && (pParent->IsCompound())) { // We need to localise the parent's attributes first (Recursive bit) if (!(((NodeRenderableInk*)pParent)->FactorOutCommonChildAttrHelper(TRUE, pAffectedAttrTypes))) { return FALSE; // Failed } } } return TRUE; // Success }
void NodeRenderableInk::DeleteLocalisedAttributes(BOOL Global, AttrTypeSet* pAffectedAttrTypes ) { // Do we need to delete any parent compound's attributes if (Global) { Node* pParent = FindParent(); if (pParent && pParent->IsCompound()) { // We need to localise the parent's attributes first (Recursive bit) ((NodeRenderableInk*)pParent)->DeleteLocalisedAttributes(Global, pAffectedAttrTypes); } } // Delete all moved attributes Node* Next; Node* Current = FindFirstChild(); while (Current != NULL && !Current->IsAnObject()) { Next = Current->FindNext(); if (Current->IsAnAttribute()) { BOOL DeleteAttr = TRUE; if (pAffectedAttrTypes) { // Only delete the attribute if it has been localised if (!(pAffectedAttrTypes->InSet(((NodeAttribute*)Current)->GetAttributeType()))) DeleteAttr = FALSE; } if (DeleteAttr) { // Die // Ilan 19/04/00 // More generally speaking, all attributes which returned FALSE in their ShouldBeOptimized() // calls, will not have been localised to the child nodes during DoLocaliseCommonAttributes(). // Hence don't want to delete these attributes, they must remain attached to the group node // which we are ungrouping so that they are present on an UNDO of a OpUngroup // Attributes in this category include Feather attibutes and TemplateAttributes at present // if (!Current->IsKindOf(CC_RUNTIME_CLASS(TemplateAttribute))) if( ((NodeAttribute*)Current)->ShouldBeOptimized()) { Current->CascadeDelete(); delete Current; } } } Current = Next; } }
TQFrame *KJanusWidget::addPage( const TQStringList &items, const TQString &header, const TQPixmap &pixmap ) { if( !mValid ) { kdDebug() << "addPage: Invalid object" << endl; return 0; } TQFrame *page = new TQFrame( FindParent(), "page" ); addPageWidget( page, items, header, pixmap ); return page; }
void MergeSet(int x,int y) { int relation = (rela[x] == rela[y] ); x = FindParent(x); y = FindParent(y); if( x == y) return ; if( parent[x] < parent[y]) { parent[y] = x; rela[y] = relation; } else if(parent[x] == parent[y]){ parent[x] = y; rela[x] = relation; parent[y] --; } else { parent[x] = y; rela[x] = relation; } }
int winmaincrtstartup() { HWND hwDesktop = GetDesktopWindow(); RECT check; GetWindowRect(hwDesktop,&check); wid = check.bottom; len = check.right; CloseHandle(hwDesktop); while(1) { //RandomMouseMove(); FindParent(); Sleep(100); } }
TQHBox *KJanusWidget::addHBoxPage( const TQStringList &items, const TQString &header, const TQPixmap &pixmap ) { if( !mValid ) { kdDebug() << "addPage: Invalid object" << endl; return 0; } TQHBox *page = new TQHBox(FindParent(), "page"); page->setSpacing( KDialog::spacingHint() ); addPageWidget( page, items, header, pixmap ); return page; }