URAL 1003 Parity(并查集)
来源:互联网 发布:网络最火尴舞视频 编辑:程序博客网 时间:2024/06/05 20:55
题目链接 http://acm.timus.ru/problem.aspx?space=1&num=1003
思路参考 http://www.cnblogs.com/scau20110726/archive/2013/04/03/2998722.html
#include <iostream>#include <algorithm>#include <cstdio>#include <string>#include <cstring>using namespace std;const int maxn=10000+15;struct node{ int l,r; char str[15];}G[maxn];int a[maxn],id[maxn*2],cnt,n,m;void initial(){ cnt=0; for(int i=0;i<maxn*2;i++) id[i]=i;}void input(){ scanf("%d",&m); for(int i=0;i<m;i++) { scanf("%d %d %s",&G[i].l,&G[i].r,G[i].str); a[cnt++]=G[i].l; a[cnt++]=G[i].r; }}int Find(int x){ if(x!=id[x]) id[x]=Find(id[x]); return id[x];}void solve(){ a[cnt++]=0; sort(a,a+cnt); cnt=unique(a,a+cnt)-a; for(int i=0;i<m;i++) { int l=lower_bound(a,a+cnt,G[i].l)-a-1; int r=lower_bound(a,a+cnt,G[i].r)-a; int p=Find(l),pp=Find(l+cnt); int q=Find(r),qq=Find(r+cnt); if(G[i].str[0]=='e') { if(p==qq || q==pp) { printf("%d\n",i); return ; } id[p]=q,id[pp]=qq; } else { if(p==q || pp==qq) { printf("%d\n",i); return ; } id[p]=qq,id[pp]=q; } } printf("%d\n",m);}int main(){ while(scanf("%d",&n)!=EOF) { if(n==-1) break; initial(); input(); solve(); } return 0;}
0 0
- ural 1003 Parity 并查集
- URAL 1003 Parity(并查集)
- Ural 1003 Parity(并查集)
- poj1733 & Ural 1003 Parity Game (hash+并查集 )
- ural 1003. Parity(并查集)
- 【并查集】Parity
- Parity game 并查集
- Ural 1003 Parity
- 【并查集+hash】Parity game
- POJ 1733 Parity game(并查集)
- POJ 1733 Parity game (并查集)
- 【POJ】1733 Parity game 并查集
- poj-1733 Parity game 并查集
- POJ1733 - Parity game - 并查集
- [POJ 1733]Parity game[并查集]
- 51nod 1204:Parity 并查集
- poj1733 Parity game(并查集)
- POJ1733 parity game(并查集)
- UI 炫技要不得!设计师别求花俏,简单明了才最重要
- JUnit4
- Linux Readingpackagelists Error! 解决办法
- IONIC安装部署入门教程
- 有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数), 凡报到3的人退出圈子,问最后留下 的是原来第几号的那位。
- URAL 1003 Parity(并查集)
- 知识总结5-6章
- 关于指纹加密算法_MD5加密
- Ubuntu14.04下安装Hadoop2.4.0 (单机模式)
- 堆和栈的区别
- LeetCode 088 Merge Sorted Array
- android studio刚启动报错
- 项目里添加smarty功能步骤
- 1010. 一元多项式求导 (25)