点击打开链接题目链接
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 6249 | Accepted: 2413 |
Description
Input
Output
Sample Input
10 5 1 2 even 3 4 odd 5 6 even 1 6 even 7 10 odd
Sample Output
3
给出a-b范围内有偶数或者奇数个1
找出最先错误的那句话
离散可以取余10000然后如果有相同的余数 建立邻接表
代码:
#include<cstdio>
#include<cstring>
int father[10010];
struct Node
{
int name;
int next;
}node[10010];
int head[10010];
int res[10010];
int t;
int find(int x)
{
if(x==father[x])
return x;
int t=father[x];
father[x]=find(father[x]);
res[x]=(res[x]+res[t])%2;
return father[x];
}
int addname(int x)
{
int xx=x%10010;
for(int e=head[xx];e!=-1;e=node[e].next)
{
if(node[e].name==x)
{
return e;
}
}
node[t].next=head[xx];
head[xx]=t;
node[t].name=x;
return t++;
}
int main()
{
int len,n;
int flag,ans;
int i;
int a,b;
char str[10];
int x,y;
int fx,fy;
int w;
while(scanf("%d %d",&len,&n)!=EOF)
{
flag=0;
t=1;
for(i=1;i<=2*n;i++)
{
father[i]=i;
res[i]=0;
}
ans=0;
memset(head,-1,sizeof(head));
for(i=1;i<=n;i++)
{
scanf("%d %d %s",&a,&b,str);
if(flag)
continue;
if(str[0]=='e')
{
w=0;
}
else
w=1;
x=addname(a-1);
y=addname(b);
// printf("x = %d y = %d\n",x,y);
fx=find(x);
fy=find(y);
// printf("fx = %d fy = %d\n",fx,fy);
if(fx!=fy)
{
if((res[x]==res[y])==w)
res[fx]=1;
else res[fx]=0;
father[fx]=fy;
}
else
{
if((res[x]!=res[y]&&w==0)||(res[x]==res[y]&&w==1))
{
ans=i-1;
flag=1;
}
}
// printf("res[x] = %d res[y] = %d w = %d \n",res[x],res[y],w);
}
if(ans==0)
{
printf("%d\n",n);
}
else
{
printf("%d\n",ans);
}
}
return 0;
}
poj 1733 Parity game 并查集 离散化,布布扣,bubuko.com
原文:http://blog.csdn.net/qq_16843991/article/details/38584359