zoukankan      html  css  js  c++  java
  • hdu 1272 小希的迷宫(并查集 最小生成树)

    http://acm.hdu.edu.cn/showproblem.php?pid=1272

    这题要求任意两个房间都相通又不能有环 

    即通过并查集求出是否构成最小生成树

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define maxn 100000
    int fa[maxn+10];
    int num[maxn+10];
    int vis[maxn+10];
    int ans,coun;
    int find(int x)
    {
        return fa[x]=(x==fa[x]?x:find(fa[x]));
    }
    void fun(int x,int y)
    {
        int fx=find(x);
        int fy=find(y);
        if(fx==fy)
        {
            ans=0;
            return ;
        }
        if(vis[x]==0) {vis[x]++;coun++;}
        if(vis[y]==0) {vis[y]++;coun++;}
        fa[fx]=fa[fy];
        num[fy]+=num[fx];
        if(num[fy]>ans) ans=num[fy];
    }
    int main()
    {
        int n;
        int l,r;
        int i,j,k;
        while(scanf("%d%d",&l,&r)!=EOF)
        {
            if(l==0&&r==0) {printf("Yes
    "); continue;}
            if(l==-1&&r==-1) break;
            ans=1;
            coun=0;
            for(i=0;i<=maxn;i++) {fa[i]=i;num[i]=1;}
            memset(vis,0,sizeof(vis));
            while(l!=0||r!=0)
            {
                if(ans)
                fun(l,r);
                scanf("%d%d",&l,&r);
            }
            if(ans==coun) printf("Yes
    ");
            else printf("No
    ");
        }
        return 0;
    }
  • 相关阅读:
    unity3D相机缓慢看向目标物体
    设计原则
    unity3D中GUI标题内文字滚动效果
    python3.6中安装PyQt报错
    codeforces 515B.Drazil and His Happy Friends
    HDU 1029 Ignatius and the Princess IV
    POJ 1052 Plato's Blocks
    Uva220 Othello
    uva201 Squares
    uva1587 Box
  • 原文地址:https://www.cnblogs.com/sola1994/p/4262058.html
Copyright © 2011-2022 走看看