题目链接:http://codeup.cn/problem.php?cid=100000615&pid=3
此前题解:https://blog.csdn.net/qian2213762498/article/details/81977278
这题和HDU 1856是一个题哦~~
问题 D: More is better
时间限制: 1 Sec 内存限制: 128 MB
提交: 136 解决: 45
[提交][状态][讨论版][命题人:外部导入]
题目描述
Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be. Of course there are certain requirements.Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang's selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.
输入
The first line of the input contains an integer n (0 ≤ n ≤ 100 000) - the number of direct friend-pairs. The following n lines each contains a pair of numbers A and B separated by a single space that suggests A and B are direct friends. (A ≠ B, 1 ≤ A, B ≤ 10000000)
输出
The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.
样例输入
3
1 3
1 5
2 5
4
3 2
3 4
1 6
2 6
样例输出
4
5
思路
求最大连通块的元素个数:
我的最初思路是利用isRoot数组,用hash的方式,记录下每个连通块的元素个数;
不过这种方法时间、和内存都消耗的很大,最后超时了(TLE)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int nmax=10000000+10;
int father[nmax];
int isRoot[nmax];
int findFather(int u){
if(u==father[u]) return u;
else{
int f=findFather(father[u]);
father[u]=f;
return f;
}
}
void Union(int u,int v){
int fu=findFather(u);
int fv=findFather(v);
if(fu!=fv){
father[fu]=fv;
}
}
void init(int n){
for(int i=1;i<=n;i++){
father[i]=i;
isRoot[i]=0;
}
}
int main(int argc, char** argv) {
int m;//m条边
while(cin>>m){
memset(father,0,sizeof(father));
memset(isRoot,0,sizeof(isRoot));
init(nmax);
int u,v;
for(int i=0;i<m;i++){
cin>>u>>v;
Union(u,v);
}
for(int i=1;i<=nmax;i++){
isRoot[findFather(i)]++;
}
int ans=-1;
for(int i=1;i<=nmax;i++){
if(ans<isRoot[i]){
ans=isRoot[i];
}
}
//int ans=*max_element(isRoot+1,isRoot+nmax);
cout<<ans<<endl;
}
return 0;
}
于是就要改变Union的策略,在合并集合元素时,用sum数组同步更新计算最大集合元素值。
以下是:
AC Code
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int nmax=10000000+10;
int father[nmax];
int sum[nmax];//sum[i]表示:节点i所在的连通块的节点个数
int ans=0; //最大连通块的节点个数
//int isRoot[nmax];
int findFather(int u){
if(u==father[u]) return u;
else{
int f=findFather(father[u]);
father[u]=f;
return f;
}
}
void Union(int u,int v){
int fu=findFather(u);
int fv=findFather(v);
if(fu!=fv){
father[fv]=fu;//fu是fv的根
sum[fu]=sum[fu]+sum[fv];
ans=max(ans,sum[fu]);
}
}
void init(int n){
for(int i=1;i<=n;i++){
father[i]=i;
sum[i]=1;//每个节点权值为1
}
}
int main(int argc, char** argv) {
int m;//m条边
ios_base::sync_with_stdio(false);
while(cin>>m){
if(m==0){
cout<<"1"<<endl;
continue;
}
memset(father,0,sizeof(father));
//memset(isRoot,0,sizeof(isRoot));
init(nmax);
int u,v;
ans=0;
for(int i=0;i<m;i++){
cin>>u>>v;
Union(u,v);
}
cout<<ans<<endl;
}
return 0;
}