#include
<
iostream
>
using
namespace
std;
#define
block 10000
#define
Hlen 6000
int
Len,N;
int
hash[
20000
];
int
root[
20000
];
int
Hash(
int
x){
int
ax
=
x
%
Hlen;
while
(hash[ax]
!=-
1
&&
hash[ax]
!=
x) ax
=
(ax
+
1
)
%
Hlen;
hash[ax]
=
x;
return
ax;
}
int
Find(
int
x){
if
(x
!=
root[x]) root[x]
=
Find(root[x]);
return
root[x];
}
void
Union(
int
a,
int
b){
int
x
=
Find(a),y
=
Find(b);
if
(x
!=
y)
root[x]
=
y;
}
int
main()
{
while
(
1
){
scanf(
"
%d
"
,
&
Len);
if
(Len
==-
1
)
break
;
getchar();
scanf(
"
%d
"
,
&
N); getchar();
memset(hash,
-
1
,
sizeof
(hash));
int
a,b;
char
c[
255
];
int
i;
for
(
int
i
=
0
;i
<
20000
;
++
i)
root[i]
=
i;
for
(i
=
0
;i
<
N;
++
i){
scanf(
"
%d%d%s
"
,
&
a,
&
b,c); getchar();
a
=
Hash(a
-
1
);
b
=
Hash(b);
if
(strcmp(c,
"
even
"
)
==
0
){
if
(Find(a)
==
Find(b
+
block))
break
;
Union(a,b);
Union(a
+
block,b
+
block);
}
else
{
if
(Find(a)
==
Find(b))
break
;
Union(a,b
+
block);
Union(a
+
block,b);
}
}
printf(
"
%d\n
"
,i);
for
(i
++
;i
<
N;
++
i)
gets(c);
}
return
0
;
}
posted on 2009-05-27 15:07
xfstart07 阅读(503)
评论(0) 编辑 收藏 引用 所属分类:
代码库