zoukankan      html  css  js  c++  java
  • HDU1325 &&poj1308 基础并查集

    和上一道小希的迷宫差点儿相同,可是在HDU上提交一直WA,POJ过了

    HDU的数据太强了吧,强的我感觉数据有问题

    题意:输入若干对点,推断是否是一颗树,转化过来也就是是否存在环

    点数-边数=1,还要推断每一个点的入度都<=1


    POJ AC代码

    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    const int INF = 1e8;
    using namespace std;
    int father[1010];
    bool vis[1010];
    int rudu[1010];
    int findx(int r)
    {
        int i = r,j;
        while(father[r]!=r)
        {
             r=father[r];
        }
    
        while(father[i]!=r)
        {
            j = father[i];
            father[i] = r;
            i = j;
        }
        return r;
    }
    
    bool Merge(int x,int y)
    {
        int fx,fy;
        fx=findx(x);
        fy=findx(y);
        if(fx!=fy)
    	{
    	 father[fx]=fy;
    	 return 1;
    	}
    	else
    	return 0;
    }
    void init()
    {
        for(int i=0;i<1010;i++)
    		{
    		father[i]=i;
    		vis[i] = 0 ;
    		rudu[i] = 0;
    		}
    }
    int main()
    {
    	int a,b,c = 0;
    	while(scanf("%d%d",&a,&b)!=EOF)
    	{
    	    c++;
    
    	    if(a<0&&b<0)
                break;
    	    int	flag=1,t=0;
             if(a==0 && b==0)
            {
                printf("Case %d ",c);
                puts("is a tree.");
                continue;
            }
    		init();
    	    int	num = 0;
    		while(1)
    		{
    		    if(a==0&&b==0) break;
    		    if(flag==1)
                {
    			if(!vis[a]) {num++; } 
    			if(!vis[b]) {num++; rudu[b]++;}
    			if(rudu[b]>1)
                    flag = 0;
    			vis[a]=1; vis[b]=1;
               if(Merge(a,b)==1)
               {
                    t++;    
               }
                else
                    flag = 0;
                }
    		   scanf("%d%d",&a,&b);
    		}
    		printf("Case %d ",c);
    			if(num-t==1 &&flag == 1)
    				puts("is a tree.");
    			else
    				puts("is not a tree.");
    	}
    	return 0;
    }
    


  • 相关阅读:
    ODI ORA-00932: 数据类型不一致: 应为 -, 但却获得 CLOB
    oracle 执行计划简介
    oracle job定时执行存储过程详解
    ODI 目标表主键有序列的同步处理
    ODI 同义词问题
    U盘安装redhat Linux
    ODI ora_01653 表空间无法扩展
    C#使用JSON相关
    常用查询汇总
    EXCEL中汉字转拼音
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4269597.html
Copyright © 2011-2022 走看看