并查集小结

## 并查集小结(一) ##

  • POJ 1703

Description
The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to. The present question is, given two criminals; do they belong to a same clan? You must give your judgment based on incomplete information. (Since the gangsters are always acting secretly.)

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
The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. Each test case begins with a line with two integers N and M, followed by M lines each containing one message as described above.

Output
For each message “A [a] [b]” in each case, your program should give the judgment based on the information got before. The answers might be one of “In the same gang.”, “In different gangs.” and “Not sure yet.”

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.

题意大概是先输入强盗人数和操作次数,每次输入一个operation或者query,op是把两个强盗分到两边,query是询问当前两个强盗是不是一组的。
两个集合,众多子元素,询问每两者之间非一即二的关系,很容易就想到了并查集。
我是开了一个长为2*maxn+5的数组,maxn为最多输入的子元素个数。【1,maxn】为一组,【maxn+1,2*maxn】为二组,因为无法确认输入的两个int,哪个是一组,哪个是二组,故merg(合并)两次。这样处理后结果就是a的敌人和a朋友的敌人,a,和a的朋友都有同样根节点,这样就很容易明白后面的判断了。


  • 调用finde函数时,有两种方法缩短路径。

int finde(int a)
{
int k = a,m;
while(a != father[a])
{
a = father[a];
}
while(k != father[a])
{
m = father[k];
father[k] = a;
k = m;
}
return father[a];
}

这样的好处是避免了递归(即栈溢出),从下往上一层层访问并修改该集维护的value值
int find(int x){
if(father[x]!=x){
int tmp=father[x];
father[x]=find(father[x]);
dis[x]+=dis[tmp];
}
return father[x];
}

这样写的好处是从上往下维护value值。


剩下的就没什么难理解。。。
最后,贴上我的AC代码。如果各位大神觉得这篇文章还说的过去,对自己有启发的话,不放点个赞吧。

#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
int n;
const int maxn = 100005;
int father[2*maxn + 5];

int finde(int a)
{
    int k = a,m;
    while(a != father[a])
    {
        a = father[a];
    }
    while(k != father[a])
    {
        m = father[k];
        father[k] = a;
        k = m;
    }
    return father[a];
}

void merg(int a,int b)
{
    int p = finde(a);
    int q = finde(b);
    if(p == q)  return;
    else    father[p] = q;
    return;
}

bool judge(int a,int b)
{
    int q = finde(a);
    int p = finde(b);
    if(p == q)  return true;
    else return false;
}

void init()
{
    for(int i=1;i<=n;i++)
    {
        father[i] = i;
    }
    for(int i=1+maxn;i <= maxn+n;i++)
    {
        father[i] = i;
    }
    return;
}
int main()
{
    int t,m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        init();
        for(int j=0;j<m;j++)
        {
            char op;
            getchar();
            scanf("%c",&op);
            if(op == 'D')
            {
                int x,y;
                scanf("%d%d",&x,&y);
                merg(x,y+maxn);
                merg(y,x+maxn);
            }
            else    if(op == 'A')
            {
                int x,y;
                scanf("%d%d",&x,&y);
                if(judge(x,y+maxn)) printf("In different gangs.\n");
                else    if(judge(x,y))  printf("In the same gang.\n");
                else    printf("Not sure yet.\n");
            }
        }
    }
    return 0;
}
**描述:“适用于JDK8的环境”** 本文将深入探讨Neo4j社区版3.5.6版本,这是一个基于图数据库的强大工具,特别适用于知识图谱构建和可视化。由于其运行需求,必须在Java Development Kit(JDK)8的环境下进行安装和操作。 **一、Neo4j概述** Neo4j是一款开源的图形数据库,它以节点、关系和属性的形式存储数据,这使得处理复杂网络结构的数据变得更为直观和高效。Neo4j社区版是免费的,适合开发和学习用途,而企业版则提供了更多的高级功能和服务。 **二、JDK8要求** 为了运行Neo4j 3.5.6,你需要在你的计算机上安装JDK8。JDK是Java开发工具包,包含了运行Java应用程序所需的Java虚拟机(JVM)以及一系列开发工具。确保安装的是与Neo4j版本兼容的JDK版本至关重要,因为不兼容的JDK可能会导致运行错误或性能问题。 **三、安装和配置** 1. **下载与解压**: 从官方渠道下载"neo4j-community-3.5.6.zip"压缩文件,并将其解压到你选择的目录。 2. **环境变量配置**: 配置系统环境变量,将Neo4j的bin目录添加到PATH环境变量中,以便于命令行启动和管理数据库。 3. **修改配置文件**: Neo4j的配置主要通过`conf/neo4j.conf`文件进行,如需更改默认设置,如内存分配、端口设置等,应在此文件中进行修改。 4. **启动和停止**: 使用`neo4j console`命令启动服务,`neo4j stop`命令关闭服务。 **四、知识图谱与可视化** Neo4j因其强大的图数据模型,成为构建知识图谱的理想选择。你可以使用Cypher查询语言来操作和查询图数据,它的语法简洁且直观,易于学习。 1. **Cypher语言**: Cypher是一种声明式、图形化
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值