Friends(采用树结构的非线性表编程)
2014-07-28 16:38
274 查看
Description
too.
Your task is to count how many people there are in the largest group of friends.
of pairs of people (0≤M≤500000), which are known to be friends. Each of the following M lines consists of two integers A and B (1≤A≤N, 1≤B≤N, A≠B) which describe that A and B are friends. There could be repetitions among the given pairs.
#include <iostream>
#include <algorithm>
using namespace std;
int p[30000],c[30000];
int find(int x)
{return p[x]==x ? x : p[x]=find(p[x]);
}
int main()
{
int t,i,x,y;
cin>>t;
while(t--)
{
int m,n;
cin>>m>>n;
for(i=1;i<=m;i++)
{p[i]=i;c[i]=0;}
for(i=1;i<=n;i++)
{
cin>>x>>y;
x=find(x);
y=find(y);
if(x!=y)
p[x]=y;
}
for(i=1;i<=m;i++)
{x=find(i);
c[x]++;
}
int max=0;
for(int i=1; i<=m; i++)
if(c[i]>max)
max=c[i];
cout<<max<<endl;
}
return 0;
}
Problem I
Friends
There is a town with N citizens. It is known that some pairs of people are friends. According to the famous saying that “The friends of my friends are my friends, too” it follows that if A and B are friends and B and C are friends then A and C are friends,too.
Your task is to count how many people there are in the largest group of friends.
Input
Input consists of several datasets. The first line of the input consists of a line with the number of test cases to follow. The first line of each dataset contains tho numbers N and M, where N is the number of town's citizens (1≤N≤30000) and M is the numberof pairs of people (0≤M≤500000), which are known to be friends. Each of the following M lines consists of two integers A and B (1≤A≤N, 1≤B≤N, A≠B) which describe that A and B are friends. There could be repetitions among the given pairs.
Output
The output for each test case should contain one number denoting how many people there are in the largest group of friends.Sample Input | Sample Output |
2 3 2 1 2 2 3 10 12 1 2 3 1 3 4 5 4 3 5 4 6 5 2 2 1 7 10 1 2 9 10 8 9 | 3 6 |
#include <algorithm>
using namespace std;
int p[30000],c[30000];
int find(int x)
{return p[x]==x ? x : p[x]=find(p[x]);
}
int main()
{
int t,i,x,y;
cin>>t;
while(t--)
{
int m,n;
cin>>m>>n;
for(i=1;i<=m;i++)
{p[i]=i;c[i]=0;}
for(i=1;i<=n;i++)
{
cin>>x>>y;
x=find(x);
y=find(y);
if(x!=y)
p[x]=y;
}
for(i=1;i<=m;i++)
{x=find(i);
c[x]++;
}
int max=0;
for(int i=1; i<=m; i++)
if(c[i]>max)
max=c[i];
cout<<max<<endl;
}
return 0;
}
相关文章推荐
- POJ 1470 Closest Common Ancestors 采用树结构的非线性表编程
- 三层体系结构与数据库编程
- ASP编程中树型结构的提交
- 读林锐博士 高质量C++/C编程指南 笔记 2006-11-15第一章文件结构
- 高质量C++/C编程指南 -- 第1章 文件结构
- 使用 COM+ 参数化对象结构编程技术
- 使用 COM+ 参数化对象结构编程技术
- 三层体系结构与数据库编程的教程下载
- SQL Server 2005编程入门经典(第2版) 部分章节下载 存储结构-触发器 PDF版本
- Linux 网络编程常用函数和结构速查
- 多层结构+存储过程应用的记录!采用Duwamish7的方法!
- 第三章 Java基本编程结构
- Chap 6 学习笔记-控制结构和过程式编程
- 高质量C++/C编程指南 -- 第1章 文件结构
- 删除内存树结构的时候,千万注意内存泄漏问题,采用递归比较简单。
- 三层体系结构与数据库 编程
- OpenGL基础图形编程 - OpenGL基本程序结构
- 系统托盘编程中的关键结构
- 高质量C++/C编程指南 -- 第1章 文件结构
- C# 高级编程 - .NET结构