zoukankan      html  css  js  c++  java
  • hdu1272 小希的迷宫

    题意:就是不存在环,而且各点相互连通

    分析:就是一颗树,树的条件,连通,不含圈,边恰好为n-1个,只要满足其中两个条件就可以了,这道题统计节点个数,可以用set,map统计,用并查集

    判断是否存在圈,只要注意空树也是正确答案就行了

    代码:

     
      2016/2/8
    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1e5+5;

    int p[maxn],v[maxn];

    void init(){
        memset(v,0,sizeof(v));
        for(int i=0;i<maxn;i++)
            p[i]=i;
    }

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

    int main(){
        int d=0,maxid=0,x,y;
        bool ok=1;
        init();
        while(~scanf("%d%d",&x,&y)){
            if(x==0&&y==0){
                if((ok&&d==maxid-1)||d==0)
                    puts("Yes");
                else
                    puts("No");
                maxid=d=0;init();ok=1;continue;
            }
            if(x==-1&&y==-1)
                return 0;
            d++;
            if(!ok)
                continue;
            if(!v[x]){
                maxid++;
                v[x]=1;
            }
            if(!v[y]){
                maxid++;
                v[y]=1;
            }
            int nx=find(x);
            int ny=find(y);
            if(nx!=ny)
                p[nx]=p[ny];
            else
                ok=0;
        }
    }

  • 相关阅读:
    leetcode Super Ugly Number
    leetcode Find Median from Data Stream
    leetcode Remove Invalid Parentheses
    leetcode Range Sum Query
    leetcode Range Sum Query
    leetcode Minimum Height Trees
    hdu 3836 Equivalent Sets
    hdu 1269 迷宫城堡
    hud 2586 How far away ?
    poj 1330 Nearest Common Ancestors
  • 原文地址:https://www.cnblogs.com/jihe/p/5185215.html
Copyright © 2011-2022 走看看