Invoking the Magic(并查集+离散化)

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 <bits/stdc++.h>
using namespace std;
const int Max = 1e5 + 10;
int seach[Max];

int Find(int x) 
{
    while (x != seach[x])
        x = seach[x];
    return x;
}
void Merge(int a, int b) 
{
    if (Find(a) != Find(b)) 
    seach[Find(a)] = Find(b);
    return;
}
int main() {
    int l[Max], r[Max], a[2 * Max];
    int T, i, n, cnt, j, ans, t1, t2;
    scanf("%d", &T);
    while (T--) 
    {
        j = 0;
        scanf("%d", &n);
        for (i = 0; i < n; i++) 
            seach[i] = i;
        for (i = 0; i < n; i++) 
        {
            scanf("%d%d", &l[i], &r[i]);
            a[j++] = l[i];
            a[j++] = r[i];
        }
        sort(a, a + j);
        cnt = unique(a, a + j) - a;
        for (i = 0; i < n; i++) 
        {
            t1 = lower_bound(a, a + cnt, l[i]) - a;
            t2 = lower_bound(a, a + cnt, r[i]) - a;
            Merge(t1, t2);
        }
        ans = 0;
        int s[Max] = {0};
        for (i = 0; i < cnt; i++) 
        {
            s[Find(i)]++;
            ans = max(ans, s[Find(i)]);
        }
        printf("%d\n", ans);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值