题目链接:http://poj.org/problem?id=1703

可以认为n个人和m句话;

每句话包含A a b;D a b;

刚开始关系不确定;

A a b 就是问ab 是否同类;

D a b就是告诉我们ab不同类;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <queue>
#include <algorithm>
using namespace std; const int oo = 0x3f3f3f3f;
const int N = ; int f[N], r[N];///r[i]代表f[i]与i的关系,1代表不同类,0代表同类; int Find(int x)
{
int k=f[x];
if(x!=f[x])
{
f[x] = Find(f[x]);
r[x] = (r[x]+r[k])%;
}
return f[x];
} int main()
{
int t;
scanf("%d", &t); while(t--)
{
char s[];
int n, m, i, a, b, fa, fb; scanf("%d%d", &n, &m); for(i=; i<=n; i++)
{
f[i] = i;
r[i] = ;
} for(i=; i<=m; i++)
{
scanf("%s%d%d", s, &a, &b); if(s[]=='A')
{ fa = Find(a);
fb = Find(b); if(fa!=fb)
printf("Not sure yet.\n");
else
{
if(r[a]==r[b])
printf("In the same gang.\n");
else
printf("In different gangs.\n");
} }
else
{
fa = Find(a);
fb = Find(b); if(fa!=fb)
{
f[fa] = fb;
r[fa] = (r[b] - r[a] + + )%;
}
}
}
}
return ;
}

Find them, Catch them---poj1703(并查集)的更多相关文章

  1. poj1703 Find them, Catch them(并查集)

    https://vjudge.net/problem/POJ-1703 9ms多,卡着时间过了.上次一道并查集也是这样,总觉得要学一波并查集的优化.. 续:好像是可以只做一层存放敌人即可. #incl ...

  2. poj1703 Find them, Catch them(并查集的应用)

    Find them, Catch them   Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32225   Accepte ...

  3. [poj1703]Find them, Catch them(种类并查集)

    题意:食物链的弱化版本 解题关键:种类并查集,注意向量的合成. $rank$为1代表与父亲对立,$rank$为0代表与父亲同类. #include<iostream> #include&l ...

  4. poj1703 Find them,Catch them 【并查集】

    做过一些的带权并查集,再来做所谓的"种类并查集",发现好像就顿悟了. 种类并查集与带权并查集实质上的区别并不大. 关键的区别就是种类并查集仅仅是带权并查集再弄个%取余操作而已.然后 ...

  5. POJ1703Find them, Catch them[种类并查集]

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

  6. POJ_1703 Find them, Catch them 【并查集】

    一.题面 POJ1703 二.分析 需要将并查集与矢量法则相结合.par数组用以记录父节点,rank用以记录与父节点的关系.如题意,有两种关系,设定0是属于同一个帮派,1表示不属于同一个帮派. 运用并 ...

  7. POJ:1703-Find them, Catch them(并查集好题)(种类并查集)

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

  8. poj1703Find them, Catch them(并查集以及路径压缩)

    /* 题目大意:有两个不同的黑帮,开始的时候不清楚每个人是属于哪个的! 执行两个操作 A a, b回答a, b两个人是否在同一帮派,或者不确定 D a, b表示a, b两个人不在同一个帮派 思路:利用 ...

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

    题意:有N名来自两个帮派的坏蛋,已知一些坏蛋两两不属于同一帮派,求判断给定两个坏蛋是否属于同一帮派. 思路: 解法一: 编号划分 定义并查集为:并查集里的元素i-x表示i属于帮派x,同一个并查集的元素 ...

  10. POJ 1703 Find them, Catch them(并查集拓展)

    Description The police office in Tadu City decides to say ends to the chaos, as launch actions to ro ...

随机推荐

  1. 手动安装OpenStack Mistral

    Prepare packages: $ sudo apt-get install python-dev python-setuptools python-pip libffi-dev libxslt1 ...

  2. JavaSE集合(八)之Map

    前面给大家介绍了集合家族中的Collection家族,这一篇给大家分享的是集合中的另一个家族就是Map家族.以前的时候学习Map的时候没有很认真的去学习,我觉得很多东西还是不是很清楚. 这次我将总结的 ...

  3. Hbase学习之javaApI封装

    http://qindongliang.iteye.com/blog/2096140  

  4. 使用 JMeter 完成常用的压力测试 [转]

    讲到测试,人们脑海中首先浮现的就是针对软件正确性的测试,即常说的功能测试.但是软件仅仅只是功能正确是不够的.在实际开发中,还有其它的非功能因素也起着决定性的因素,例如软件的响应速度.影响软件响应速度的 ...

  5. (转)Linux开启mysql远程连接的设置步骤

    Mysql默认root用户只能本地访问,不能远程连接管理mysql数据库,Linux如何开启mysql远程连接?设置步骤如下: 1.GRANT命令创建远程连接mysql授权用户itlogger mys ...

  6. 使用鼠标监听器,使鼠标悬停在JTable某行时背景色改变

    一.需要知道的事实: 1.当鼠标悬停在JTable上时,相应的格子(cell)的渲染器(TableCellRenderer)的渲染方法(getTableCellRenererComponent)会被调 ...

  7. Android 4.0 Tabhost图标显示不出来

    安卓4.0会有这个问题,修改Manifest.xml里面的Theme,找到System Resources,里面有Theme.black,选这个就行了.剩下自己要改背景色什么的这个还是比较easy的吧 ...

  8. Android NDK开发-3-环境搭建

    1.创建Android工程 2.打开android-ndk32-r10-windows-x86_64\android-ndk-r10\samples例子 3.打开hello-jni,拷贝java代码和 ...

  9. 实例:用类来写一个 memcached 启动脚本

    [root@localhost ~]$ yum install -y memcached #!/usr/bin/env python #-*- coding:utf-8 -*- import os i ...

  10. 说说M451的例程库的说明

    Directory Information Document   Driver reference manual and reversion history. Library         Driv ...