zoukankan      html  css  js  c++  java
  • HDU1272 小希的迷宫(基础并查集)

    杭电的图论题目列表。共计500题,努力刷吧

    AC 64ms

    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    const int INF = 1e8;
    using namespace std;
    int father[100010];
    bool vis[100010];
    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<100010;i++)
    		{
    		father[i]=i;
    		vis[i]=0;
    		}
    }
    int main()
    {
    	int a,b;
    	while(scanf("%d%d",&a,&b)!=EOF)
    	{
    	    if(a==-1&&b==-1)
                break;
    	    int	flag=1,t=0;
             if(a==0 && b==0)
            {
                puts("Yes");
                continue;
            }
    		init();
    	    int	num = 0;
    		while(1)
    		{
    		    if(a==0&&b==0) break;
    		    if(flag)
                {
    			if(!vis[a]) num++;  //num记录点数
    			if(!vis[b]) num++;
    			vis[a]=1; vis[b]=1;
               if(Merge(a,b)==1)
    			  t++;              //t记录边数
                else
                    flag = 0;
                }
    		   scanf("%d%d",&a,&b);
    		}
    			if(num-t==1 &&flag == 1)//满足题意的仅仅能是 点数-边数==1
    				puts("Yes")
    			else
    				puts("No");
    	}
    	return 0;
    }
    


  • 相关阅读:
    游标cursor
    SQL: EXISTS
    LeetCode Reverse Integer
    LeetCode Same Tree
    LeetCode Maximum Depth of Binary Tree
    LeetCode 3Sum Closest
    LeetCode Linked List Cycle
    LeetCode Best Time to Buy and Sell Stock II
    LeetCode Balanced Binary Tree
    LeetCode Validate Binary Search Tree
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/6856372.html
Copyright © 2011-2022 走看看