poj1703(基于分组)
来源:互联网 发布:全球域名注册量排名 编辑:程序博客网 时间:2024/06/15 18:06
一开始拿到这题不知道该怎么去做,想用opposite[i]来保存相对立的数,最后做出来连样例都跑不起来,查说有两种做法:
一:分组,把1~n看成一组,把n+1~2n看成相对立的另一组;
二:用并查集的便宜向量来做;
先给出用分组来做的;
#include<cstdio>const int maxn=1e5+5;int p[maxn<<1];int find(int x){ return p[x]==x?x:p[x]=find(p[x]);}int main(){ int T; scanf("%d",&T); while(T--){ int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=2*n;i++)p[i]=i; int a,b; char s[2]; while(m--){ scanf("%s%d%d",s,&a,&b); int x1=find(a),y1=find(a+n); int x2=find(b),y2=find(b+n); if(s[0]=='A'){ if(x1!=x2&&y1!=x2)printf("Not sure yet.\n");//y1==x2表示相对 else if(x1==x2)printf("In the same gang.\n"); else printf("In different gangs.\n"); } else if(s[0]=='D'){ p[y1]=x2; p[y2]=x1; } } } return 0;}
0 0
- poj1703(基于分组)
- poj1703
- POJ1703
- POJ1703
- poj1703
- poj1703
- poj1703
- poj1703
- poj1703
- poj1703
- poj1703
- poj1703
- poj1703
- POj1703
- poj1703(种类并查集)
- Find them, Catch them (poj1703)
- 见微知著----POJ1703(并查集)
- hdu1829基于分组
- PAT Basic Level 1011. A+B和C(15)
- super关键字
- infobright优化-group by 字段在过程再次处理会很耗时
- Java关键字介绍之final
- Git修改前一次提交的方法(特别注意保持Change-Id不变)
- poj1703(基于分组)
- RTL Layout Support
- PHP中的Array2String & String2Array
- Seek the Name, Seek the Fame KMP
- ubuntu Terminal 路径太长 vi 时出现字母
- Cracking the coding interview--Q11.1~Q11.6
- Ant打包工具介绍
- R2009b 版本安装后显示的错误
- dsa fas fas