HDOJ-1856 More is better&&POJ-1611(并查集)
2016-07-12 10:36
381 查看
More is better
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others)Total Submission(s): 22851 Accepted Submission(s): 8297
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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)
[align=left]Output[/align]
The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.
[align=left]Sample Input[/align]
4
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8
[align=left]Sample Output[/align]
4
2
Hint
A and B are friends(direct or indirect), B and C are friends(direct or indirect),
then A and C are also friends(indirect).
In the first sample {1,2,5,6} is the result.
In the second sample {1,2},{3,4},{5,6},{7,8} are four kinds of answers.
明明就是判断所有树里面点的个数的最大值,为什么单单线性扫一遍会wa,在合并两子集的函数中累加可以ac,是我理解错了吗,求解答
ac代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 10000005; int f[maxn]; int sum[maxn]; void Init() { for(int i = 1;i <= maxn;i++) { f[i] = i; sum[i] =1; } } int getf(int t) { if(f[t] == t) return t; else { f[t] = getf(f[t]);//状态压缩 return f[t]; } } void merge(int u,int v) { int t1 = getf(u); int t2 = getf(v); if(t1 != t2) { f[t2] = t1; //靠左原则 sum[t1] += sum[t2]; } } int main() { int n,x,y; while(~scanf("%d",&n)) { if(n == 0) { cout<<1<<endl; continue; } Init(); for(int i = 0;i < n;i++) { scanf("%d%d",&x, &y); merge(x, y); } int ans = 0; for(int i = 1;i <= maxn;i++) ans = max(ans,sum[i]); cout<<ans<<endl; } return 0; }
wa代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 10000005; int f[maxn]; int sum[maxn]; void Init() { for(int i = 1;i <= maxn;i++) f[i] = i; memset(sum,0,sizeof(sum)); } int getf(int t) { if(f[t] == t) return t; else { f[t] = getf(f[t]);//状态压缩 return f[t]; } } void merge(int u,int v) { int t1 = getf(u); int t2 = getf(v); if(t1 != t2) { f[t2] = t1; //靠左原则 } } int main() { int n,x,y; while(~scanf("%d",&n)) { if(n == 0) { cout<<1<<endl; continue; } Init(); for(int i = 0;i < n;i++) { scanf("%d%d",&x, &y); merge(x, y); } for(int i = 1;i <= maxn;i++) sum[f[i]]++; int maximum = 0; for(int i = 1;i <= maxn;i++) maximum = max(maximum, sum[i]); printf("%d\n",maximum); } return 0; }
POJ-1611
#include<iostream>
#include<cstdio>
using namespace std;
int n,m,k;
int f[30005],sum[30005];
int getf(int u)
{
if(u == f[u])
return u;
else
{
f[u] = getf(f[u]);
return f[u];
}
}
void merge(int u,int v)
{
u = getf(u);
v = getf(v);
if(u != v)
{
f[v] = u;
sum[u] += sum[v];
//靠左
}
}
int main()
{
int p,q;
while(~scanf("%d%d",&n,&m)&&(n||m))
{
for(int i = 0;i < n;i++)
{
f[i] = i;
sum[i] = 1;
}
for(int i = 1;i <= m;i++)
{
scanf("%d",&k);
k--;
scanf("%d",&p);
while(k--)
{
scanf("%d",&q);
merge(p,q);
}
}
printf("%d\n",sum[getf(0)]);//这里的getf(0)不能替换
//成f[0],因为此时还未进行路径压缩
}
return 0;
}
相关文章推荐
- 个人学习-java-JDBC事务的操作
- OSG 基础 学习心得
- [转帖]易助8注册机 +免狗补丁 支持32位和64位
- Redis 缓存 + Spring 的集成示例
- Swift学习笔记 - 教程学习四 控制流(Control Flow)
- 自定义TextView滚动
- 团体程序设计天梯赛-练习集 L2-001. 紧急救援
- Android Tcp客户端发送数据
- php中curl模拟post提交多维数组
- Ubuntu下MongoDB的安装和使用
- nodejs的express自动生成项目框架-2016
- [C]记录C语言中由于粗心遇到的奇葩错误.
- Android获取View的宽高与View.measure详解
- javascript运行过程中的“预编译阶段”和“执行阶段”
- XOCDE7 '_compress2', referenced from:
- C#中的委托机制
- Java序列化和反序列化
- 删了/etc/environment之后不能进入界面了
- mysql日期时间类函数
- nodejs之log4js日志记录模块简单配置使用