poj1733 Parity game
好神的题啊
首先在意识上差分,那么条件就变成了x-1和y的奇偶性异同
现在就像是程序分析那题了
但是要输出哪里开始不对
神的是用带权并查集,假如相同设为0,不同设为1,那么同一并查集内的点通过异或运算就可以确定奇偶性的关系了
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> using namespace std; int m; struct node{int x,y,o;}a[11000]; int lslen,ls[21000]; void LSH() { for(int i=1;i<=m;i++) ls[++lslen]=a[i].x, ls[++lslen]=a[i].y; sort(ls+1,ls+lslen+1); lslen=unique(ls+1,ls+lslen+1)-ls-1; for(int i=1;i<=m;i++) { a[i].x=lower_bound(ls+1,ls+lslen+1,a[i].x)-ls; a[i].y=lower_bound(ls+1,ls+lslen+1,a[i].y)-ls; } } int fa[110000],d[110000]; int findfa(int x) { if(fa[x]==x)return x; int f=findfa(fa[x]); d[x]^=d[fa[x]];fa[x]=f; return fa[x]; } char ss[5]; int main() { int n; scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) { scanf("%d%d%s",&a[i].x,&a[i].y,ss+1); if(ss[1]=='o')a[i].o=1; else a[i].o=0; } LSH(); for(int i=0;i<=100000;i++)fa[i]=i,d[i]=0; for(int i=1;i<=n;i++) { int x=a[i].x-1,y=a[i].y; int fx=findfa(x),fy=findfa(y); if(fx!=fy) { fa[fx]=fy; d[fx]=d[x]^d[y]^a[i].o; } else if(d[x]^d[y]!=a[i].o){printf("%d\n",i-1);return 0;} } printf("%d\n",m); return 0; }
pain and happy in the cruel world.