热门标签 | 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 }

 


推荐阅读
  • 深入理解CSS中的margin属性及其应用场景
    本文主要介绍了CSS中的margin属性及其应用场景,包括垂直外边距合并、padding的使用时机、行内替换元素与费替换元素的区别、margin的基线、盒子的物理大小、显示大小、逻辑大小等知识点。通过深入理解这些概念,读者可以更好地掌握margin的用法和原理。同时,文中提供了一些相关的文档和规范供读者参考。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • CentOS 7部署KVM虚拟化环境之一架构介绍
    本文介绍了CentOS 7部署KVM虚拟化环境的架构,详细解释了虚拟化技术的概念和原理,包括全虚拟化和半虚拟化。同时介绍了虚拟机的概念和虚拟化软件的作用。 ... [详细]
  • 本文介绍了一种解析GRE报文长度的方法,通过分析GRE报文头中的标志位来计算报文长度。具体实现步骤包括获取GRE报文头指针、提取标志位、计算报文长度等。该方法可以帮助用户准确地获取GRE报文的长度信息。 ... [详细]
  • CentOS 6.5安装VMware Tools及共享文件夹显示问题解决方法
    本文介绍了在CentOS 6.5上安装VMware Tools及解决共享文件夹显示问题的方法。包括清空CD/DVD使用的ISO镜像文件、创建挂载目录、改变光驱设备的读写权限等步骤。最后给出了拷贝解压VMware Tools的操作。 ... [详细]
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社区 版权所有