#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
using namespace std;
#define maxn 1000007
#define LL long long
struct node
{
LL data;
int next;
} hashta[1000100];
int head[maxn], num;
int def[1000100], fa[1000100];
void init(int n)
{
num = 0;
memset(head, -1, sizeof(head));
memset(def, 0, sizeof(def));
for( int i = 0; i <= maxn; i++)
fa[i] = i;
}
LL gethash( LL n)
{
return n % maxn;
}
int try_to_insert(LL n)
{
LL h = gethash(n);
for( int i = head[h]; i != -1; i = hashta[i].next)
if(hashta[i].data == n)
return i;
hashta[num].data = n;
hashta[num].next = head[h];
head[h] = num++;
int res = num - 1;
return ( res );
}
int find( int x)
{
if(fa[x] == x)
return x;
int tem = find(fa[x]);
def[x] = (def[x]+ def[fa[x]])%2;
return fa[x] = tem;
}
void unio( int x, int y, int fx, int fy, int val)
{
fa[fx] = fy;
def[fx] = (def[y]+def[x]+val)%2;
return ;
}
int main()
{
int n, m;
scanf("%d %d", &n, &m );
init(n);
LL a, b;
char kind[10];
for( int i = 0; i < m ; i++)
{
scanf("%lld%lld%s",&a, &b, kind);
a--;
int ha = try_to_insert(a);
int hb = try_to_insert(b);
int fx = find(ha);
int fy = find(hb);
int v = 0;
if(kind[0] == 'o')
v = 1;
if(fx != fy)
unio(ha, hb, fx, fy, v);
if(fx == fy )
{
if((def[ha] + def[hb])%2 != v)
{
printf("%d\n",i);
return 0;
}
}
}
printf("%d\n",m);
return 0;
}
POJ 1733
最新推荐文章于 2019-02-11 13:11:31 发布