poj 1733 并查集
来源:互联网 发布:git mac客户端下载 编辑:程序博客网 时间:2024/05/21 17:32
首先将数据离散化,然后r[x]表示x节点与根节点关系,左闭右开,这样每次输入时y++,求关系时候异或就行。每次合并时让大节点合并到小节点上去。
#include<stdio.h>#include<iostream>#include<string.h>#include<algorithm>using namespace std;const int nMax=10005;int p[nMax],r[nMax],x[nMax],y[nMax],ty[nMax],sot[nMax];int L,M,cnt;int find(int x){ if(p[x]==x) return x; int par=find(p[x]); r[x]=(r[x]^r[p[x]]); return p[x]=par;}void merge(int x,int y,int tye){ int a=find(x),b=find(y); if(a<b) { p[b]=a; r[b]=(r[x]^r[y]^tye); } else { p[a]=b; r[a]=(r[x]^r[y]^tye); }}int bi(int l,int r,int a){ while(l<=r) { int mid=(l+r)>>1; if(sot[mid]==a) return mid; else if(sot[mid]>a) r=mid-1; else l=mid+1; }}void init(){ for(int i=1;i<=cnt;i++) p[i]=i,r[i]=0;}int main(){// freopen("test.txt","r",stdin); scanf("%d%d",&L,&M); for(int i=0;i<M;i++) { char s[10]; scanf("%d%d%s",&x[i],&y[i],s); if(s[0]=='e') ty[i]=0; else ty[i]=1; if(x[i]>y[i]) swap(x[i],y[i]); sot[++cnt]=x[i];sot[++cnt]=y[i]; } sort(sot+1,sot+cnt+1); cnt=0; for(int i=1;i<=2*M;i++) if(sot[i]!=sot[i-1]) sot[++cnt]=sot[i]; int ans=-1; init(); for(int i=0;i<M;i++) { int a=bi(1,cnt,x[i]),b=bi(1,cnt,y[i])+1; int pa=find(a),pb=find(b); if(pa==pb && (r[a]^r[b])!=ty[i]) { ans=i; break; } else if(pa!=pb) merge(a,b,ty[i]); } if(ans==-1) ans=M; printf("%d\n",ans); return 0;}
- poj 1733 并查集
- poj 1733 并查集
- POJ 1733并查集
- Poj 并查集
- poj并查集
- POJ 并查集
- POJ 1733 并查集 偏移向量
- POJ 1733 Parity game(并查集)
- POJ 1733 Parity game (并查集)
- 【POJ】1733 Parity game 并查集
- poj-1733 Parity game 并查集
- poj 1733带权并查集
- poj 1733(带权并查集)
- [POJ 1733]Parity game[并查集]
- poj 1733_Parity game_并查集
- POJ 1733 Parity game(并查集)
- poj 2524 并查集
- POJ 2236 并查集
- 如何不让php自动把×换成×号
- jquery插件大全,开发记点网站很重要啊
- VisionMobile:2013年移动开发者经济报告(八):第二章 开发工具图景(上)
- 【Android开发学习26】界面控件之选项组件(RadioGroup和CheckBox)
- 在ASP.NET下使用jQuery UI下的Dialog,服务器端按钮无响应的问题。
- poj 1733 并查集
- data too long for column 'spath' at row 1
- Unity3D之使用 C#合成解析XML与JSON
- Increase entropy on a 2.6 kernel linux box
- hibernate杂谈
- Delphi——Trunc函数精度的问题
- OpenStack相关资源
- 刚开始走向工作岗位初遇到的一些问题
- 了解目标攻击:我们真正对抗的是什么