ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1003. Parity

Who can help me?WA#1
Posted by hywwqq 20 Nov 2012 17:52
#include <iostream>
#include <cstdio>
#include <deque>
#include <map>
#include <string>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <functional>
using namespace std;
const int maxn=20002;
int Hlen=6000;
int father[maxn];
int flag[maxn];
int getfather(int x)
{
    int tmp=father[x];
    if(x==father[x])
        return x;
        else
        {
            father[x]=getfather(father[x]);
            return father[x];
        }
}
void Union(int x,int y)
{
    int fx=getfather(x);
    int fy=getfather(y);
    if(fx!=fy)
      father[fy]=fx;
}
void init()
{
    for(int i=0;i<maxn;i++)
        father[i]=i;
}
int mp(int x)
{
    int ax=x%Hlen;
    if(flag[ax]!=-1&&flag[ax]!=x)
        ax=(ax+1)%Hlen;
    flag[ax]=x;
    return ax;
}
int main()
{
    int len,N,i;
    int level=10000;

    while(1)
    {
    scanf("%d",&len);
    if(len==-1)
        break;
    scanf("%d",&N);

    init();
    memset(flag,-1,sizeof (flag));
    for(i=0;i<N;i++)
    {
        int a,b;
        char str[10];
        scanf("%d%d%s",&a,&b,str);
        a=mp(a-1);
        b=mp(b);
        if(str[0]=='e')
        {
            if(getfather(a)==getfather(b+level))
                break;
            Union(a,b);
            Union(a+level,b+level);
        }
        else
        {
            if(getfather(a)==getfather(b))
                break;
            Union(a,b+level);
            Union(a+level,b);
        }
    }
    char str1[100];

    int m=i;
    for(i;i<N;i++)
        gets(str1);
    printf("%d\n",m);

    }
    system("pause");
    return 0;
}