FIG set and check the maze ----- little hope

Small Greek maze

HDU - 1272


FIG determination:

1. does not communicate: all data not in the same set, i.e., do not communicate FIG not

2. communication: if the parent node to the same as before the merger, description has communicated, there are multiple communication paths are not FIG.

#include<cstdio>
#include<cstdlib>
#include<cstring>
#define MAXN 100100
int flag[MAXN],/**标记是否存在该房间号*/ f[MAXN];
int getf(int a)
{
    if(f[a] == a)
        return a;
    else
    {
        f[a] = getf(f[a]);
        return f[a];
    }
}
void merge(int s, int t)
{
    int a = getf(s);
    int b = getf(t);
    if (a > b)///以小的房间号作为父亲
        f[a] = b;
    else
        f[b] = a;
}
int main()
{
    int n, m, i, sign;
    while(~scanf("%d %d", &n, &m))
    {
        if (n == -1 && m == -1)
            exit(1);
        for(i = 1; i <= MAXN; i++)
            f[i] = i;///初始化父结点
        sign = 0;///判断标记
        memset(flag, 0, sizeof(flag));
        while(1)
        {
            if(m == 0 && n == 0)
                break;
            if(f[n] == f[m])///如果合并之前父节点就相同, 说明已经连通,存在多条相通的路径
                sign = 1;
            merge(n, m);
            flag[m] = flag[n] = 1;
            scanf("%d %d", &n, &m);
        }
        if (sign)
            printf("No\n");
        else
        {
            int sum = 0;
            for(i = 0; i <= MAXN; i++)
                if (flag[i] && f[i] == i)
                    sum++;///所有房间不在同一个集合,即不连通
            if (sum > 1)
                printf("No\n");
            else
                printf("Yes\n");
        }
    }
    return 0;
}


Published 29 original articles · won praise 4 · Views 4702

Guess you like

Origin blog.csdn.net/onion___/article/details/79136743