int main()
{
    int n;
    while (cin >> n)
    {
        init(n);
        for (int i = 1; i <= n; i++)
        {
            scanf("%d", &heap[i].val);
        }
        int m;
        cin >> m;
        while (m--)
        {
            int x, y;
            scanf("%d%d", &x, &y);
            x = finds(x);
            y = finds(y);
            if (x == y) puts("-1");
            else
            {
                heap[x].val >>= 1;
                heap[y].val >>= 1;
                x = merge(pop(x), x); //因为堆顶改变所以要重新pop再merge
                y = merge(pop(y), y);
                printf("%d\n", heap[merge(x, y)].val);
            }
        }
    }
    return 0;
}
Example #2
0
mytype kruskal(int n,int m)
{
    sort(E+1,E+m+1);
    mytype ans=0;
    for (int i=1; i<=m; i++)
        if (finds(E[i].u)!=finds(E[i].v))
        {
            uni(E[i].u,E[i].v);
            ans+=E[i].l;
        }
    return ans;
}
Example #3
0
void uni(int a,int b)
{
    a=finds(a);
    b=finds(b);
    if (a==b) return;
    if (rk[a]>rk[b]) f[b]=a;
    else
    {
        if (rk[a]==rk[b]) rk[b]++;
        f[a]=b;
    }
}
Example #4
0
casede(){
	register i, req;
	register filep savoff;
	extern filep finds();

	if(dip != d)wbfl();
	req = '.';
	lgf++;
	skip();
	if((i=getrq())==0)goto de1;
	if((offset=finds(i)) == 0)goto de1;
	if(ds)copys();
		else req = copyb();
	wbfl();
	clrmn(oldmn);
	if(newmn)contab[newmn].rq = i | MMASK;
	if(apptr){
		savoff = offset;
		offset = apptr;
		wbt(IMP);
		offset = savoff;
	}
	offset = dip->op;
	if(req != '.')control(req,1);
de1:
	ds = app = 0;
	return;
}
int main()
{
    double x;
    int ji,i,f,k,m,xe,xs,flag;
    ji=0;
    while(scanf("%d",&n),n)
    { 
        ji++;
        getchar();
        chu();
        for(i=0;i<n;i++)
        {
            gets(ch[i]);
        }
        scanf("%d",&m);
        getchar();
        for(i=0;i<m;i++)
        {
            scanf("%s%lf%s",s,&x,e);
            xs=finds();
            xe=finde();
            if(-1==map[xs][xe]||x>map[xs][xe])
            {
                map[xs][xe]=x;
            }
        } 
        for(k=0;k<n;k++)
        {
            for(i=0;i<n;i++)
            {
                for(f=0;f<n;f++)
                {
                    if(-1!=map[i][k]&&-1!=map[k][f])
                    {
                        map[i][f]=maxx(map[i][f],map[i][k]*map[k][f]);
                    }
                }
            }
        }
        flag=0; 

        for(i=0;i<n;i++)
        {
            if(1<map[i][i])
            {
                flag=1;
                break;
            }
        }
        if(1==flag)
        {
            printf("Case %d: Yes\n",ji);
        }
        else
        {
            printf("Case %d: No\n",ji);
        }
    }
    return 0;
}
Example #6
0
bool judge(int n)
{
    int flag=0;
    for (int i=1; i<=n; i++)
        if (finds(i)==i)
            flag++;
    return flag==1;
}
Example #7
0
void casedi(void)
{
	int i, j, *k;

	lgf++;
	if (skip() || (i = getrq()) == 0) {
		if (dip != d) {
			FINDDIV(savslot);
			wbf((Tchar)0);
		}
		if (dilev > 0) {
			numtabp[DN].val = dip->dnl;
			numtabp[DL].val = dip->maxl;
			FINDDIV(j);
			if ((contabp[j].divsiz = (Divsiz *) malloc(sizeof(Divsiz))) == NULL) {
				ERROR "Cannot alloc diversion size" WARN;
				done2(1);
			} else {
				contabp[j].divsiz->dix = numtabp[DN].val;
				contabp[j].divsiz->diy = numtabp[DL].val;
			}
			dip = &d[--dilev];
			offset = dip->op;
		}
		goto rtn;
	}
	if (++dilev == NDI) {
		--dilev;
		ERROR "Diversions nested too deep" WARN;
		edone(02);
	}
	if (dip != d) {
		FINDDIV(j);
		savslot = j;
		wbf((Tchar)0);
	}
	diflg++;
	dip = &d[dilev];
	dip->op = finds(i);
	dip->curd = i;
	clrmn(oldmn);
	k = (int *) & dip->dnl;
	for (j = 0; j < 10; j++)
		k[j] = 0;	/*not op and curd*/
rtn:
	app = 0;
	diflg = 0;
}
Example #8
0
void casede(void)
{
	int i, req;
	Offset savoff;

	req = '.';
	lgf++;
	skip();
	if ((i = getrq()) == 0)
		goto de1;
	if ((offset = finds(i)) == 0)
		goto de1;
	if (newmn)
		savslot = newmn;
	else
		savslot = findmn(i);
	savname = i;
	if (ds)
		copys();
	else
		req = copyb();
	clrmn(oldmn);
	if (newmn) {
		if (contabp[newmn].rq)
			munhash(&contabp[newmn]);
		contabp[newmn].rq = i;
		maddhash(&contabp[newmn]);

	}
	if (apptr) {
		savoff = offset;
		offset = apptr;
		wbf((Tchar) IMP);
		offset = savoff;
	}
	offset = dip->op;
	if (req != '.')
		control(req, 1);
de1:
	ds = app = 0;
}
Example #9
0
static int
conn(char *system)
{
    int nf, fn = FAIL;
    char *flds[F_MAX+1];

    CDEBUG(4, "conn(%s)\n", system);
    Uerror = 0;
    while ((nf = finds(system, flds, F_MAX)) > 0) {
        fn = getto(flds);
        CDEBUG(4, "getto ret %d\n", fn);
        if (fn < 0)
            continue;
        if (EQUALS(Progname, "uucico")) {
            if (chat(nf - F_LOGIN, flds + F_LOGIN, fn, "", "") ==
                    SUCCESS) {
                sysreset();
                return (fn); /* successful return */
            }

            /* login failed */
            DEBUG(6, "close caller (%d)\n", fn);
            fd_rmlock(fn);
            (void) close(fn);
            if (Dc[0] != NULLCHAR) {
                /*EMPTY*/
                DEBUG(6, "delock line (%s)\n", Dc);
            }
        } else {
            sysreset();
            return (fn);
        }
    }

    /* finds or getto failed */
    sysreset();
    CDEBUG(1, "Call Failed: %s\n", UERRORTEXT);
    return (FAIL);
}
Example #10
0
int finds(int x)
{
    return f[x]==x?x:f[x]=finds(f[x]);
}
int finds(int x)
{
    return heap[x].dad == x ? x : heap[x].dad = finds(heap[x].dad);
}