zoukankan      html  css  js  c++  java
  • poj 1733 Parity game【hash+带权并查集】

    hash一下然后用带权并查集做模2下的前缀和

    #include<iostream>
    #include<cstdio>
    #include<map>
    #include<algorithm>
    using namespace std;
    const int N=100005;
    int n,m,f[N],s[N],ans,g[N],tot,has,x[N],y[N],z[N];
    char o[10];
    map<int,int>mp;
    int read()
    {
    	int r=0,f=1;
    	char p=getchar();
    	while(p>'9'||p<'0')
    	{
    		if(p=='-')
    			f=-1;
    		p=getchar();
    	}
    	while(p>='0'&&p<='9')
    	{
    		r=r*10+p-48;
    		p=getchar();
    	}
    	return r*f;
    }
    int zhao(int x)
    {
    	if(x==f[x])
    		return x;
    	int nw=zhao(f[x]);
    	s[x]=(s[x]+s[f[x]])%2;
    	return f[x]=nw;
    }
    int main()
    {
    	n=read(),m=read();
    	for(int i=1;i<=m;i++)
    	{
    		x[i]=read()-1,y[i]=read();
    		scanf("%s",o+1);
    		z[i]=(o[1]=='e')?0:1;
    		g[++tot]=x[i],g[++tot]=y[i];
    	}
    	sort(g+1,g+1+tot);
    	for(int i=1;i<=tot;i++)
    		if(i==1||g[i]!=g[i-1])
    			mp[g[i]]=++has;
    	for(int i=1;i<=m;i++)
    		x[i]=mp[x[i]],y[i]=mp[y[i]];
    	for(int i=1;i<=has;i++)
    		f[i]=i;
    	for(int i=1;i<=m;i++)
    	{
    		int fx=zhao(x[i]),fy=zhao(y[i]);
    		if(fx!=fy)
    		{
    			f[fy]=fx;
    			s[fy]=(s[x[i]]-s[y[i]]+z[i])%2;
    		}
    		else if((s[y[i]]-s[x[i]]+2)%2!=z[i])
    		{
    			ans=i-1;
    			break;
    		}
    	}
    	if(!ans)
    		ans=m;
    	printf("%d",ans);
    	return 0;
    }
    
  • 相关阅读:
    hdu 3577 线段树
    hdu 5316 Magician 线段树
    POJ3468 本来是一道线段树
    hdu 3183 st表
    hdu 5285 BestCoder Round #48 ($) 1002 种类并查集
    hdu 5282 序列计数
    zoj 2432 模板LCIS
    hdu 1052 贪心
    Angular实践----定制你自己的指令
    Angular实践----理解数据绑定过程
  • 原文地址:https://www.cnblogs.com/lokiii/p/9974570.html
Copyright © 2011-2022 走看看