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. Plugging an Unplugged Pluggable Database

    1.unplug To unplug a PDB, you first close it and then generate an XML manifest file. The XML file co ...

  2. CF963A Alternating Sum

    思路:利用周期性转化为等比数列求和. 注意当a != b的时候 bk * inv(ak) % (109 + 9)依然有可能等于1,不知道为什么. 实现: #include <bits/stdc+ ...

  3. 《Head First HTML与CSS》的HTML标签、属性

    一个标准的html5页面: <!doctype html> <html lang="zh-cmn-Hans"> <head> <meta ...

  4. Mac Outlook 15 连接office365 一直跳出登录框的问题

    解决: 比如 原登录用户名密码为 abc@xxx.com 密码:***** 改一下outlook帐号中的登录用户名为:xxx.com\abc   密码为原来密码,尝试连接,连接状态会连上,但过会儿又提 ...

  5. android studio MQTT测试成功

    package myself.mqtt.wenzheng.studio.mqtt; import android.app.Notification; import android.app.Notifi ...

  6. php 缓存工具类 实现网页缓存

    php 缓存工具类 实现网页缓存 php程序在抵抗大流量访问的时候动态网站往往都是难以招架,所以要引入缓存机制,一般情况下有两种类型缓存 一.文件缓存 二.数据查询结果缓存,使用内存来实现高速缓存 本 ...

  7. Hadoop分布式集群安装

        环境准备     操作系统使用ubuntu-16.04.2 64位 JDK使用jdk1.8 Hadoop使用Hadoop 2.8版本     镜像下载  操作系统     操作系统使用ubun ...

  8. 如何快速上手基础的CSS3动画

    前言 说起CSS3动画,就必须说说 transform,translate,transition,animation这4个属性,transform主要定义元素的动作,比如旋转.倾斜.位移等,trans ...

  9. Spring-01 注解实现IOC

    Spring框架四大原则 使用pojo进行轻量级和最小侵入式开发. 通过依赖注入和基于接口编程实现松耦合. 使用AOP和默认习惯进行声明式编程. 使用AOP和模板(template)减少模式化代码. ...

  10. Java笔记:编写第一个Java程序

    2017.6.17 1.编写第一个Java程序 创建text文本,命名第一个Java程序.txt 在里面编写Java代码 public class Demo1{ public static void ...