并查集:POJ No1703 Find them, Catch them
题目链接:http://poj.org/problem?id=1703
题意:两个坏蛋属于不同的组织,给出两个坏蛋判定是否一个组织。
题解:已知每次输入的两个帮派人员 x, y; 合并 (x, y + N), (x + N, y)。判定时,如果 (x, y) 属于同一个 根,就是同一个组织,(x, y+N) 属于同一个根,则说明是不同组织。不确定则无解。
- #include <iostream>
- using namespace std;
- const int maxn = * + ;
- int Rank[maxn], par[maxn];
- void solve();
- void init(const int& n) {
- for (int i = ; i < n; i++) {
- par[i] = i;
- Rank[i] = ;
- }
- }
- int find(const int& x) {
- if (par[x] == x) {
- return x;
- }
- else {
- return par[x] = find(par[x]);
- }
- }
- void unite(int x, int y)
- {
- x = find(x);
- y = find(y);
- if (x == y) return ;
- if (Rank[x] < Rank[y]) {
- par[x] = y;
- }
- else {
- par[y] = x;
- if (Rank[x] == Rank[y]) {
- ++Rank[x];
- }
- }
- }
- bool same(int x , int y)
- {
- return find(x) == find(y);
- }
- void solve()
- {
- int T, N, M;
- int x, y;
- char cmd;
- scanf("%d", &T);
- while (T--)
- {
- scanf("%d%d", &N, &M);
- init(N * );
- getchar();
- while (M--)
- {
- //忽略每次输入后的回车符
- scanf("%c%d%d%*c", &cmd, &x, &y);
- //检查
- if (cmd == 'A') {
- if (same(x, y)) {
- printf("In the same gang.\n");
- }
- else if (same(x, y + N)) {
- printf("In different gangs.\n");
- }
- else {
- printf("Not sure yet.\n");
- }
- }
- else {
- //合并 (x, y+N) 或 (x+N, y)
- unite(x, y + N);
- unite(x + N, y);
- }
- }
- }
- }
- int main()
- {
- solve();
- return ;
- }
并查集:POJ No1703 Find them, Catch them的更多相关文章
- [并查集] POJ 1703 Find them, Catch them
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43132 Accepted: ...
- 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条臭 ...
- [并查集] POJ 2236 Wireless Network
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 25022 Accepted: 103 ...
- [并查集] POJ 1182 食物链
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 66294 Accepted: 19539 Description ...
- [并查集] POJ 1611 The Suspects
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 35206 Accepted: 17097 De ...
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题五 并查集 POJ 2236 Wireless Network
题意: 一次地震震坏了所有网点 现在开始修复它们 有N个点 距离为d的网点可以进行通信 O p 代表p点已经修复 S p q 代表询问p q之间是否能够通信 思路: 基础并查集 每次修复一个点重新 ...
- 并查集--poj 2492
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
- 并查集 POJ 1988
#include <cstdio> #define N 30010 int pa[N]; //parent int siz_tree[N]; //size of tree int d[N] ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
随机推荐
- Spring下使用开发webservice
依赖包 <!-- CXF Dependencies --> <dependency> <groupId>org.apache.cxf</groupId> ...
- JS计算两个日期之间的天数,时间差计算
1.日期之间的天数计算 //计算天数差的函数,通用 function DateDiff(sDate1, sDate2) { //sDate1和sDate2是2017-9-25格式 var aDate, ...
- 优先队列的一种实现--堆ADT
二叉堆的两个重要性质: 1.结构性,为完全二叉树,可以用数组方便地表示.2.堆序性:树中每个节点的关键字值大于或等于其父节点的关键字值. 二叉堆的数据结构声明如下: struct HeapStruct ...
- Python入门:数据结构的3个小技巧
这是关于Python的第11篇文章,主要介绍下数据结构的3个小技巧. 排序: 使用sorted函数实现排序. sorted函数按照长短.大小.英文字母的顺序给每个列表的元素进行排序.这个函数经常在数据 ...
- yii 验证码 CCaptcha的总结(转)
今天用到yii的验证码 ccaptcha,经过在网上搜寻 找到以下例子: 1.在controller中加入代码 (1)启用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 &l ...
- soap 简单的例子
首先确保你的soap模块开启 客户端代码 <?php try { $client = new SoapClient(null, array('location' =>"http: ...
- 13个实用的Linux find命令示例
除了在一个目录结构下查找文件这种基本的操作,你还可以用find命令实现一些实用的操作,使你的命令行之旅更加简易. 本文将介绍15种无论是于新手还是老鸟都非常有用的Linux find命令. 首先,在你 ...
- sublimeText3的一些操作记录
# 给绿色版的sublimeText3添加右键菜单,其中@=“Sublime Text 3” 是右键展示的文字, 后面的icon是图标将下面代码保存为.reg文件执行 Windows Registry ...
- contextmap相当于session之类的 用于设置属性 投放到页面上 contextmap的数据存储在map中
contextmap相当于session之类的 用于设置属性 投放到页面上
- 打开eclipse编译后的.class文件
众所周知,用文本编辑器打开.class文件会乱码.我们可以使用命令行打开.class文件项目结构: 代码: public class Synchronized { public static void ...