HDU 1829 (并查扩展)
锻炼对于未给予集合类型的数据的技巧,使用并查,按rank归并。
#include <iostream>
using namespace std;
typedef struct
{
long father;
long opper;
}Info;
const long MAXN=2100;
Info hash[MAXN];
inline void Make_Set()
{
long i;
for (i=0;i<MAXN;++i)
{
hash[i].father=hash[i].opper=i;
}
}
inline long Find(long x)
{
long r=x;
while (r!=hash[r].father)
{
r=hash[r].father;
}
while (r!=hash[x].father)
{
long j=hash[x].father;
hash[x].father=r;
x=j;
}
return r;
}
inline void Unition(long x,long y)
{
long fx=Find(x);
long fy=Find(y);
hash[fy].father=fx;
}
int main()
{
long T;
scanf("%ld",&T);
long b=1;
while (T--)
{
long N,M;
scanf("%ld %ld",&N,&M);
long i;
Make_Set();
bool doit=true;
for (i=0;i<M;++i)
{
long from,to;
scanf("%ld %ld",&from,&to);
if (!doit)
{
continue;
}
long f,t;
f=Find(from);
t=Find(to);
if (f==t)
{
doit=false;
}
if (hash[from].opper!=from)
{
Unition(t,hash[from].opper);
}
else
{
hash[from].opper=t;
}
if (hash[to].opper!=to)
{
Unition(f,hash[to].opper);
}
else
{
hash[to].opper=f;
}
}
printf("Scenario #%ld:\n",b);
++b;
if (doit)
{
puts("No suspicious bugs found!");
}
else
{
puts("Suspicious bugs found!");
}
puts("");
}
return 0;
}
using namespace std;
typedef struct
{
long father;
long opper;
}Info;
const long MAXN=2100;
Info hash[MAXN];
inline void Make_Set()
{
long i;
for (i=0;i<MAXN;++i)
{
hash[i].father=hash[i].opper=i;
}
}
inline long Find(long x)
{
long r=x;
while (r!=hash[r].father)
{
r=hash[r].father;
}
while (r!=hash[x].father)
{
long j=hash[x].father;
hash[x].father=r;
x=j;
}
return r;
}
inline void Unition(long x,long y)
{
long fx=Find(x);
long fy=Find(y);
hash[fy].father=fx;
}
int main()
{
long T;
scanf("%ld",&T);
long b=1;
while (T--)
{
long N,M;
scanf("%ld %ld",&N,&M);
long i;
Make_Set();
bool doit=true;
for (i=0;i<M;++i)
{
long from,to;
scanf("%ld %ld",&from,&to);
if (!doit)
{
continue;
}
long f,t;
f=Find(from);
t=Find(to);
if (f==t)
{
doit=false;
}
if (hash[from].opper!=from)
{
Unition(t,hash[from].opper);
}
else
{
hash[from].opper=t;
}
if (hash[to].opper!=to)
{
Unition(f,hash[to].opper);
}
else
{
hash[to].opper=f;
}
}
printf("Scenario #%ld:\n",b);
++b;
if (doit)
{
puts("No suspicious bugs found!");
}
else
{
puts("Suspicious bugs found!");
}
puts("");
}
return 0;
}