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)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 1076. Forwards on Weibo (30)

    时间限制 3000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Weibo is known as the Chinese v ...

  4. 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 ...

  5. 1076. Forwards on Weibo (30) - 记录层的BFS改进

    题目如下: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, a ...

  6. 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 ...

  7. 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 ...

  8. PAT (Advanced Level) 1076. Forwards on Weibo (30)

    最短路. 每次询问的点当做起点,然后算一下点到其余点的最短路.然后统计一下最短路小于等于L的点有几个. #include<cstdio> #include<cstring> # ...

  9. PAT甲题题解-1076. Forwards on Weibo (30)-BFS

    题目大意:给出每个用户id关注的人,和转发最多的层数L,求一个id发了条微博最多会有多少个人转发,每个人只考虑转发一次.用BFS,同时每个节点要记录下所在的层数,由于只能转发一次,所以每个节点要用vi ...

随机推荐

  1. Windows转到linux中,文件乱码,文件编码转换 & 解决sqlplus连接oracle乱码

    转载:http://www.cnblogs.com/wanyao/p/3399269.html 最近,学习又重新开始Linux学习,所以一直在Centos中,昨天一朋友把他在Windows下写的C程序 ...

  2. jquery实现静态html文件的include嵌入效果

    //引入jQuery的js 建立footer.html,内容为要嵌入的内容. 在需要嵌入的页面中加入: $.get("footer.html",function(data){ $( ...

  3. SpringMVC学习笔记(一)

    一.MVC的流程图 分析流程图 1. 首先用户发送请求---->前端控制器,前端控制器根据请求信息(如URL)来决定选择哪一个页面控制器进行处理并把请求委托给它,即以前的控制器的控制逻辑部分:图 ...

  4. log4net的各种Appender配置示例

    Apache log4net™ Config Examples Overview This document presents example configurations for the built ...

  5. CLASSIC VS INTERGRATED IN IIS 7.0

    Classic mode (the only mode in IIS6 and below) is a mode where IIS only works with ISAPI extensions ...

  6. 小白如何进入IOS,答案就在这里

    ***对于进来看过我博客的博友们,请看一下最后面的几道题,觉得可以的可以自己私下做一下,有不懂的我们可以相互交流*** 现在我来说一下我们IOS需要的基础,现在用的比较多的就是swift语言. 首先, ...

  7. 项目中可能用到的demo

    1. 轮播图   https://github.com/codingZero/XRCarouselView 2. 图表   https://github.com/Zirkfied/ZFChart

  8. XML数据 JSON数据 LitJSON 数据 的编写和解析 小结

    用XML生成如下数据<?xml version="1.0"encoding="UTF-8"?><Transform name="My ...

  9. exception 'yii\base\ErrorException' with message 'Class 'MongoClient' not found'

    问题描述: 本来项目运行的好好的,搬了一次办公室(电脑主机一起搬的),第二天的时候就登录不了了. php版本和扩展没有改变,且没有修改任何配置,我尝试重启php5-fpm 服务,又重启nginx服务, ...

  10. (引用)Python 生成随机数小结

    转载:http://blog.csdn.net/shuaijiasanshao/article/details/51339438