int main() { int n,m; while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) return 0; int i,j,num,a,b; init(n); for(i=0;i<m;i++) { scanf("%d",&num); scanf("%d",&a); for(j=1;j<num;j++) { scanf("%d",&b); Union(a,b); } } int count=0; for(i=0;i<n;i++) { if(find_father(i)==find_father(0)) count++; } printf("%d\n",count); } return 0; }
void dfs_lca(graph_list g, int p, map<pair<int, int>, int>& query, int *visited, tree_node *tree) { visited[p] = 1; //进行下一轮dfs遍历,并更新并查集 for(int i = 1; i < (int)g.g_l[p].size(); ++ i){ dfs_lca(g, g.g_l[p][i].g_idx, query, visited, tree); //将p节点和它的孩子节点i.g_idx合并 tree_node *pf1 = find_father(&tree[p]); tree_node *pf2 = find_father(&tree[g.g_l[p][i].g_idx]); //将孩子节点i.g_idx的父节点指针设置为p节点的父节点 pf2->t_fa = pf1; } //用并查集进行查询 for(map<pair<int, int>, int>::iterator it = query.begin(); it != query.end(); ++ it){ //查询所有与节点p相关的节点对 if(p == it->first.first && visited[it->first.second]){ //若有与节点p相关的查询,且另一个节点second已被访问 tree_node *fa = find_father(&tree[it->first.second]); //则节点p与另一个节点second的最近公共祖先是second的并查集父节点 it->second = fa->t_idx; } if(p == it->first.second && visited[it->first.first]){ tree_node *fa = find_father(&tree[it->first.first]); it->second = fa->t_idx; } } }
tree_ptr find_father(SEARCH_TREE T,SEARCH_TREE root) { if(T==NULL||root==NULL) return NULL; if(root->left==T||root->right==T) return root; if(T->element<root->element) return(find_father(T,root->left)); else return(find_father(T,root->right)); }
void insert_nonrecu(element_type x,SEARCH_TREE T) { if(T==NULL){ T=node_init(x); return; } avl_ptr f,tmp; f=T; int i; while((i=is_father_fit(x,f))<0){ if(i==-1) f=f->left; else f=f->right; } if(i==0) /* 已有 */ return; tmp=node_init(x); if(i==1){ f->left=tmp; }else{ f->right=tmp; } for(;tmp!=T;tmp=f){ f=find_father(tmp,T); total_rotated(x,f); } }
void total_rotate(element_type x,SEARCH_TREE T,SEARCH_TREE root) { tree_ptr father=find_father(T,root),rotated_tree; if((height(T->left)-height(T->right))==2){ if(x<T->left->element) rotated_tree=s_rotated_left(T); else rotated_tree=d_rotated_left(T); if(parent->left==T) parent->left=rotated_tree; else parent->right=rotated_tree; }else{ T->height=node_height(T); } if((height(T->right)-height(T->left))==2){ if(x>T->right->element) rotated_tree=s_rotated_right(T); else rotated_tree=d_rotated_right(T); if(parent->right==T) parent->right=rotated_tree; else parent->left=rotated_tree; }else{ T->height=node_height(T); } }
int find_father(int child) { if(father[child]==child) return child; father[child]=find_father(father[child]); return father[child]; }
node* tree::insert (const float num) { node* newnode = new node(num); node* father = find_father(headnode, newnode); if ( father == sentry ) headnode=newnode; if ( (newnode->value() ) > ( father->value() ) ) link (father, newnode, RIGHT); else link (father, newnode, LEFT); newnode->set_right (sentry); newnode->set_left (sentry); return newnode; }
int main() { scanf("%d %d",&n,&m); memset(a,-1,sizeof(a)); int i; int tag=1; for(i=0;i<m;i++) { scanf("%d%d",&l,&r); l--,r--; int rl=find_father(r); if(rl==l) { printf("0\n"); tag=0; } add(l,r); } if(tag) printf("1\n"); return 0; }
int find_father(int n) { if(a[n]==-1) return n; else return find_father(a[n]); }
void Union(int a,int b) { int f_a=find_father(a); int f_b=find_father(b); father[f_a]=f_b; }
tree_node* find_father(tree_node *p) { if(p->t_fa != p) p->t_fa = find_father(p->t_fa); return(p->t_fa); }