Find them, Catch them

好久没有做并查集的题,竟然快把并查集忘完了。

题意:大致是有两个监狱,n个犯人,m次操作,每次操作可以是查询也可以是确定两个人是否在同一个监狱里。

思路:其实这题完全可能做出来,结果就是没做出来。理清思路还是很好想的,我们用一个并查集来把能够确定在同一监狱里的人放在一个集合里,用一个diff数组把确定不同监狱的两个犯人分别与对方不同监狱的犯人放在一个集合里,就是这么简单。

const int N=1e5+10;
int f[N],dif[N];
char s[5];
void init()
{
for(int i=0;i<N;i++) f[i]=i;
memset(dif,-1,sizeof(dif));
}
int find(int x)
{
return f[x]==x?x:f[x]=find(f[x]);
}
int main()
{
int t,n,m;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
int u,v;
while(m--)
{
scanf("%s%d%d",s,&u,&v);
if(s[0]=='A')
{
if(find(u)==find(v)) printf("In the same gang.\n");
else if(find(dif[u])==find(v)||find(dif[v])==find(u)) printf("In different gangs.\n");
else printf("Not sure yet.\n");
}
else
{
if(dif[u]==-1) dif[u]=v;
if(dif[v]==-1) dif[v]=u;
int uu=find(dif[u]),vv=find(dif[v]);
f[uu]=find(v);
f[vv]=find(u);
}
}
}
return 0;
}

POJ1703-Find them, Catch them 并查集构造的更多相关文章

  1. poj1703 Find them, Catch them 并查集

    poj(1703) Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26992   ...

  2. POJ-1703 Find them, Catch them(并查集&数组记录状态)

    题目: The police office in Tadu City decides to say ends to the chaos, as launch actions to root up th ...

  3. HDU-1829 A Bug's Life。并查集构造,与POJ1709异曲同工!

    A Bug's Life                                                     Find them, Catch them 都是并查集构造的题,不久前 ...

  4. POJ 1703 Find them, catch them (并查集)

    题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2  D 3 4 D 5 6...这就至少有3个集合了.并且 ...

  5. poj1703--Find them, Catch them(并查集应用)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32073   Accepted: ...

  6. POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集

    POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...

  7. POJ 1703 Find them, Catch them 并查集的应用

    题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...

  8. poj1703_Find them, Catch them_并查集

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42451   Accepted: ...

  9. poj.1703.Find them, Catch them(并查集)

    Find them, Catch them Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I6 ...

随机推荐

  1. [在读]HTML5程序设计(第二版)

    去年买的,看了30%不到,之后一直是搁置状态,内容还不错,确确实实纯粹讲H5的.

  2. nodejs+multiparty 文件上传

    通过表单提交上传文件:     html代码 <form action="/uploadFile" method="post" enctype=" ...

  3. Java基础50题test4—分解质因数

    [分解质因数] 题目:将一个正整数分解质因数.例如:输入 90,打印出 90=2*3*3*5. 程序分析:对 n 进行分解质因数,应先找到一个最小的质数 k,然后按下述步骤完成: (1)如果这个质数恰 ...

  4. 初识jstl标签库

    JSTL(JSP Standard Tag Library,JSP标准标签库) 是一个不断完善的开源的 JSP 标签库,是由 apache 的 jakarta 小组来维护的,根据 JST L标签所提供 ...

  5. 【转】JobScheduler

    JobScheduler JobScheduler是Android L(API21)新增的特性,用于定义满足某些条件下执行的任务.它的宗旨是把一些不是特别紧急的任务放到更合适的时机批量处理,这样可以有 ...

  6. P3372 【模板】线段树 1 区间查询与区间修改

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个 ...

  7. 在Android上使用酷狗歌词API

    参考自http://blog.csdn.net/u010752082/article/details/50810190 代码先贴出来: public void searchLyric(){ final ...

  8. axios的简单封装及在组件内使用

    /**第一步 * 配置编译环境和线上环境之间的切换 * baseUrl: 域名地址 * routerMode: 路由模式 * imgBaseUrl: 图片所在域名地址 * */ let Host = ...

  9. 如何破解密码的哈希值,破解双MD5密码值

    这是关于我如何破解密码的哈希值1亿2200万* John the Ripper和oclHashcat-plus故事. 这是几个月前,当我看到一条推特:从korelogic约含共1亿4600万个密码的密 ...

  10. saltstack入门个人笔记

    offical website reference1 reference2 install apt-get install python-software-properties apt install ...