LOADING

加载过慢请开启缓存 浏览器默认开启

并查集

2023/4/16 c++ C++ ACM 刷题

概念

并查集是一种树型的数据结构,用于处理一些不相交集合的合并及查询问题(即所谓的并、查)。比如说,我们可以用并查集来判断一个森林中有几棵树、某个节点是否属于某棵树等。并查集(Union-Find)就是用来对集合进行 合并(Union) 与 查询(Find) 操作的一种数据结构。

合并 就是将两个不相交的集合合并成一个集合。
查询 就是查询两个元素是否属于同一集合。

简易版并查集模板

int n=1001;//定义节点数量
int father[1001]
//初始化并查集
int init(){
    for(int i=0;i<n;i++){
        father[i]=i;
    }
}
//并查集查找根
int find(int x){
    return u==father[u]?u:father[u]=find(father[u]);//压缩路径
}
//将v->u这条边添加到并查集中
int merge(int u,int v){
    u=find(u);
    v=find(v);
    if(u==v) return;
    father[v]=u;
}
//在减少find函数迭代次数的情况,优化加边的操作就会变成将小集合连接到大集合上
int merge_2(int u,int v){
    u=find(u);
    v=find(v);
    if(u==v) return ;
    if(size[u]<size[v]) father[u]=v;
    else father[v]=u; 
}
//判断u和v是否是在一个根下
int same(int u,int v){
    u=find(u);
    v=find(v);
    return u==v;
}

带权并查集

在一般的并查集的基础上,在每条边中记录额外的信息

int n=1001;//定义节点数量
int father[1001];
int value[1001];
//初始化并查集
int init(){
    for(int i=0;i<n;i++){
        father[i]=i;
    }
}
//并查集查找根
int find(int x){
    if(x!=father[x]){
        int t=father[x];
        father[x]=find(father[x]);
        value[x]+=value[t];
    }
    return father[x];
}

例题

题目描述:

Now and then you play the following game with your friend. Your friend writes down a sequence consisting of zeroes and ones. You choose a continuous subsequence (for example the subsequence from the third to the fifth digit inclusively) and ask him, whether this subsequence contains even or odd number of ones. Your friend answers your question and you can ask him about another subsequence and so on. Your task is to guess the entire sequence of numbers.
You suspect some of your friend’s answers may not be correct and you want to convict him of falsehood. Thus you have decided to write a program to help you in this matter. The program will receive a series of your questions together with the answers you have received from your friend. The aim of this program is to find the first answer which is provably wrong, i.e. that there exists a sequence satisfying answers to all the previous questions, but no such sequence satisfies this answer.

输入:

The first line of input contains one number, which is the length of the sequence of zeroes and ones. This length is less or equal to 1000000000. In the second line, there is one positive integer which is the number of questions asked and answers to them. The number of questions and answers is less or equal to 5000. The remaining lines specify questions and answers. Each line contains one question and the answer to this question: two integers (the position of the first and last digit in the chosen subsequence) and one word which is either even' or odd’ (the answer, i.e. the parity of the number of ones in the chosen subsequence, where even' means an even number of ones and odd’ means an odd number).

输出:

There is only one line in output containing one integer X. Number X says that there exists a sequence of zeroes and ones satisfying first X parity conditions, but there exists none satisfying X+1 conditions. If there exists a sequence of zeroes and ones satisfying all the given conditions, then number X should be the number of all the questions asked.

样例:
10
5
1 2 even
3 4 odd
5 6 even
1 6 even
7 10 odd

Sample Output

3

#include<bits/stdc++.h>
using namespace std;
const int N=5e4+10;
int p[N],d[N];
int n,k;
int find(int x)
{
    if(x!=p[x]){
        int t=p[x];
        p[x]=find(p[x]);
        d[x]=d[x]+d[t];
    }
    return p[x];
}
signed main()
{
    cin>>n>>k;
    int ans=0;
    for(int i=1;i<=n;i++) p[i]=i;
    while(k--)
    {
        int v,a,b;
        cin>>v>>a>>b;
        if(a>n||b>n) {
            ans++;
            continue;
        }
        int pa=find(a),pb=find(b);
        if(v==1){
            if(pa==pb){//在同一个集合内
                if((d[a]-d[b])%3){//如果不是同类关系
                    ans++;
                }
            }
            else {//不在一个集合内,要合并
                p[pa]=pb;
                d[pa]=d[b]-d[a];//更新信息,可以画图理解
                                //因为要满足d[b]=d[pa]+d[a]
            }
        }
        else{
            if(a==b) {ans++; continue;}
            else 
            {
                if(pa==pb){
                    if((d[a] - d[b] - 1) % 3) ans++;
                }
                else {
                    p[pa]=pb;//要满足d[b]=d[pa]+d[a]-1;
                    d[pa]=d[b]-d[a]+1;
                }
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}
本文作者:GWB
当前时间:2023-11-09 11:11:12
版权声明:本文由gwb原创,本博客所有文章除特别声明外,均采用 CC BY-NC-ND 4.0 国际许可协议。
转载请注明出处!