You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# include <bits/stdc++.h>
using namespace std ;
const int N = 40010 , B = N / 2 ;
// 简化版本的食物链
int n , m ;
// 无序离散化
unordered_map < int , int > S ;
int get ( int x ) {
if ( S [ x ] = = 0 ) S [ x ] = + + n ;
return S [ x ] ;
}
// 并查集
int p [ N ] ;
int find ( int x ) {
if ( p [ x ] ! = x ) p [ x ] = find ( p [ x ] ) ;
return p [ x ] ;
}
int main ( ) {
scanf ( " %d %d " , & n , & m ) ;
n = 0 ;
for ( int i = 1 ; i < N ; i + + ) p [ i ] = i ;
int res = m ;
for ( int i = 1 ; i < = m ; i + + ) {
int a , b ;
scanf ( " %d %d " , & a , & b ) ;
char t [ 100 ] ;
scanf ( " %s " , t ) ;
a = get ( a - 1 ) , b = get ( b ) ; // 计算出新的在并查集中的号
if ( t [ 0 ] = = ' e ' ) { // 偶数个1
if ( find ( a + B ) = = find ( b ) ) { // 如果奇偶性不同, 因为b与a+B相同
res = i - 1 ;
break ;
}
// join两个奇偶相同的集合
p [ find ( a ) ] = find ( b ) ;
p [ find ( a + B ) ] = find ( b + B ) ;
} else { // 奇数个1
if ( find ( a ) = = find ( b ) ) {
res = i - 1 ;
break ;
}
// join两个奇偶不相同的集合
p [ find ( a + B ) ] = find ( b ) ;
p [ find ( a ) ] = find ( b + B ) ;
}
}
printf ( " %d \n " , res ) ;
return 0 ;
}