hdu 1272 小希的迷宫(并查集)

/*
2011-9-4
author:BearFly1990
*/
package acm.hdu.tests;

import java.io.BufferedInputStream;
import java.util.Arrays;
import java.util.Scanner;

public class HDU_1272 {
    private static Node[] maze = new Node[100001];
    private static int[] visited = new int[100001];
    private static int maxn = 0;               
    private static int minn = 1000000;
    public static void main(String[] args) {
        Scanner in = new Scanner(new BufferedInputStream(System.in));
        for(int i = 0; i <100001; i++){
            maze[i] = new Node();
        }
        while(in.hasNext()){
            int a = in.nextInt();
            int b = in.nextInt();
            if(a == -1 && b == -1)break;
            if(a == 0 && b == 0){
                System.out.println("Yes");
                continue;
            }
            inits();
            boolean isTrue = true;
            
            while(true){
                if ( a < minn )minn = a;
                if ( b < minn )minn = b;
                if ( a > maxn )maxn = a;
                if ( b > maxn )maxn = b;
                visited[a] = visited[b] = 1;
                int aFather = findFather(a);
                int bFather = findFather(b);
                
                if(aFather == bFather)isTrue = false;
                else{
                    merge(aFather,bFather);
                }
                
                a = in.nextInt();
                b = in.nextInt();
                if(a == 0 && b == 0)break;
            }
            
            if(!isTrue){
                System.out.println("No");  
            }else{
                int count = 0;
                for(int i = minn; i <= maxn; i++){
                    if(visited[i]==1 && maze[i].parent == i ){
                        count++; 
                    }
                }
                if(count == 1){
                    System.out.println("Yes");
                }else{
                    System.out.println("No");
                }
            }
        }
    }
    public static void inits(){
        Arrays.fill(visited, 0);
        for ( int i = 1; i < 100001; i ++ )
        {
         maze[i].parent = i;
         maze[i].weight = 1;
        } 
        maxn =-1;
        minn = 1000000;
    }
    public static int findFather(int i){
        while(i != maze[i].parent)
            i = maze[i].parent;
        return i;
    }
    
    public static void merge(int a , int b){
        if(maze[a].weight == maze[b].weight){
            maze[a].weight = b;
            maze[b].parent = a;
        }else if(maze[a].weight > maze[b].weight){
            maze[b].parent = a;
        }else{
            maze[a].parent = b;
        }
    }
    
    public static class Node{
        int parent = 0;
        int weight = 0;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值