题目不难理解,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;
}

最新文章

  1. Java学习笔记(03)
  2. 一个简易的四则运算单元...(15.12.15 BUG更新)
  3. js的规范写法ES5(自己以后按照这样写)
  4. 使用openssl生成RSA公私密钥
  5. TCP之心跳包实现思路
  6. 【BZOJ 3188】【Coci 2011】Upit Splay模板题
  7. (剑指Offer)面试题14:调整数组顺序使奇数位于偶数前面
  8. ASP.NET 学习博客
  9. Activity.startManagingCursor方法
  10. 浅谈print2flash的在线预览转换应用(原创)
  11. js 验证手机号 以及身份证正则表达式
  12. xcode Workspaces
  13. CSS 文件的4种引入方式
  14. [机器学习] Apriori算法
  15. 如何拼接FusionCharts的JSON格式的双轴图
  16. Cloud TPU Demos(TensorFlow 云 TPU 样例代码)
  17. PHP-MySQL基本操作
  18. React-理解高阶组件
  19. python基础(12)-包的导入&amp;异常处理
  20. LVDS时序分析

热门文章

  1. python类变量的分类和调用方式
  2. 运维电子书PDF汇总
  3. Linux进阶之Linux中的标准输入输出
  4. 10.8 ss:查看网络状态
  5. 201871030136-颜静 实验三 结对项目—《D{0-1}KP 实例数据集算法实验平台》项目报告
  6. 前端基础——js数据类型及判断方法
  7. 树莓派3B+常用操作
  8. AI算子列表
  9. 向Relay添加算子
  10. 10分钟内基于gpu的目标检测