Invoking the Magic(补题)并查集&&离散化

Invoking the Magic
( 并查集路径优化+map)

BaoBao is a lazy boy. He has n pairs of socks in different colours and he washes them once a month. In the washing machine, these socks will be mixed.

Because there are too many socks that need to be paired, BaoBao divides the whole sock pairing process into two stages.

In the first stage, BaoBao randomly distributes the socks in pairs. Then in the second stage, BaoBao repeats the following operation until all the socks are paired: BaoBao chooses some of the pairs of socks, puts them into his magic washing basin, and uses his magic. If the socks in the magic washing basin can be paired perfectly when he uses his magic, the magic washing basin will pair them for BaoBao automatically. However, if they can’t (which means there is at least one sock whose colour is unique in the basin), the magic basin will explode and BaoBao must not let this happen.

BaoBao’s magic is limited, after the first stage, he needs to know the minimum capacity of the magic washing basin that he needs to pair all the socks successfully. The capacity of the basin is the maximum number of pairs of socks BaoBao can put in the magic washing basin at one time.
Input
The input contains multiple cases. The first line of the input contains a single positive integer T (1≤T≤10), the number of cases.

For each case, the first line of the input contains a single integer n (1≤n≤105), the number of pairs of socks.

For the following n lines, the i-th (1≤i≤n) line contains two integers ai and bi (1≤ai,bi≤230), denoting the colours of the two socks in the i-th pair after the first stage. It is guaranteed that for each sock, there is exactly one other sock of the same colour.
Output
For each case, print a single line containing a single integer, the minimum capacity of the magic washing basin.
Example
Input
1
5
1 2
2 3
1 3
4 5
4 5
Output
3
题意:将成双的袜子随机放入魔法机,只能成双地洗,输出洗衣机能洗袜子最大的双数。

解题:因为袜子是成双的,所以可以证明肯定是能成环的,这道题就是让找出最大的环。而且两个环之间不交叉。

#include <iostream>
#include <unordered_map>
#define ll long long
#define MAX 100010
using namespace std;
unordered_map <ll,ll> F,M;
ll a[MAX],b[MAX];
ll found(ll x)//并查集路径压缩,直接将父节点更新为祖宗结点,减少后续查找的时间
{
    if(F[x]==x) return x;
    else
        return F[x]=found(F[x]);
        //找根支点,运用递归,直接找到x的根支点,将根支点赋值给F[x],使x的上级直接是根支点
}
int main()
{
    ios::sync_with_stdio(false);
    ll t;
    cin>>t;
    while(t--)
    {
        F.clear();
        M.clear();
        //map,清空用法很方便
        ll n;
        cin>>n;
        for(int i=0; i<n; i++)
        {
            cin>>a[i]>>b[i];
            F[a[i]]=a[i];
            F[b[i]]=b[i];//自己是自己的上级
        }
       //cout << a[0] << endl<<F[a[0]]<<' '<<found(a[0])<<endl;
        for(int i=0; i<n; i++)
        {
            if(found(a[i])!=found(b[i]))//一次放入两个,判断两个的上级是否相同
                F[found(a[i])]=found(b[i]);//不同的话,果断将b[i]的上级先赋值给a[i],使他们拥有相同的上级
            //cout << i<<' '<<F[a[0]] << endl;
        }
        //数据使用该题样例,经过以上操作 found(a[0],a[1],a[2])=3 found(a[3],a[4])=5
       ll ma=0;
       // cout << F[a[0]] << endl;
        for(int i=0; i<n; i++)
        {
            //cout << i<<" "<<M[found(a[i])]<< endl;
            M[found(a[i])]++;
            M[found(b[i])]++;
            ma=max(ma,M[found(a[i])]);//记录一下根节点也就是祖先出现的次数,找到最大值
            //cout << i<<' '<<found(a[i])<<' '<<M[found(a[i])]<<" "<<ma<< endl;
            ma=max(ma,M[found(b[i])]);
            //cout << i<<" "<<found(b[i])<<' '<<M[found(b[i])]<<' '<<ma<< endl;
        }
        /*for(int i=0; i<n; i++)
        {
           cout<<found(a[i])<<' '<<M[found(a[i])]<<endl;
           cout<<found(b[i])<<' '<<M[found(b[i])]<<endl;
        }*/
        cout<<ma/2<<endl;//除以二就是找到的最后的双数
    }
    return 0;
}

(第一次详细地分析并查集,还有点糊涂,再接再厉!)点击查看并查集基础用法
还有博主用了离散化,看了半天不会用。离散化学习

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值