POJ 1733

#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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值