并差集的应用
#include <stdio.h>

const int LEN = 2005;

struct
  {
int fa;
int dis;
}p[LEN];

void make ( int n )
  {
for ( int i=0; i<n; i++ )
 {
p[i].fa = -1;
p[i].dis = 0;
}
}

int find ( int a )
  {
if ( p[a].fa < 0 )
 {
return a;
}
int r = find ( p[a].fa );
p[a].dis += p[ p[a].fa ].dis;
p[a].fa = r;
return r;
}

void un ( int a, int b )
  {
int ra = find ( a );
int rb = find ( b );
p[rb].fa = ra;
p[rb].dis = p[a].dis + 1 - p[b].dis;
}

int main ()
  {
int t;
int m, n;
int a, b;
scanf ( "%d", &t );
for ( int count = 1; count <= t; count ++ )
 {
scanf ( "%d%d", &n, &m );
make ( n );
int flag = 1;
for ( int i=0; i<m; i++ )
 {
scanf ( "%d%d", &a, &b );
if ( flag )
 {
a --;
b --;
int ra = find ( a );
int rb = find ( b );
if ( ra != rb )
 {
un ( a, b );
}
else
 {
if ( !( ( p[a].dis + p[b].dis ) & 1 ) )
 {
printf ( "Scenario #%d:\nSuspicious bugs found!\n\n", count );
flag = 0;
}
}
}
}
if ( flag )
 {
printf ( "Scenario #%d:\nNo suspicious bugs found!\n\n", count );
}
}
return 0;
}

|
|
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
24 | 25 | 26 | 27 | 28 | 29 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 1 | 2 | 3 | 4 | 5 |
|
公告
决定从线程开始!!
常用链接
留言簿(6)
随笔档案
搜索
最新评论

阅读排行榜
评论排行榜
|
|