您的位置:首页 > 编程语言 > C语言/C++

hdu1272-小希的迷宫

2013-04-27 14:17 323 查看

小希的迷宫

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

#include<vector>
#include<list>
#include<map>
#include<set>
#include<deque>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<ctime>

using namespace std;
const int maxn=100005;
int fa[maxn],hav[maxn];
int flag;
int find( int x )
{
//	return fa[ x ] = x == fa[ x ] ? x :find( fa[ x ] );
while( x != fa[ x ])
x = fa[ x ];
return x;

}

void merge( int x1 ,int x2 )
{
int	x = find( x1 );
int y = find( x2 );
if( x != y )
fa[ x ] = y;
else
flag = 0;//同父节点,成环
}

int main()
{
int i,m,n;
while(cin>>m>>n)
{
if( m == -1 && n == -1 )
break;
if( m==0 && n==0 )
{
cout<<"Yes"<<endl;
continue;
}
for( i = 1; i < maxn; i++ )
{
fa[ i ] = i;
hav[i]=0;
}
hav[ m ] = hav[ n ] = 1;
flag = 1;
merge( m , n );
while( cin >> m >> n )
{
if( m == 0 && n == 0)
break;
merge( m , n );
hav[ m ] = hav[ n ] = 1;
}
int ans = 0;
for( i = 1;i < maxn; i++)
{
if( hav[ i ] && fa[ i ] == i )//判断根节点k数目
ans ++;
if( ans > 1 )
flag=0;
}
if( flag )
cout<<"Yes"<<endl;//这里也坑爹,用PUTS输出和下面一样的错误
else
cout<<"No"<<endl;
}
return 0;
}



这个是下面的代码提交,深表无语。。。。。。。。。。。。。。。。。。。。。。。。。

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

#include<vector>
#include<list>
#include<map>
#include<set>
#include<deque>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<ctime>

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  学习笔记 c++ 杭电