poj1703 Find them, Catch them(并查集的应用)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 32225 | Accepted: 9947 |
Description
Assume N (N <= 10^5) criminals are currently in Tadu City, numbered from 1 to N. And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. You will be given M (M <= 10^5) messages in sequence, which are in the following two kinds:
1. D [a] [b]
where [a] and [b] are the numbers of two criminals, and they belong to different gangs.
2. A [a] [b]
where [a] and [b] are the numbers of two criminals. This requires you to decide whether a and b belong to a same gang.
Input
Output
Sample Input
- 1
- 5 5
- A 1 2
- D 1 2
- A 1 2
- D 2 4
- A 1 4
Sample Output
- Not sure yet.
- In different gangs.
- In the same gang.
- 题目大意:N 表示共有 N 个帮派;下面有 M 条个询问
有两个帮派,现在告诉你一些互相敌对的点对,然后让你判断某两个点之间的关系,并首先判两个点关系是否确定;
用并查集的思想只要两个点在一个集合里就可以了,直接用并查集然后时候在同一个集合;
AC代码:
- #include<iostream>
- #include<algorithm>
- #include<cstdio>
- using namespace std;
- // 0 代表木有关系,1表示不同类,2代表同类
- const int N = 1e5+;
- int m;
- int f[N],c[N];
- int init()
- {
- for(int i=; i<=m; i++)
- {
- f[i] = i;
- c[i] = ;
- }
- }
- int xfind(int x)
- {
- if(f[x] == x)
- return x;
- int t = f[x];
- f[x] =xfind(f[x]);
- c[x] = (c[x]+c[t])%;
- return f[x];
- }
- int solve(char ch,int x,int y )
- {
- int fx = xfind(x);
- int fy = xfind(y);
- if(fx != fy)
- {
- if(ch == 'A') return ; //返回值0,表示不确定
- f[fx] =fy;
- if((c[x]+c[y])% ==)
- c[fx] = ;
- }
- else
- {
- if(ch == 'A')
- {
- if(c[x] != c[y]) return ; //返回值1,表示在不同的帮派
- if(c[x] == c[y]) return ; //返回值2,表示在相同的帮派
- }
- }
- if(ch == 'D') return ;//这种情况不同在考虑的
- }
- int main( )
- {
- int T,n,x,y;
- char s;
- scanf("%d",&T);
- while(T--)
- {
- scanf("%d %d",&m,&n);
- init(); //初始化并查集
- for(int i=; i<n; i++)
- {
- scanf(" %c %d %d",&s,&x,&y);
- int flag = solve(s,x,y);
- if(flag == ) printf("Not sure yet.\n");
- else if(flag == ) printf("In different gangs.\n");
- else if(flag == ) printf("In the same gang.\n");
- }
- }
- return ;
- }
poj1703 Find them, Catch them(并查集的应用)的更多相关文章
- poj1703 Find them, Catch them 并查集
poj(1703) Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26992 ...
- 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 ...
- POJ 1703 Find them, catch them (并查集)
题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2 D 3 4 D 5 6...这就至少有3个集合了.并且 ...
- poj1703--Find them, Catch them(并查集应用)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 32073 Accepted: ...
- POJ1703-Find them, Catch them 并查集构造
Find them, Catch them 好久没有做并查集的题,竟然快把并查集忘完了. 题意:大致是有两个监狱,n个 ...
- POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集
POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...
- POJ 1703 Find them, Catch them 并查集的应用
题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...
- poj1703_Find them, Catch them_并查集
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42451 Accepted: ...
- poj.1703.Find them, Catch them(并查集)
Find them, Catch them Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I6 ...
- POJ 1703 Find them, Catch them(并查集高级应用)
手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...
随机推荐
- Android内存优化11 内存泄漏常见情况2 内部类泄漏
线程持久化 Java中的Thread有一个特点就是她们都是直接被GC Root所引用,也就是说Dalvik虚拟机对所有被激活状态的线程都是持有强引用,导致GC永远都无法回收掉这些线程对象,除非线程被手 ...
- osx的10款文本编辑器
) Eddie Eddie 是 Mac OS X 和 Gnome 下的一个程序员编辑器,灵感来自于 Macintosh Programmer’s Workshop, Eddie 包含很多强大的特性,而 ...
- javascript常用排序算法实现
毕业后,由于工作中很少需要自已去写一些排序,所以那些排序算法都忘得差不多了,不过排序是最基础的算法,还是不能落下啦,于是找了一些资料,然后用Javascript实现了一些常用的算法,具体代码如下: & ...
- linux grep的选项
grep -i 关闭大写和小写敏感性 grep -v 打印全部不包括. . 的行(屏蔽某些条目) grep -l 打印包括模式的文件名称 grep ...
- 【转】go语言的字节序
原文:http://lihaoquan.me/2016/11/5/golang-byteorder.html 这个人的博客写的不错,品质也比较高. 我应该也要有这种精神,这种态度.深入到计算机的世界中 ...
- TCP的状态(SYN,FIN等)
TCP的标志位有SYN,FIN,RST,ACK,PSH,URG SYN:建立连接. FIN:关闭连接. RST:连接重置. ACK:响应. PSH:有数据传输. URG:urgent紧急. ACK可以 ...
- 【甘道夫】Win7环境下Eclipse连接Hadoop2.2.0
准备: 确保hadoop2.2.0集群正常执行 1.eclipse中建立javaproject,导入hadoop2.2.0相关jar包 2.在src根文件夹下拷入log4j.properties,通过 ...
- C#秘密武器之特性
一.概述 Attribute说白了就是一个类而已,里边一般含有一些附加信息,或者一些特殊的处理逻辑,以便告诉编译器应用该特性的东东是个奇葩,需要特殊对待! 二.使用时的注意事项 2.1. Attrib ...
- vs2012升级到vs2013后,sql server 无法通过IP登录解决方案
因项目需要,vs升级到2013,可是数据库通过IP却无法连接,一直报内存异常,困扰许久,原来是.net框架出了问题,可以通过用管理员身份运行:netsh winsock reset解决!
- 【转】Socket状态变迁图
转自:http://www.cnblogs.com/ILove/archive/2008/12/08/1350430.html 服务端,端口的状态变化 先在本机(IP地址为:192.168.1.1 ...