种类并查集,基本思想是每次压缩路径都必须同时更新子节点和根节点的关系,这种关系是通过子节点和父亲节点的关系,以及父亲节点与根节点的关系运算出来。

压缩路径的findme();参考了大神的代码,做的第二个种类并查集....

#include<stdio.h>
#include<string.h>
int cri[];//记录每一个元素的根节点
int pp[];//记录元素与根节点的关系,0代表盟友,1代表敌人
int findme(int a)
{
if(a==cri[a])
{
return a;
}
int temp=cri[a];//temp用于记录递归前该节点的父亲节点
cri[a]=findme(cri[a]);//找到a节点的根节点
pp[a]=(pp[temp]+pp[a])&;//通过a节点的父亲节点与根节点的关系以及a节点和其父亲节点的关系更新得到a节点和新的根节点的关系
return cri[a];
}
void link(int a, int b)
{
int ttmp,tmpa,tmpb;
tmpa=findme(a);
tmpb=findme(b);
if(tmpa!=tmpb)
{
cri[tmpb]=tmpa;
pp[tmpb]=(pp[a]+pp[b]+)&;
}
}
int main()
{
int t,n,m,a,b;
char ca;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
memset(pp,,sizeof(pp));
for(int i=;i<=n;i++)
{
cri[i]=i;
}
getchar();
for(int i=;i<m;i++)
{
ca=getchar();
if(ca=='D')
{
scanf("%d%d",&a,&b);
getchar();
if(a==b)
continue;
link(a,b);
}
else
{
scanf("%d%d",&a,&b);
getchar();
if(findme(a)==findme(b))
{
if((pp[a]+pp[b])&)
{
printf("In different gangs.\n");
}
else
{
printf("In the same gang.\n");
}
}
else
{
printf("Not sure yet.\n");
}
}
}
}
return ;
}

POJ 1703的更多相关文章

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

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

  2. POJ 1703 Find them, Catch them(种类并查集)

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

  3. 【原创】POJ 1703 && RQNOJ 能量项链解题报告

    唉 不想说什么了 poj 1703,从看完题到写完第一个版本的代码,只有15分钟 然后一直从晚上八点WA到第二天早上 最后终于发现了BUG,题目要求的“Not sure yet.”,我打成了“No s ...

  4. K - Find them, Catch them POJ - 1703 (带权并查集)

    题目链接: K - Find them, Catch them POJ - 1703 题目大意:警方决定捣毁两大犯罪团伙:龙帮和蛇帮,显然一个帮派至少有一人.该城有N个罪犯,编号从1至N(N<= ...

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

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

  6. 【并查集】模板 + 【HDU 1213、HDU 1232、POJ 2236、POJ 1703】例题详解

    不想看模板,想直接看题目的请戳下面目录: 目录: HDU 1213 How Many Tables[传送门] HDU 1232 畅通工程 [传送门] POJ 2236 Wireless Network ...

  7. 种类并查集(POJ 1703)

    1703 -- Find them, Catch them http://poj.org/problem?id=1703 题目大意:有2个敌对帮派,输入D a b表示a,b在不同帮派,输入A a b表 ...

  8. hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them

    http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...

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

    题目:http://poj.org/problem?id=1703 题意:一个地方有两个帮派, 每个罪犯只属于其中一个帮派,D 后输入的是两个人属于不同的帮派, A后询问 两个人是否属于 同一个帮派. ...

  10. (并查集 带关系)Find them, Catch them -- poj -- 1703

    链接: http://poj.org/problem?id=1703 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3676 ...

随机推荐

  1. android layout_weight 使用总结

    今天在使用androidlayout_weight的时候遇到点奇怪的问题,就上网查了一下,发现这篇文章很详细,就转了过来,谢谢分享者,写的很详细.  在 android开发中LinearLayout很 ...

  2. 数据接口管理工具 thx RAP

    RAP是数据接口管理工具.在开发时前端将请求转至RAP,由RAP提供模拟数据:而后端使用RAP测试接口的正确性.这样RAP就成为了开发过程中的强 依赖,进而确保接口文档的实时正确性.RAP采用JSON ...

  3. ORALCE 游标简单的实例

    --取简单的游标 declare cursor sp is select * from user_tables; myrecord user_tables%ROWTYPE; begin open sp ...

  4. RMAN备份数据库与恢复数据库(整库)

    1 准备 2 1.1 检查数据库归档状态 2 1.2 RMAN登陆目标 2 2 备份全库 2 2.1 创建备份数据存储目录 2 2.2 RMAN备份全库 2 2.3 试验(备份后,改变数据) 5 2. ...

  5. 程序员书单_java专项进阶篇

    JDBC API数据库编程实作教材 http://download.csdn.net/detail/shenzhq1980/9145715 Java事务设计模式 http://download.csd ...

  6. HackerRank "Components in a graph"

    Regular Union-Find practice one. #include <cmath> #include <cstdio> #include <climits ...

  7. CSS媒体查询(@media)

    @media only screen and (max-width: 500px) { .gridmenu { width:100%; } .gridmain { width:100%; } .gri ...

  8. Centos7安装Zabbix3.0

    1.安装服务器端包 #rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch. ...

  9. QT5中的信号与槽与C++ 11的function的配合

    最近将公司的界面开发库从WX转换到了QT,有了信号和槽,原本我们在使用WX的时候,为了使用信号,我们是使用BOOST的signal2库,到了QT有了信号槽,这个就没什么必要了 但是前段时间使用QT发现 ...

  10. CXF超时设置

    转自: http://peak.iteye.com/blog/1285211 http://win.sy.blog.163.com/blog/static/9419718620131014385644 ...