hoj1878

看是否所有定点的度都是偶数,并使用并查集看图是否连通。我开始因为没有判断是否连通而错了许多次。

View Code
   
     
#include < iostream >
#include
< cstdio >
#include
< cstdlib >
#include
< cstring >
using namespace std;

#define maxn 1005

int n, m, f[maxn], father[maxn];

int getanc( int a)
{
if (father[a] == a)
return a;
return father[a] = getanc(father[a]);
}

bool ok()
{
for ( int i = 0 ; i < n; i ++ )
if (f[i] % 2 != 0 || getanc(i) != getanc( 0 ))
return false ;
return true ;
}

int main()
{
// freopen("D:\\t.txt", "r", stdin);
while (scanf( " %d " , & n) != EOF && n != 0 )
{
scanf(
" %d " , & m);
for ( int i = 0 ; i < n; i ++ )
father[i]
= i;
memset(f,
0 , sizeof (f));
for ( int i = 0 ; i < m; i ++ )
{
int a, b;
scanf(
" %d%d " , & a, & b);
a
-- ;
b
-- ;
father[getanc(a)]
= getanc(b);
f[a]
++ ;
f[b]
++ ;
}
if (ok())
printf(
" 1\n " );
else
printf(
" 0\n " );
}
return 0 ;
}

你可能感兴趣的:(OJ)