I - Invoking the Magic Gym - 102770I补题

BaoBao is a lazy boy. He has nn 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 TT (1≤T≤101≤T≤10), the number of cases.

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

For the following nn lines, the ii-th (1≤i≤n1≤i≤n) line contains two integers aiai and bibi (1≤ai,bi≤230)(1≤ai,bi≤230), denoting the colours of the two socks in the ii-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
Note
In the sample, BaoBao can first put these three pairs of socks: {1,2}{2,3}{1,3} in the magic washing basin and then they will be paired into {1,1}{2,2}{3,3}. Then, he can put {4,5}{4,5} in the magic washing basin and they will be paired into {4,4}{5,5}. Therefore, with a capacity of 33 it is possible to pair all the socks successfully. It can be shown by brute-force that this is impossible with a capacity of 22.
题意,有n双,袜子,初始状态给你n对随机配对的袜子,说有一个魔法洗衣机,你把袜子放进去,他就可以自动给你配对,但是要保证放进去的袜子可以恰好配对,也就是不能有无法配对的袜子,问洗衣机的容量(就是一次可以处理袜子的双数)最少是多少
思路:这个题目用到并查集合。为什么呢?因为并查集合是把所有有关系的节点连接起来,在这里也就是每次把随机配对的两只袜子加入并查集,假如,当前一对袜子中有一只或者两只之前加入过一个并查集,那么这只已经加入进去的袜子肯定要进去和那一只袜子配对,这就要求把当前这双袜子加进去,也就是把没加进那个并查集的袜子加进去。最终输出的值就是并查集的节点值,也就是有几双袜子。
上代码
#include
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1000010;

ll pre[maxn];
ll num[maxn];
ll a[maxn];
ll b[maxn];
ll s[maxn];
ll n, tot;
ll maxx;
ll findd(ll x)
{
if(pre[x] == x)
return x;
else
return pre[x] = findd(pre[x]);
}
void unionn(ll x, ll y)
{
ll xx = findd(x);
ll yy = findd(y);
if(xx != yy)
{
pre[xx] = yy;
num[yy] += num[xx];
}
}
int main()
{
int t;
scanf("%d", &t);
while(t–)
{
maxx = -1;
tot = 0;
scanf("%lld", &n);
for(int i=0; i<n; i++)
{
scanf("%lld %lld", &a[i], &b[i]);
s[tot++] = a[i];
s[tot++] = b[i];
}
sort(s, s+tot);
tot = unique(s, s+tot) - s;//删除相同的元素
for(int i=0; i<=tot; i++)
{
pre[i]=i;
num[i]=1;
}
for(int i=0; i<n; i++)
{
int u = lower_bound(s, s+tot, a[i]) - s;//第几个元素(相当于离散化之后的)
int v = lower_bound(s, s+tot, b[i]) - s;
unionn(u, v);
}
for(int i=0; i<tot; i++)
{
if(findd(i)==i)
maxx = maxx > num[i] ? maxx : num[i];
}
printf("%lld\n", maxx);
}
return 0;
}
最后补充几个函数的用法;用于离散化
1:unique函数,用来去重
https://www.cnblogs.com/wangkundentisy/p/9033782.html
2.lower_bound()用法
https://www.cnblogs.com/Tang-tangt/p/9291018.html
3.离散化
https://blog.csdn.net/justidle/article/details/104518292?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522160351649519724838508856%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=160351649519724838508856&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~rank_v28-1-104518292.pc_first_rank_v2_rank_v28&utm_term=%E7%A6%BB%E6%95%A3%E5%8C%96&spm=1018.2118.3001.4187

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值