题意: 有 N 个人分属于两个帮派,对应两种操作:

A   X Y      询问x,y 是否属于一个帮派,或两者关系不能确定。

D   X Y      X和Y 分属不同帮派

分析: 感觉就是简化版的食物链...

方法一:

加一个数组 r[i]

r[i] = 0  表示 i 与祖先属于同一个帮派

r[i] = 1  表示 i 与祖先属于不同帮派

View Code

#include<stdio.h>
#include<string.h>
int f[100005];
int r[100005];
int find(int x)
{int s;if(f[x]==-1)return x;else s=find(f[x]);r[x]=(r[x]+r[f[x]])%2;f[x]=s;return s;
}
void join(int x,int y)
{int fx=find(x);int fy=find(y);if(fx!=fy){f[fy]=fx;r[fy]=(r[x]+r[y]+1)%2;}
}
int main()
{char c[2];int t,i,a,b,n,m,fx,fy;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);for(i=1;i<=n;i++){f[i]=-1;r[i]=0;}//    scanf("%c",&c);while(m--){scanf("%s%d%d",c,&a,&b);if(c[0]=='A'){fx=find(a);fy=find(b);if(fx==fy){if(r[a]==r[b])printf("In the same gang.\n");else printf("In different gangs.\n");}else printf("Not sure yet.\n");}else join(a,b);//    scanf("%c",&c);
        }}return 0;
}

方法二:

对于每一个人 i    ,都假设 i + n  和他对立,假如 j 和 i+n 对立,那么 j 和 i 必属与于同一个派别,若 j 和 i+n属于同一个集合,那么i 和 j 一定对立。

View Code

#include<stdio.h>
#include<string.h>
int f[200005];
int find(int x)
{return f[x]==x?x:(f[x]=find(f[x]));
}
void join(int x,int y)
{int fx=find(x);int fy=find(y);if(fx!=fy)f[fy]=fx;
}
int main()
{char s[2];int t,n,m,a,b,fx,fy,i;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);for(i=1;i<=2*n;i++)f[i]=i;while(m--){scanf("%s%d%d",s,&a,&b);if(s[0]=='A'){if(find(a+n)==find(b))printf("In different gangs.\n");else if(find(a)==find(b))printf("In the same gang.\n");else printf("Not sure yet.\n");}else{join(a,b+n);join(b,a+n);}}}return 0;
}

转载于:https://www.cnblogs.com/dream-wind/archive/2012/08/07/2626813.html

POJ 1703 Find them, Catch them【并查集】相关推荐

  1. POJ 1703 Find them, Catch them(并查集高级应用)

    POJ 1703 Find them, Catch them(并查集高级应用) 手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/sunc ...

  2. POJ 1703 Find them, Catch them 并查集

    题意:给你t组数据,每组数据给你编号为1-n的坏人,这些坏人要么属于团伙A,要么属于团伙B,然后给你m次操作: A操作:询问x和y是不是同一个团伙 D操作:告诉你x和y不是同一个团伙 思路:和POJ ...

  3. POJ 1703 Find them, Catch them(路径压缩并查集)

    POJ 1703 Find them, Catch them(路径压缩并查集) 2014年03月11日 20:13:54 阅读数:881 POJ 1703 Find them, Catch them( ...

  4. POJ 1417 True Liars(路径压缩并查集+DP背包问题)

    POJ 1417 True Liars(路径压缩并查集+DP背包问题) http://poj.org/problem?id=1417 题意: 给出p1+p2个人,其中p1个是好人,p2个是坏人.然后有 ...

  5. POJ 1703 Find them, Catch them 种类并查集

    题意 给出一堆点和关系 D为两点不同集合 A为查询两点是否不同集合 n<=1e5 code #include<cstdio> #include<iostream> #in ...

  6. POJ 1703 Find them, Catch them

    简单带权并查集0,1关系 //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio& ...

  7. POJ1703 Find them, Catch them 并查集

    点击打开链接 Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 50757   Ac ...

  8. POJ - 2513 Colored Sticks(字典树+并查集+欧拉回路)

    题目链接:点击查看 题目大意:给出n个木棍,问若两两相连,最终能否构成一根长直木棍,相连的规则是两个木棍的相接端点的颜色需要保持相同 题目分析:关于这个题目,我们可以将每个木棍视为一条边,每个木棍的两 ...

  9. POJ 3694 (tarjan缩点+LCA+并查集)

    好久没写过这么长的代码了,题解东哥讲了那么多,并查集优化还是很厉害的,赶快做做前几天碰到的相似的题. 1 #include <iostream> 2 #include <algori ...

  10. POJ 3694 Network ★(边双连通分量+并查集缩点+LCA)

    [题意]一个无向图可以有重边,下面q个操作,每次在两个点间连接一条有向边,每次连接后整个无向图还剩下多少桥(每次回答是在上一次连边的基础之上) [分析]好题,做完后涨了很多姿势~ 普通做法当然就是每加 ...

最新文章

  1. 【面向对象】第一单元总结——表达式求导
  2. Python 之glob模块
  3. ArcEngine 添加字段
  4. Tensorflow C3D完成视频动作识别
  5. 重装linux服务器简易流程
  6. Java中的synchronized
  7. VALSE学习(十八):复杂视频的深度高效分析与理解方法
  8. Windows与Linux下查看占用端口的进程
  9. java面向对象程序设计
  10. JS基础知识思维导图
  11. android 5.0设备 外接键盘 输入中文
  12. 第五届山东ACM大赛汇总
  13. cfree编译报错[Error] g++.exe: 5\mingw\lib\: No such file or directory
  14. 看了这个你也可以做SYSLINUX启动光盘
  15. 动态更新 fqdn ptr linux,DNS简单概念 一
  16. 国家开放大学2021春1021劳动与社会保障法题目
  17. 使用maven编译打包用javac还是eclipse的jdt的问题
  18. java excel 单元格类型,POI Excel 单元格内容类型判断并取值
  19. 最新精华版申请苹果开发者账号-企业版
  20. Reflected File Download Attack

热门文章

  1. Microsoft Excel软件打开文件出现文件的格式与文件扩展名指定格式不一致?
  2. linux命令:grep
  3. Mapreuduce实现网络数据包的清洗工作
  4. 为什么工业控制系统需要安全防护?
  5. 依赖注入容器Unity Application Block快速入门
  6. 完全理解python迭代对象_完全理解Python迭代对象、迭代器、生成器
  7. 互联网协议 — IPv4 — 分片与重组
  8. Service Mesh — APIGW vs ServiceMesh
  9. 删除文件提示“您需要权限才能执行此操作”如何解决
  10. 布线时其他区域变黑、高亮Net时其他区域太黑