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 ...
随机推荐
- dynamic 的使用 待续
Dynamic 使用场景之一 : 替代反射 class Me { public string Blog { get; set; } public string GetName() { return&q ...
- C# 小例子
数组冒泡排序(升序排序): , , , , , , , , , , , }; //第一种 ; i < num.Length - ; i++) { ; j < num.Length - i ...
- 实例学习SQL的Select命令
1.查找员工的编号.姓名.部门和出生日期,如果出生日期为空值,--显示日期不详,并按部门排序输出,日期格式为yyyy-mm-dd.select emp_no ,emp_name ,dept ,isnu ...
- highchart访问一次后台服务返回多张图表数据
本文承接上一篇,我们制作动态图表的时候,往往需要的不止一张图表,如果每张图表都与服务接口做一次交互的话未免太过频繁,这无论对前后还是后台都是一种压力,本文介绍一种一次访问返回多组数据的方式来减少前台与 ...
- javascript照片墙效果
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- 操作素有的ul的元素
1 $(function() { var all = $(".test"); $(".test").each(function() { var y = $(th ...
- Device eth0 does not seem to be present, delaying initialization. 问题
今天在复制vmware的时候 出现网卡无法启动 报错显示 Device eth0 does not seem to be present, delaying initialization. 这个错误原 ...
- IntelliJ设置鼠标悬浮提示和修改快捷键
IntelliJ设置鼠标悬浮提示和修改快捷键 设置鼠标悬浮提示 修改快捷键 进入设置菜单 删除原来的快捷键(注:你可以选择保留原来的快捷键,同时使用两个快捷键) Good Luck
- 配置BUG-Linux系统下ssh登陆很慢的解决办法
很多的Linux用户发现连接上Linux服务器在输入用户名之后还要再等一下才能输入密码,时间过长了,现在小编与大家分享一下如何解决ssh登陆问题的问题,希望对您有所帮助 . 1.我们平时登陆Linux ...
- Endless Sky源码学习笔记-3
文本解析: 将txt存入DataFile,DataFile包含DataNode,每个DataNode是txt中的一行,每个DataNode包含若干token(std::vector<string ...