PAT T1014 Circles of Friends
大水题,dfs判连通块的数量,bfs每个点找朋友圈的最大直径~
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
vector<int> g[maxn];
bool visit[maxn];
int N;
int maxdepth=;
void dfs (int s) {
visit[s]=true;
for (int i=;i<g[s].size();i++)
if (visit[g[s][i]]==false) dfs(g[s][i]);
}
int dfsTrave () {
int block=;
for (int i=;i<=N;i++)
if (visit[i]==false) dfs(i),block++;
return block;
}
void bfs (int s) {
int depth[maxn]={};
queue<int> q;
fill (visit,visit+maxn,false);
q.push(s);
visit[s]=true;
while (!q.empty()) {
int now=q.front();
q.pop();
for (int i=;i<g[now].size();i++)
if (visit[g[now][i]]==false) {
q.push(g[now][i]);
depth[g[now][i]]=depth[now]+;
visit[g[now][i]]=true;
maxdepth=max(maxdepth,depth[g[now][i]]);
}
}
}
int main () {
scanf ("%d",&N);
int k,x;
for (int i=;i<=N;i++) {
scanf ("%d",&k);
for (int j=;j<=k;j++) {
scanf ("%d",&x);
g[i].push_back(x);
g[x].push_back(i);
}
}
int block=dfsTrave();
for (int i=;i<=N;i++) bfs (i);
printf ("%d %d",block,max(,maxdepth-));
return ;
}
PAT T1014 Circles of Friends的更多相关文章
- 《转载》PAT 习题
博客出处:http://blog.csdn.net/zhoufenqin/article/details/50497791 题目出处:https://www.patest.cn/contests/pa ...
- PAT Judge
原题连接:https://pta.patest.cn/pta/test/16/exam/4/question/677 题目如下: The ranklist of PAT is generated fr ...
- PAT/字符串处理习题集(二)
B1024. 科学计数法 (20) Description: 科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+ ...
- PAT 1041. 考试座位号(15)
每个PAT考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位.正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座 ...
- PAT 1040. 有几个PAT(25)
字符串APPAPT中包含了两个单词"PAT",其中第一个PAT是第2位(P),第4位(A),第6位(T):第二个PAT是第3位(P),第4位(A),第6位(T). 现给定字符串,问 ...
- PAT 1032. 挖掘机技术哪家强(20)
为了用事实说明挖掘机技术到底哪家强,PAT组织了一场挖掘机技能大赛.现请你根据比赛结果统计出技术最强的那个学校. 输入格式: 输入在第1行给出不超过105的正整数N,即参赛人数.随后N行,每行给出一位 ...
- pat甲级题解(更新到1013)
1001. A+B Format (20) 注意负数,没别的了. 用scanf来补 前导0 和 前导的空格 很方便. #include <iostream> #include <cs ...
- PAT (Basic Level) Practise 1040 有几个PAT(DP)
1040. 有几个PAT(25) 时间限制 120 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CAO, Peng 字符串APPAPT中包含了两个单 ...
- PAT (Basic Level) Practise 1045 快速排序(离散化+主席树区间内的区间求和)
1045. 快速排序(25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CAO, Peng 著名的快速排序算法里有一个经典的划分 ...
随机推荐
- 联合查询:union
1.联合查询:union 1.1 作用:将多条select语句的结果,合并到一起,称之为联合操作. 1.2 语法:( ) union ( ); 例子:(select name from info_or ...
- power-plan如何定
Power-Plan或者说PG如何打,这是一个仁者见仁智者见智的问题,没有一个标准的答案,因为有各种各样的影响因素.本文将列举一些可能的影响因素: 1.和design 相关 1) Utilizati ...
- md5模块(Python内置模块)和hashlib模块
转自https://my.oschina.net/duhaizhang/blog/67214 MD5模块用于产生消息摘要,康用来判断文件是否相同. python的md5模块使用非常简单,包括以下几个函 ...
- CSS - div中的文字不换行,超出宽度就用省略号表示
问题 过多的文字会把盒子撑开,造成布局错乱. 解决 .card-title { white-space: nowrap; text-overflow: ellipsis; overflow: hidd ...
- laravel 模型观察器
模型观察器 对模型的生命周期内的多个时间点进行监控,分别有 ~ing 和 ~ed 事件 每个监控方法接收 model 作为唯一参数 使用观察器 创建观察器文件,一个普通类,不需要继承什么 针对需要的事 ...
- html学习3-CSS补充
position fixed:把标签固定在页面的某处 例子:使用fixed制作“回到顶部”按钮 <!DOCTYPE html> <html lang="en"&g ...
- bootstrap下拉选择框倒三角所占宽度
<select id="edit" class="form-control" style="width:42%;padding-right: 3 ...
- js正则验证表达式验证
/* 合法uri */ export function validateURL(textval) { const urlregex = /^(?:http(s)?:\/\/)?[\w.-]+(?:\ ...
- netty(八)buffer源码学习3
问题 : compositeByteBuf 是干什么和其他 compositeByteBuf 有何区别 内部实现 概述 compositeByteBuf 就像数据库中的视图,把几个表的字段组合在一起, ...
- POJ 2142 The Balance(exgcd)
嗯... 题目链接:http://poj.org/problem?id=2142 AC代码: #include<cstdio> #include<iostream> using ...