并查集

本文详细介绍了并查集这一数据结构的基本概念、实现方法及其在解决特定类型问题上的应用。并查集主要用于处理不相交集合的合并及查询问题,在算法竞赛中常用于判环等问题。文中还提供了一个具体的实例问题,通过并查集算法来寻找最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

并查集

并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题。常常在使用中以森林来表示。
基本代码如下:


void init()   //初始化
{
    for(int i=0;i<=n;i++){
        a[i]=i;
    }
}
int find(int x)    //查找x的父代及路径压缩
{
    return x==a[x] ? x : a[x]=find(a[x]);
}
void mix(int p,int q)  //将p和q联合到一个集合
{
    int x=find(p);
    int y=find(q);
    if(x!=y){
        a[x]=y;
        num--;
    }
} 

并查集判环

int mix(int x,int y)     
{
    int p=find(x);
    int q=find(y);
    if(p!=q)
    {
        a[p]=q;
        if(b[q]||b[p]||b[x]||b[y])  b[x]=b[y]=b[p]=b[q]=1;   //与环在同一集合内的点都进行标记
    }
    else   b[x]=b[y]=b[p]=b[q]=1;    //若形成环,则进行标记
    return 0;
}
int check()   //检查入度为0的点
{
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        if(b[find(i)]==false&&find(i)==i) ans++;
    }
    return ans;
}

E. New Reform

Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It isnot guaranteed that you can get from any city to any other one, using only the existing roads.

The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).

In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is consideredseparate, if no road leads into it, while it is allowed to have roads leading from this city.

Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.

Input
The first line of the input contains two positive integers, n and m — the number of the cities and the number of roads in Berland (2 ≤ n ≤ 100 000,1 ≤ m ≤ 100 000).

Next m lines contain the descriptions of the roads: thei-th road is determined by two distinct integersxi, yi (1 ≤ xi, yi ≤ n,xi ≠ yi), wherexi andyi are the numbers of the cities connected by thei-th road.

It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.

Output
Print a single integer — the minimum number of separated cities after the reform.

Examples
Input
4 3
2 1
1 3
4 3
Output
1
Input
5 5
2 1
1 3
2 3
2 5
4 3
Output
0
Input
6 5
1 2
2 3
4 5
4 6
5 6
Output
1

题意:给你n个点,m条边, 让你给每条边一个方向, 求入度为0的点的最小个数

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<iostream>
#include<map>
#include<vector>
#include<set>
#include<queue>
using namespace std;
typedef long long ll;
typedef unsigned long long ull; 

const int maxn=100000+5;
const int inf=0x3f3f3f3f;

int a[maxn];
int b[maxn];

int n,m;

void init()
{
    for(int i=0;i<=n;i++)
        a[i]=i;
}

int find(int x)   
{
    return x==a[x] ? x : a[x]=find(a[x]);
}

int mix(int x,int y)
{
    int p=find(x);
    int q=find(y);
    if(p!=q)
    {
        a[p]=q;
        if(b[q]||b[p]||b[x]||b[y])  b[x]=b[y]=b[p]=b[q]=1;
    }
    else   b[x]=b[y]=b[p]=b[q]=1;
    return 0;
}

int check(){
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        if(b[find(i)]==false&&find(i)==i) ans++;
    }
    return ans;
}
int main(){
    ios::sync_with_stdio(false);
    cin>>n>>m;
    init();
    for(int i=1;i<=m;i++)
    {
        int p,q;
        cin>>p>>q;
        mix(p,q);
    }   
    cout<<check()<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值