热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

CirclesofFriends

Acircleoffriendsisanetworkoffriendrelationships.IfAisafriendofB,thenBisconsideredafriendof

A circle of friends is a network of friend relationships. If A is a friend of B, then B is considered a friend of A no matter B admits or not, and they are said to belong to the same circle. Here we assume that friendship is transitive, that is, if A is a friend of B, and B is a friend of C, then A is a friend of C and the three of them all belong to the same circle.

On the other hand, A is not so close to C as B is. We define the distance D(X, Y) between two friends X and Y as the minimum number of friends between them. For example, D(A, B) = 0, and D(C, A) = 1. The diameter of a friends circle is the maximum distance between any pair of friends in the circle.

Now given some people‘s relationships, you are supposed to find the number of friends circles and the circle with the largest diameter.


Input Specification:

Each input file contains one test case. For each case, the first line gives an integer N (2), which is the total number of people involved, and hence they are numbered from 1 to N. Then N lines follow, each in the format:

p?1?? ... p?k??

where k (0) is the number of friends and p?1?? to p?k?? (if k>0) are the friends‘ indices. The i-th line corresponds to the i-th person. All the numbers in a line are separated by spaces. It is guaranteed that no one is given as a friend of oneself.


Output Specification:

For each case, print in a line the number of friends circles, and the largest diameter, separated by exactly one space.


Sample Input:

17
2 15 12
1 17
2 16 9
1 8
4 10 13 15 14
0
2 11 14
1 4
2 2 3
2 13 11
2 15 7
2 1 14
2 5 15
0
0
1 3
1 2

 

Sample Output:

4 3

1 #include
2 using namespace std;
3 vector<set<int> > v;
4 vector<bool> vb;
5 vector<int> vr;
6 set<int>::iterator its;
7 int spart,smax;
8 void bfs(int n,bool flag)
9 {
10 vector<bool> vbb(vb.size(),true);
11 int md=0;
12 pair<int,int> p;
13 queueint,int> > q;
14 q.push(pair<int,int>(n,-1));
15 if(flag)
16 vb[n]=false;
17 else
18 vbb[n]=false;
19 for(;!q.empty();q.pop())
20 {
21 p=q.front();
22 if(p.second>md)
23 {
24 md=p.second;
25 if(flag)
26 {
27 vr.clear();
28 vr.push_back(p.first);
29 }
30 }
31 else if(p.secOnd==md)
32 if(flag)
33 vr.push_back(p.first);
34 for(its=v[p.first].begin();its!=v[p.first].end();++its)
35 {
36 if(flag)
37 {
38 if(vb[*its])
39 {
40 vb[*its]=false;
41 q.push(pair<int,int>(*its,p.second+1));
42 }
43 }
44 else
45 {
46 if(vbb[*its])
47 {
48 vbb[*its]=false;
49 q.push(pair<int,int>(*its,p.second+1));
50 }
51 }
52 }
53 }
54 if(md>smax)
55 smax=md;
56 return;
57 }
58 int main()
59 {
60 // ios::sync_with_stdio(false);
61 // freopen("data.txt","r",stdin);
62 int n,k,x;
63 spart=0,smax=0;
64 scanf("%d",&n);
65 v.resize(n);
66 vb.resize(n,true);
67 for(int i=0;i)
68 {
69 scanf("%d",&k);
70 for(;k--;)
71 {
72 scanf("%d",&x);
73 x--;
74 v[i].insert(x);
75 v[x].insert(i);
76 }
77 }
78 for(int t=0;t)
79 {
80 if(vb[t])
81 {
82 bfs(t,true);
83 for(int i=0;i)
84 bfs(vr[i],false);
85 spart++;
86 t=0;
87 }
88 }
89 printf("%d %d",spart,smax);
90
91 return 0;
92 }

 


推荐阅读
  • QUIC协议:快速UDP互联网连接
    QUIC(Quick UDP Internet Connections)是谷歌开发的一种旨在提高网络性能和安全性的传输层协议。它基于UDP,并结合了TLS级别的安全性,提供了更高效、更可靠的互联网通信方式。 ... [详细]
  • 深入理解OAuth认证机制
    本文介绍了OAuth认证协议的核心概念及其工作原理。OAuth是一种开放标准,旨在为第三方应用提供安全的用户资源访问授权,同时确保用户的账户信息(如用户名和密码)不会暴露给第三方。 ... [详细]
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 深入解析Android自定义View面试题
    本文探讨了Android Launcher开发中自定义View的重要性,并通过一道经典的面试题,帮助开发者更好地理解自定义View的实现细节。文章不仅涵盖了基础知识,还提供了实际操作建议。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 深入理解 Oracle 存储函数:计算员工年收入
    本文介绍如何使用 Oracle 存储函数查询特定员工的年收入。我们将详细解释存储函数的创建过程,并提供完整的代码示例。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • CSS 布局:液态三栏混合宽度布局
    本文介绍了如何使用 CSS 实现液态的三栏布局,其中各栏具有不同的宽度设置。通过调整容器和内容区域的属性,可以实现灵活且响应式的网页设计。 ... [详细]
  • 数据管理权威指南:《DAMA-DMBOK2 数据管理知识体系》
    本书提供了全面的数据管理职能、术语和最佳实践方法的标准行业解释,构建了数据管理的总体框架,为数据管理的发展奠定了坚实的理论基础。适合各类数据管理专业人士和相关领域的从业人员。 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 深入理解 SQL 视图、存储过程与事务
    本文详细介绍了SQL中的视图、存储过程和事务的概念及应用。视图为用户提供了一种灵活的数据查询方式,存储过程则封装了复杂的SQL逻辑,而事务确保了数据库操作的完整性和一致性。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
author-avatar
mobiledu2502894591
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有