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
#include<iostream> #include<algorithm> #include<cstdio> #include<vector> #include<string> #include<queue> #include<map> #include<cmath> #include<cstring> using namespace std; const int maxn= 10005; typedef long long LL; int p[maxn],d[maxn]; void init(int n) { for(int i=1;i<=n;i++){ p[i]=i; d[i]=0; } } int findx(int x) { if(p[x]==x)return x; int root=findx(p[x]); d[x]+=d[p[x]]; d[x]%=2; return p[x]=root; } bool Union(int x,int y,int op) { int u=findx(x),v=findx(y); if(u!=v) { p[u]=v; d[u]=(d[x]-d[y]+op+2)%2; return true; } return d[x]==(op+d[y])%2==op; } int main() { map<int,int>q; int cnt=0,n,qus; scanf("%d",&n); scanf("%d",&qus); init(qus*2+4); int a,b,ans=-1; char cmd[5]; for(int i=0;i<qus;i++) { scanf("%d%d%s",&a,&b,cmd); b++; if(!q[a])q[a]=++cnt; if(!q[b])q[b]=++cnt; if(ans<0&&!Union(q[a],q[b],cmd[0]=='o')) { ans=i;break; } } printf("%d\n",ans>-1?ans:qus); return 0; }
原文地址:http://blog.csdn.net/acvcla/article/details/39010079