POJ 1703 Find them, Catch them 并查集,还是有点不理解
2024-09-04 23:25:28
题目不难理解,A判断2人是否属于同一帮派,D确认两人属于不同帮派。于是需要一个数组r[]来判断父亲节点和子节点的关系。具体思路可参考http://blog.csdn.net/freezhanacmore/article/details/8774033
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std; const int maxn=100005;
int f[maxn];
int r[maxn]; int Find_f(int x)
{
if (x != f[x])
{
int t=f[x];
f[x] = Find_f(f[x]);
r[x]=(r[x]+r[t])%2;
return f[x];
}
return f[x];
}
void Union(int x,int y)
{
int fx=Find_f(x);
int fy=Find_f(y);
f[fx]=fy;
r[fx]=(r[x]+1+r[y])%2;
}
int main()
{
//freopen("in.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
{
f[i]=i;
r[i]=0;
}
char c;
int crime1,crime2;
while(m--)
{
getchar();
scanf("%c%d%d",&c,&crime1,&crime2);
//printf("%c\n",c);
if(c=='D')
Union(crime1,crime2);
else if(c=='A')
{
if(Find_f(crime1)==Find_f(crime2))
if(r[crime1]!=r[crime2])
printf("In different gangs.\n");
else
printf("In the same gang.\n");
else
printf("Not sure yet.\n");
}
}
}
return 0;
}
最新文章
- Java学习笔记(03)
- 一个简易的四则运算单元...(15.12.15 BUG更新)
- js的规范写法ES5(自己以后按照这样写)
- 使用openssl生成RSA公私密钥
- TCP之心跳包实现思路
- 【BZOJ 3188】【Coci 2011】Upit Splay模板题
- (剑指Offer)面试题14:调整数组顺序使奇数位于偶数前面
- ASP.NET 学习博客
- Activity.startManagingCursor方法
- 浅谈print2flash的在线预览转换应用(原创)
- js 验证手机号 以及身份证正则表达式
- xcode Workspaces
- CSS 文件的4种引入方式
- [机器学习] Apriori算法
- 如何拼接FusionCharts的JSON格式的双轴图
- Cloud TPU Demos(TensorFlow 云 TPU 样例代码)
- PHP-MySQL基本操作
- React-理解高阶组件
- python基础(12)-包的导入&;异常处理
- LVDS时序分析