B - Friends(8.4.1)
来源:互联网 发布:道路里程数据库 编辑:程序博客网 时间:2024/06/01 14:33
Description
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 number 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.
Output
The output for each test case should contain one number denoting how many people there are in the largest group of friends.
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 <iostream>
#include <cstring>
using namespace std;
const int maxn=30005;
int set[maxn],s[maxn];
int set_find(int p)
{
if(set[p]<0)
return p;
return
set[p]=set_find(set[p]);
}
int main()
{
int t,n,m,a,b,x;
cin>>t;
while(t--)
{
cin>>n>>m;
int p,q;
memset(set,255,sizeof(set));
memset(s,0,sizeof(s));
for(int i=0;i<m;i++)
{
cin>>a>>b;
q=set_find(a);
p=set_find(b);
if(q!=p)
{
set[p]=q;
}
}
for(int k=1;k<=n;k++)
{
x=set_find(k);
s[x]++;
}
int max1=-1;
for(int j=1;j<=n;j++)
if(max1<s[j])
max1=s[j];
cout<<max1<<endl;
}
return 0;
}
优化后的代码:
#include <iostream>
#include <cstring>
using namespace std;
const int maxn=30005;
int set[maxn],s[maxn];
int set_find(int p)
{
if(set[p]<0)
return p;
return
set[p]=set_find(set[p]);
}
int main()
{
int t,n,m,a,b,x;
cin>>t;
while(t--)
{
cin>>n>>m;
int p,q;
memset(set,255,sizeof(set));
//memset(s,0,sizeof(s));
for(int l=0;l<=n;l++)
s[l]=1;
int max1=-1;
for(int i=0;i<m;i++)
{
cin>>a>>b;
q=set_find(a);
p=set_find(b);
if(q!=p)
{
set[p]=q;
s[q]+=s[p];
max1=(s[q]>max1)?s[q]:max1;
}
}
cout<<max1<<endl;
}
return 0;
}
- B - Friends(8.4.1)
- (并查集)B - Friends(8.4.1)
- B - Friends
- CF761 B. Dasha and friends(水题)
- B. Friends and Presents
- B. Dasha and friends
- B. Dasha and friends
- Codeforces 483B - Friends and Presents(二分)
- codeforces #483B# Friends and Presents(二分+math)
- CodeForces 658B Bear and Displayed Friends(树状数组)
- 文章标题 coderforces 761B : Dasha and friends(KMP)
- VK Cup 2016 - Round 1 (Div. 2 Edition)-B - Bear and Displayed Friends-STL(维护前k大)
- codefoeces B. Friends and Presents
- 【CODEFORCES】 B. Friends and Presents
- 761B Dasha and friends
- VK Cup 2016 - Round 1 (Div. 2 Edition) B B. Bear and Displayed Friends 优先队列
- Codeforces #275 (Div. 2) B - Friends and Presents(二分大法好)
- CodeForces 483B Friends and Presents(二分+容斥定理)
- ReportStudio入门教程(七十六) - JS拼接报表- 第2页显示表头
- 遍历Json
- hql的使用之小细节
- 关于android源码4.3 CTS测试的问题
- Android 4.2 Input Event事件处理流程<一>---应用注册
- B - Friends(8.4.1)
- 设计模式之观察者observer模式
- 关于tcpjava网络编程服务器端收不到信息
- 实模式与保护模式解惑之(一)——二者的起源与区别
- js ==与===区别(两个等号与三个等号)
- Tree Recovery(过遍历确定二叉树结构)
- 使用异步 I/O 大大提高应用程序的性能
- 华为编程大赛——高精度加减法
- 同是流行MVC框架,比较Strtus2和SpringMVC的区别