Find them, Catch them POJ - 1703
题意:N个人,M次操作,操作一:A X Y,X,Y不是同一帮派,操作二:D X Y,判断X和Y的关系。
思路:如果X和Y不是同一帮派,那X与Y+N、Y与X+N是同一帮派,如果X与Y不在同一帮派且X与Y+N不是同一帮派则不能确定关系,如果X与Y是同一帮派则输出same那句话,如果X与Y+N是同一帮派则输出different那句话。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m;
int pre[];
int rank[];
char s[];
void init(int n)
{
for(int i=;i<=n;i++)
pre[i]=i,rank[i]=;
}
int find(int x)
{
return x==pre[x]?x:pre[x]=find(pre[x]);
}
void unite(int x,int y)
{
x=find(x);
y=find(y);
if(x==y)
return ;
if(rank[x]<rank[y])
pre[x]=y;
else
{
pre[y]=x;
if(rank[x]==rank[y])
rank[x]++;
}
}
int same(int x,int y)
{
return find(x)==find(y);
}
int main()
{
//std::ios_base::sync_with_stdio(false);
int t,x,y;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init(n*);
for(int i=;i<=m;i++)
{
scanf("%s%d%d",s,&x,&y);
if(s[]=='A')
{
if(!same(x,y)&&!same(x,y+n)) //确定是否已有确定的关系
printf("Not sure yet.\n");
else if(same(x,y)) //x与y同帮派
printf("In the same gang.\n");
else if(same(x,y+n)) //x与y不同帮派
printf("In different gangs.\n");
}
else
{
unite(x,y+n);
unite(x+n,y);
}
}
}
return ;
}
Find them, Catch them POJ - 1703的更多相关文章
- K - Find them, Catch them POJ - 1703 (带权并查集)
题目链接: K - Find them, Catch them POJ - 1703 题目大意:警方决定捣毁两大犯罪团伙:龙帮和蛇帮,显然一个帮派至少有一人.该城有N个罪犯,编号从1至N(N<= ...
- (并查集 带关系)Find them, Catch them -- poj -- 1703
链接: http://poj.org/problem?id=1703 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3676 ...
- Find them, Catch them(POJ 1703 关系并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 38668 Accepted: ...
- DisJSet:Find them, Catch them(POJ 1703)
抓住他们! 题目大意:两个黑社会帮派,互相打架,现在你很多条信息,要你确定两个人是否属于不同帮派 这题很有趣,题目不是直接给你两个人是否是一个帮派的,他给你的是不同帮派的,也就是给你很多个不同的要你找 ...
- 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(种类并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41463 Accepted: ...
- POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集
POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...
- 【并查集】模板 + 【HDU 1213、HDU 1232、POJ 2236、POJ 1703】例题详解
不想看模板,想直接看题目的请戳下面目录: 目录: HDU 1213 How Many Tables[传送门] HDU 1232 畅通工程 [传送门] POJ 2236 Wireless Network ...
- 【原创】POJ 1703 && RQNOJ 能量项链解题报告
唉 不想说什么了 poj 1703,从看完题到写完第一个版本的代码,只有15分钟 然后一直从晚上八点WA到第二天早上 最后终于发现了BUG,题目要求的“Not sure yet.”,我打成了“No s ...
随机推荐
- Image Storage
- python拼接字符串方法汇总
python拼接字符串一般有以下几种方法: 1.直接通过(+)操作符拼接 s = 'Hello'+' '+'World'+'!' print(s) 输出结果:Hello World! 这种方式最常用. ...
- 项目(九) 企业级Memcached服务应用实践
一, Memcached介绍 1.1 Memcached与常见同类软件对比 (1)Memcached是什么? Memcached是一个开源的,支持高性能,高并发的分布式内存缓存系统,由C语言编写, ...
- Spring <mvc:default-servlet-handler/>
优雅REST风格的资源URL不希望带 .html 或 .do 等后缀.由于早期的Spring MVC不能很好地处理静态资源,所以在web.xml中配置DispatcherServlet的请求映射,往往 ...
- IoU
IoU #include <cstdio> #include <algorithm> #define re(i,a,b) for(int i=a;i<=b;i++) us ...
- python之字符串拼接:%和format
使用百分号拼接字符串: 例如: msg='i am %s my hobby is...' %'abc' print(msg) 如果需要用2个%s呢?就使用括号例如: msg='I am %s my h ...
- iis网站部署常见错误
sqlserver 授权登录名没有db_owner权限 数据库连接字符串端口没开,连接形式:192.168.0.1,1433
- 20175314薛勐 MyOD(课下作业,选做)
MyOD(课下作业,选做) 要求 编写MyOD.java 用java MyOD XXX实现Linux下od -tx -tc XXX的功能 思路 伪代码: 读取命令行输入的参数(文件名) 以16为每个字 ...
- Mysql数据库左外连接,右外连接,模糊查询
内连接,左外连接,右外连接都是数据库的常用连接与使用手段 内连接 select * from assets_car c inner join category c on a.id = c.id; 左外 ...
- Appium+python自动化获取toast消息的方法
转载地址:https://www.cnblogs.com/shangren/p/8191879.html 1. 首先执行这个命令:npm install -g cnpm --registry=http ...