Forwards on Weibo (30)
BFS,题意比较难懂,是求离query L层的总共人数
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <queue> using namespace std; const int N = 1005; struct E
{
int node;
};
vector<E> v[N]; int mark[N]; void add_edge(int a, int b)
{
E tmp;
tmp.node = b; v[a].push_back(tmp);
} struct ANS
{
int num;
int lev;
}; void BFS(int x, int ll)
{
queue<ANS> q;
ANS tmp;
tmp.lev = 0;
tmp.num = x;
q.push(tmp);
mark[x] = 1; int level = 0;
int ans = 0;
while (!q.empty())
{
ANS top = q.front(); int node = top.num;
int lev = top.lev;
if (lev >= ll) break;
q.pop(); for (int i = 0; i < v[node].size(); i++)
{
if (mark[v[node][i].node] == 0)
{
mark[v[node][i].node] = 1; tmp.lev = lev + 1;
tmp.num = v[node][i].node; //printf("%d %d\n", tmp.num, tmp.lev);
ans++;
q.push(tmp);
}
}
} printf("%d\n", ans);
} int main()
{
int n, ll; while (scanf("%d%d", &n, &ll) != EOF)
{
for (int i = 1; i <= n; i++)
{
int k, a;
scanf("%d", &k);
while (k--)
{
scanf("%d", &a);
add_edge(a, i);
}
}
int query_num, query;
scanf("%d", &query_num);
while (query_num--)
{
scanf("%d", &query);
memset(mark, 0, sizeof(mark));
BFS(query, ll);
}
}
return 0;
}
Forwards on Weibo (30)的更多相关文章
- 1076. Forwards on Weibo (30)【树+搜索】——PAT (Advanced Level) Practise
题目信息 1076. Forwards on Weibo (30) 时间限制3000 ms 内存限制65536 kB 代码长度限制16000 B Weibo is known as the Chine ...
- PAT 甲级 1076 Forwards on Weibo (30分)(bfs较简单)
1076 Forwards on Weibo (30分) Weibo is known as the Chinese version of Twitter. One user on Weibo m ...
- 1076. Forwards on Weibo (30)
时间限制 3000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Weibo is known as the Chinese v ...
- PAT 1076. Forwards on Weibo (30)
Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may ...
- 1076. Forwards on Weibo (30) - 记录层的BFS改进
题目如下: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, a ...
- 1076 Forwards on Weibo (30)(30 分)
Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may ...
- PAT Advanced 1076 Forwards on Weibo (30) [图的遍历,BFS,DFS]
题目 Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and ...
- PAT (Advanced Level) 1076. Forwards on Weibo (30)
最短路. 每次询问的点当做起点,然后算一下点到其余点的最短路.然后统计一下最短路小于等于L的点有几个. #include<cstdio> #include<cstring> # ...
- PAT甲题题解-1076. Forwards on Weibo (30)-BFS
题目大意:给出每个用户id关注的人,和转发最多的层数L,求一个id发了条微博最多会有多少个人转发,每个人只考虑转发一次.用BFS,同时每个节点要记录下所在的层数,由于只能转发一次,所以每个节点要用vi ...
随机推荐
- Google Protocol Buffer 的使用
简介 Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162 种报文格式定义和超过 12,183 ...
- C# iis 错误配置信息( CS0016: 未能写入输出文件 )
IIS发布时,编译器错误消息: CS0016: 未能写入输出文件 "c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.N ...
- LR常用函数以及调用自定义函数
2.LR常用函数以及调用自定义函数 2.1.LR常用函数以及对信息的判断 2.1.1. LR内部自定义函数 在LR脚本中定义变量和编写自定义函数,需将变量的声明放在脚本其他内容的上方,否则会提示[il ...
- SQL Server 常用命令使用方法
(1) 数据记录筛选: sql="select * from 数据表 where 字段名=字段值 order by 字段名 [desc]" sql="select * f ...
- 并发下常见的加锁及锁的PHP具体实现-转载
并发下常见的加锁及锁的PHP具体实现 http://www.cnblogs.com/scotoma/archive/2010/09/26/1836312.html 在最近的项目中有这样的场景 1.生成 ...
- 如何写一个简单的shell
如何写一个简单的shell 看完<UNIX环境高级编程>后我就一直想写一个简单的shell来作为练习,因为有事断断续续的写了好几个月,如今写了差不多来总结一下. 源代码放在了Github: ...
- linux 驱动学习笔记03--Linux 内核的引导
如图所示为 X86 PC 上从上电/复位到运行 Linux 用户空间初始进程的流程.在进入与 Linux相关代码之间,会经历如下阶段. ( 1 ) 当系统上电或复位时, CPU 会将 PC 指针赋值为 ...
- delphi中midas是什么
Delphi中MIDAS到底是什么呢?和他相关组件是什么呢? MIDAS(Multitiered Distributed Application Services)多层分布式应用服务. Del ...
- linux下mv命令使用方法
1.作用mv命令来为文件或目录改名或将文件由一个目录移入另一个目录中.该命令等同于DOS系统下的ren和move命令的组合.它的使用权限是所有用户.2.格式mv [options] 源文件或目录 目标 ...
- vc++ mfc中拖动效果的实现 借助于CImageList
拖动是界面编程频繁使用的一个效果,在windows系统下可谓大行其道.纵观时下的应用软件几乎各个都支持各种各样拖动的效果,windows7更是把拖动做到了极致.其实说起来拖动的实现也很简单,对于有句柄 ...