hdu1272-小希的迷宫 (二)

2014-11-24 02:42:05 · 作者: · 浏览: 3
;
merge(m,n);
max = n > m n : m;
//int c;
while( ( m | n ) && ( scanf( "%d%d" ,& m, &n ), m | n ) )//当n和m均为0时,不执行,在输入前面和输入数据后面均进行判断
{
merge( m , n );
t=hav[ m ] = hav[ n ] = 1;
int c = n > m n : m;
max = c > max c : max;
}
int c = 0;
if( !t )//防止恶心数据,当一开始就输入0 0时,特殊情况
{
printf( "Yes\n" );
continue;
}
//int ans = 0;
for( int i = 0; i <= max; i++)//看有没有全部连通
if( hav[ i ] )//判断是否有这条路
if( fa[ i ] == i )
++c;
if( c != 1 )
f=1;
if( !f )
puts( "Yes" );
else
puts( "No" );
}
return 0;
}

// File Name: hdu1272.cpp
// Author: rudolf
// Created Time: 2013年04月27日 星期六 13时32分01秒

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;
const int maxn=100005;
int fa[maxn],hav[maxn];
int f;
int find( int x )
{
return fa[ x ] = x == fa[ x ] x :find( fa[ x ] );
}

void merge( int x1 ,int x2 )
{
int x = find( x1 );
int y = find( x2 );
if( x != y )
fa[ x ] = y;
else
if( x1 != x2 )//防止(1 1) 且判断两点之间是否只有一条通路
f = 1;
}

int main()
{
int max;
int m,n;
while( scanf( "%d%d" ,& m ,& n ) && m + n != -2 )
{
int t=0;
memset( hav , 0 , sizeof ( hav ) );
for( int i = 0; i <= maxn; i++ )
fa[ i ] = i;
//int t,max;

f = max = 0;
hav[ n ] = hav[ m ] = 1;
merge(m,n);
max = n > m n : m;
//int c;
while( ( m | n ) && ( scanf( "%d%d" ,& m, &n ), m | n ) )//当n和m均为0时,不执行,在输入前面和输入数据后面均进行判断
{
merge( m , n );
t=hav[ m ] = hav[ n ] = 1;
int c = n > m n : m;
max = c > max c : max;
}
int c = 0;
if( !t )//防止恶心数据,当一开始就输入0 0时,特殊情况
{
printf( "Yes\n" );
continue;
}
//int ans = 0;
for( int i = 0; i <= max; i++)//看有没有全部连通
if( hav[ i ] )//判断是否有这条路
if( fa[ i ] == i )
++c;
if( c != 1 )
f=1;
if( !f )
puts( "Yes" );
else
puts( "No" );
}
return 0;
}