并查集模板

基础并查集:

How Many Tables HDU-1213

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 56924 Accepted Submission(s): 28381

Problem Description

Today is Ignatius’ birthday. He invites a lot of friends. Now it’s dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.

One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.

For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.

Input

The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.

Output

For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.

Sample Input

2
5 3
1 2
2 3
4 5

5 1
2 5

Sample Output

2
4

解题思路:

A和B是朋友,可以坐一桌,B和C是朋友,可以坐一桌,那么朋友的朋友也是自己的朋友,所以ABC可以坐同一桌,现给出一堆朋友关系,问最少需要多少桌子。最最基础的并查集模板题(带路径压缩)。

#include <iostream>
using namespace std;

const int N = 1e6+10;
int pre[N];

void pre_wk(int n) // 预处理
{
    for(int i = 0 ; i <= n; i ++)
        pre[i] = i;
}
int find_(int root)	 // 带路径压缩查找父节点,既然能压缩为何不压缩
{
    return pre[root] == root ?  root : pre[root] = find_(pre[root]);
}
/**
int find_(int root) // 不带路径压缩版本
{
	while(pre[root] != root) 
		root = pre[root];
	return root;
}
**/

void join(int a,int b) // 把a,b加到同一个集合
{
    pre[find_(a)] = pre[find_(b)];
}

bool same(int a,int b)
{
    return find_(a) == find_(b);
}

int main()
{
    int t,n,m,a,b;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        pre_wk(n);
        for(int i = 0 ; i < m ; i ++)
        {
            cin>>a>>b;
            join(a,b);
        }
        int ans = 0;
        for(int i = 1 ; i <= n ; i ++)
            if(pre[i] == i)
                ans++;
        cout<<ans<<endl;
    }
    return 0;
}

带权并查集:

How Many Answers Are Wrong HDU-3038

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 21864 Accepted Submission(s): 7523

Problem Description

TT and FF are … friends. Uh… very very good friends -________-b

FF is a bad boy, he is always wooing TT to play the following game with him. This is a very humdrum game. To begin with, TT should write down a sequence of integers-_-!!(bored).

Then, FF can choose a continuous subsequence from it(for example the subsequence from the third to the fifth integer inclusively). After that, FF will ask TT what the sum of the subsequence he chose is. The next, TT will answer FF’s question. Then, FF can redo this process. In the end, FF must work out the entire sequence of integers.

BoringBoringa very very boring game!!! TT doesn’t want to play with FF at all. To punish FF, she often tells FF the wrong answers on purpose.

The bad boy is not a fool man. FF detects some answers are incompatible. Of course, these contradictions make it difficult to calculate the sequence.

However, TT is a nice and lovely girl. She doesn’t have the heart to be hard on FF. To save time, she guarantees that the answers are all right if there is no logical mistakes indeed.

What’s more, if FF finds an answer to be wrong, he will ignore it when judging next answers.

But there will be so many questions that poor FF can’t make sure whether the current answer is right or wrong in a moment. So he decides to write a program to help him with this matter. The program will receive a series of questions from FF together with the answers FF has received from TT. The aim of this program is to find how many answers are wrong. Only by ignoring the wrong answers can FF work out the entire sequence of integers. Poor FF has no time to do this job. And now he is asking for your help~(Why asking trouble for himself~~Bad boy)

Input

Line 1: Two integers, N and M (1 <= N <= 200000, 1 <= M <= 40000). Means TT wrote N integers and FF asked her M questions.

Line 2…M+1: Line i+1 contains three integer: Ai, Bi and Si. Means TT answered FF that the sum from Ai to Bi is Si. It’s guaranteed that 0 < Ai <= Bi <= N.

You can assume that any sum of subsequence is fit in 32-bit integer.

Output

A single line with a integer denotes how many answers are wrong.

Sample Input

10 5
1 10 100
7 10 28
1 3 32
4 6 41
6 6 1

Sample Output

1

解题思路:

依旧是一个并查集模板题,但是相对于普通的并查集,多了一个权值,于是可以用一个数组记录下每个权值,在进行路径压缩的时候,同时进行权值的合并,在计算答案时,就判断权值是否合理就行了。

AC代码:

#include <bits/stdc++.h>

const int N = 2e6+10;
using namespace std;
int rank_[N];
int pre[N];

void pre_wk(int n) // 预处理
{
    for(int i = 0 ; i <= n+1 ; i ++)
    {
        pre[i] = i;
        rank_[i] = 0;
    }
}

int find_(int root)
{
    if(root == pre[root])
        return root;
    else{
        int x = find_(pre[root]);
        rank_[root] += rank_[pre[root]]; // 合并权值
        return pre[root] = x;            // 压缩路径
    }
}


int main()
{
    int n,m,a,b,s;
    int ans = 0;
    while(cin>>n>>m)
    {
        ans = 0;
        pre_wk(n);
        for(int i = 0 ; i < m ; i ++)
        {
            cin>>a>>b>>s;
            b ++;				// 这里要注意形成一个左闭右开的区间,不然会wa掉!
            int a1 = find_(a);
            int b1 = find_(b);
            if(a1 == b1)
            {
                if(rank_[a]-rank_[b] != s)  //  判断权值的合理性
                    ans++;
            }
            else{
                rank_[a1] = s - rank_[a] + rank_[b]; // 进行权值的更新
                pre[a1] = b1;
            }
        }
        cout<<ans<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值