HDU 1856 More is better
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others)
前话
离散化,把无限空间中有限的个体映射到有限的空间中去,以此提高算法的时空效率。
题意
- 房间里有编号为 [ 1 , 1e7 ] 的这些人,现在有n ( [ 0, 1e5 ] ) 对人认识,我们要留下来尽可能多的一批人,这些人直接或间接认识,问最后留在房间的最多能有多少人。
思路
- 因为所给的内存有限,所以我们无法开出1e7的数组,所以我们想到用离散化。我们将n对认识的人中出现的这些人编号放进set中,并存进real[ 1- cnt ]。然后我们遍历一遍这n对朋友,找到对应的real中对应的下标,然后将下标Merge。然后我们将所有结点的祖先结点放进multiset里面,然后找到子孙最多的结点,并输出这个最大值。
- End
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <list>
#include <map>
#define INF 0x3f3f3f3f
#define MID l + r >> 1
#define lsn rt << 1
#define rsn rt << 1 | 1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
using namespace std;
typedef long long ll;
const int maxN = 2e5 + 5;
int n, real[maxN];
int root[maxN], high[maxN], size[maxN];
int Find(int x) { return root[x] == x ? x : root[x] = Find(root[x]); }
int Same(int x, int y) { return Find(x) == Find(y); }
void Merge(int x, int y)
{
x = Find(x);
y = Find(y);
if(high[x] < high[y] || size[x] < size[y])
{
root[x] = y;
size[y] += size[x];
} else{
root[y] = x;
size[x] += size[y];
if(high[x] == high[y] ) high[x] ++;
}
}
void init()
{
for(int i = 1 ; i <= 2e5 ; i++)
{
root[i] = i;
high[i] = 0;
size[i] = 1;
}
}
struct node{
int a, b;
}bro[maxN];
int main()
{
while(~scanf("%d", &n))
{
init();
set<int>st;
for(int i = 1 ; i <= n ; i ++)
{
scanf("%d%d", &bro[i].a, &bro[i].b);
st.insert(bro[i].a);
st.insert(bro[i].b);
}
int cnt = 0;
while(!st.empty())
{
real[++ cnt] = *st.begin();
st.erase(st.begin());
}
for(int i = 1 ; i <= n; i++)
{
int aa = lower_bound(real + 1, real + 1 + cnt , bro[i].a) - real;
int bb = lower_bound(real + 1, real + 1 + cnt , bro[i].b) - real;
if(!Same(real[aa], real[bb]))
Merge(real[aa], real[bb]);
}
multiset<int>rt;
for(int i = 1 ; i <= cnt ; i++)
rt.insert(Find(real[i]));
int ans = 1;
while(!rt.empty())
{
int tmp = *rt.begin();
int num = rt.count(tmp);
ans = max(ans, num);
rt.erase(tmp);
}
printf("%d\n", ans);
}
return 0;
}
另
- 或许我对离散化有什么误解叭。在网上看了一篇这道题的题解,他是这样做的:
- 将所有n对的元素都直接用real存起来,然后sort排序,之后用了unique来找到去重后的末地址,更新有效元素个数cnt的值,之后找到最多子孙的那棵树,输出最多的结点数。
- 还做了一个优化就是,cnt更新完之后再初始化并查集所用的那些数组,减小了时间复杂度
- End
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <list>
#include <map>
#define INF 0x3f3f3f3f
#define MID l + r >> 1
#define lsn rt << 1
#define rsn rt << 1 | 1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
using namespace std;
typedef long long ll;
const int maxN = 2e5 + 5;
int n, real[maxN];
int root[maxN], high[maxN], size[maxN];
int Find(int x) { return root[x] == x ? x : root[x] = Find(root[x]); }
int Same(int x, int y) { return Find(x) == Find(y); }
void Merge(int x, int y)
{
x = Find(x);
y = Find(y);
if(high[x] < high[y] || size[x] < size[y])
{
root[x] = y;
size[y] += size[x];
} else{
root[y] = x;
size[x] += size[y];
if(high[x] == high[y] ) high[x] ++;
}
}
void init(int x)
{
for(int i = 0 ; i <= x ; i++)
{
root[i] = i;
high[i] = 0;
size[i] = 1;
}
}
struct node{
int a, b;
}bro[maxN];
int main()
{
while(~scanf("%d", &n))
{
int cnt = 0;
for(int i = 1 ; i <= n ; i ++)
{
scanf("%d%d", &bro[i].a, &bro[i].b);
real[cnt ++ ] = bro[i].a;
real[cnt ++ ] = bro[i].b;
}
sort(real, real + cnt);
cnt = unique(real, real + cnt) - real;
init(cnt);
for(int i = 1 ; i <= n; i++)
{
int aa = lower_bound(real , real + cnt , bro[i].a) - real;
int bb = lower_bound(real , real + cnt , bro[i].b) - real;
if(!Same(real[aa], real[bb]))
Merge(real[aa], real[bb]);
}
multiset<int>rt;
for(int i = 1 ; i <= cnt ; i++)
rt.insert(Find(real[i]));
int ans = 1;
while(!rt.empty())
{
int tmp = *rt.begin();
int num = rt.count(tmp);
ans = max(ans, num);
rt.erase(tmp);
}
printf("%d\n", ans);
}
return 0;
}