static void Union(int x,int y) { int fx = get_father(x); int fy = get_father(y); int ndisx = ndis[x]; int ndisy = ndis[y]; #ifdef DBGMSG printf("union x : %d y : %d fx : %d fy : %d\n",x+1,y+1,fx+1,fy+1); #endif if(fx == fy){ #ifdef DBGMSG printf("fx == fy!return\n"); #endif return; } if(rank[fx] > rank[fy]){ father[fy] = fx; ndis[fy] = (ndisx + ndisy + 1)%2; #ifdef DBGMSG printf("father[%d] == %d\n",fy+1,fx+1); printf("ndis[%d] = (ndis[%d] + ndis[%d] + 1)%2 = (%d + %d +1)%%2 = %d\n",fy+1,x+1,y+1,ndisx,ndisy,ndis[fy]); #endif }else{ father[fx] = fy; if(rank[fx] == rank[fy]){ rank[fy]++; } ndis[fx] = (ndisx + ndisy + 1)%2; #ifdef DBGMSG printf("father[%d] == %d\n",fx+1,fy+1); printf("ndis[%d] = (ndis[%d] + ndis[%d] + 1)%2 = (%d + %d +1)%%2 = %d\n",fx+1,x+1,y+1,ndisx,ndisy,ndis[fx]); #endif } return; }
int main(){ scanf("%d%d",&n,&m); for(i=1;i<=n;i++){ scanf("%d%d",&co[i][0],&co[i][1]); father[i]=i; flag[i]=false; for(j=1;j<i;j++){ edge[i][j]=false; edge[j][i]=false; if (judge_distance(m,i,j)){ edge[i][j]=true; edge[j][i]=true; } } } while(scanf("%c",&c)!=EOF){ if (c=='O'){ scanf("%d",&l); flag[l]=true; for(i=1;i<=n;i++){ if ((flag[i])&&(edge[i][l])){ father[get_father(i)]=get_father(l); } } } if (c=='S'){ scanf("%d%d",&i,&j); if (get_father(i)==get_father(j)){ printf("SUCCESS\n"); } else printf("FAIL\n"); } } }
int main() { int t,n,m,c=0,i,j,k,fj,fk; char cc; scanf("%d",&t); scanf("%d%d",&n,&m); while(c<t){ #ifdef DBGMSG printf("n -- %d\nm -- %d\n",n,m); #endif rank = (int*)malloc(n*sizeof(int)); father = (int*)malloc(n*sizeof(int)); ndis = (int*)malloc(n*sizeof(int)); for(i=0;i<n;i++){ make_set(i); } for(i=0;i<m;i++){ getchar(); scanf("%c%d%d",&cc,&j,&k); #ifdef DBGMSG printf("#%d : cc -- %c %d\nj -- %d\nk -- %d\n",i,cc,cc,j,k); #endif if((cc != 'A' && cc != 'D') || j > n || k > n){ fprintf(stderr,"invalid input\n"); exit(1); } if(cc == 'A'){ fj = get_father(j-1); fk = get_father(k-1); if(fj == fk){ if(ndis[j-1] == ndis[k-1]){ printf("In the same gang.\n"); }else{ printf("In different gangs.\n"); } }else{ printf("Not sure yet.\n"); } }else if(cc == 'D'){ Union(j-1,k-1); } } free(rank); free(father); free(ndis); if(c == (t-1)){ break; } scanf("%d%d",&n,&m); } return 0; }
void Heap::shift_up(int pos){ //Ao chegar na raiz não faz nada if(pos==1){ return; } //se o elemento for maior que o pai dele, troca de posição com o pai else if(this->elements[pos] > this->elements[get_father(pos)]){ int temp = this->elements[pos]; this->elements[pos] = this->elements[get_father(pos)]; this->elements[get_father(pos)] = temp; shift_up(get_father(pos)); } }
int get_father(int x){ if (father[x]==x) return x; else { int tmp=get_father(father[x]); father[x]=tmp; return tmp; } }
void traverse_up(tree_node *r, int **fdown, int **fup, int level) {//用先序遍历树的方法遍历每个节点找出其向上的最大权值 //实际竞赛题目中,使用最简单的数组与下标来构建树,则也可以使用bfs if(r != NULL){ get_father(r, r, fdown, fup, level, 1); for(int i = 0; i < r->t_cnt; ++ i) traverse_up(r->t_child[i], fdown, fup, level); } }
void decrease_key(position pos,element_type delta,PRIORITY_QUEUE H) { if(pos>H->size) return; position prev; *H->elements[pos]+=delta; for(prev=get_father(pos); prev>=1&&((*H->elements[prev])>(*H->elements[pos])); pos=prev,prev=get_father(prev)) { swap_elements(prev,pos,H); } if(((*H->elements[prev])=(*H->elements[pos]))&&((*H->elements[prev])>(*H->elements[pos+1]))) { swap_elements(prev,pos+1,H); } }
static int get_father(int x) { int prev,ndisx = ndis[x]; #ifdef DBGMSG printf("current father of %d is %d\n",x+1,father[x]+1); #endif if(father[x] != x){ prev = father[x]; father[x] = get_father(father[x]); ndis[x] = (ndisx + ndis[prev])%2; #ifdef DBGMSG printf("new father of %d is %d\n",x+1,father[x]+1); printf("adjust ndis : ndis[%d] = (ndis[%d] + ndis[%d])%%2 = (%d + %d)%%2 = %d\n",x+1,x+1,prev+1,ndisx,ndis[prev],ndis[x]); #endif } return father[x]; }
void get_father(tree_node *r, tree_node *p, int **fdown, int **fup, int level, int prev = 1) {//递归的向上求出r到达 p 祖父节点那一层的最大权值 //p指代r向上第 prev-1 层的祖父节点 //prev == 1时,p即为r自己,p->t_fa为r的父节点 //prev > level时,已经超越了向上的最大层数level //递归终止条件 //若p的父节点为空指针,即p已经为根节点 if(p->t_fa == NULL) return; //prev > level是递归结束条件 if(prev > level) return; if(prev == level){ //到达最高的祖父节点时,不访问孩子子树 fup[r->t_idx][prev] = p->t_fa->t_value; } else{ //其他祖父节点可以到达其孩子子树 for(int i = 0; i < p->t_fa->t_cnt; ++ i) //遍历p点的父节点的所有孩子节点,除过自己 //即遍历p点的所有兄弟节点 if(p->t_fa->t_child[i] != p){ //累加父节点的向下权值时要除去p自己这一子树的分支 //r节点向上prev层的最大权值等于 //上面prev层的祖父节点的所有孩子节点的向下 level-prev-1 层的最大权值 //再加上孩子节点自己的权值 fup[r->t_idx][prev] += fdown[p->t_fa->t_child[i]->t_idx][level - prev - 1]; fup[r->t_idx][prev] += p->t_fa->t_child[i]->t_value; } //还要加上p的父节点的权值 fup[r->t_idx][prev] += p->t_fa->t_value; } //递归上一层父节点 get_father(r, p->t_fa, fdown, fup, level, prev + 1); }