MST 小希的迷宫

不知道有没有更好的做法(省空间,省时间)。

http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?pid=1002&ojid=0&cid=1304&hide=0

# include <cstdio>

# include <cstring>



# define N 100000 + 5



int p[N];

bool ok, vis[N];



void init(void)

{

    ok = false;

    for (int i = 1; i < N; ++i) p[i] = i;

    memset(vis, false, sizeof(vis));

}



int find(int x) {return x==p[x] ? x:(p[x]=find(p[x]));}



int main()

{

    int x, y;



    init();

    while (~scanf("%d%d", &x, &y))

    {

        if (x==-1 && y==-1) break;

        if (x==0 && y==0)

        {

            if (ok == false)

            {

                int fa = -1;

                for (int i = 1; i < N; ++i) if (vis[i])

                {

                    if (fa == -1) fa = find(i);

                    else if (fa != find(i)) {ok = true;break;}

                }

            }

            puts(ok ? "No":"Yes");

            init();

        }

        else if(ok == false)

        {

            x = find(x), y = find(y);

            vis[x] = true, vis[y] = true;

            if (x != y) p[y] = x;

            else ok = true;

        }

    }



    return 0;

}

你可能感兴趣的:(迷宫)