/*
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;
}
}
hdu 1272 小希的迷宫(并查集)
最新推荐文章于 2021-11-09 21:23:16 发布