I - Invoking the Magic(并查集+map)

并查集(路径压缩)+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

题意

给你一些不同颜色的袜子,你要把它们放到一个容器中配对,如果能使所有的袜子配对成功,问这个容器最小的容量是多少。
比如:样例中:共5双袜子,先放前三双,这样就能完成1 1/2 2/3 3的配对,再后两双,就能全部配对成功,你也可以直接放5双在容器里,当然这显然不是最优的解。

AC代码

#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]);
}
int main()
{
    
    
    ios::sync_with_stdio(false);
    ll t;
    cin>>t;
    while(t--)
    {
    
    
        F.clear();
        M.clear();
        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];
        }
        for(int i=0; i<n; i++)
            if(found(a[i])!=found(b[i]))
                F[found(a[i])]=found(b[i]);
        ll ma=0;
        for(int i=0; i<n; i++)
        {
    
    
            M[found(a[i])]++;
            M[found(b[i])]++;
            ma=max(ma,M[found(a[i])]);
            ma=max(ma,M[found(b[i])]);
        }
        cout<<ma/2<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/rookie636/article/details/109270233
I