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. 8天掌握EF的Code First开发系列之3 管理数据库创建,填充种子数据以及LINQ操作详解

    本文出自8天掌握EF的Code First开发系列,经过自己的实践整理出来. 本篇目录 管理数据库创建 管理数据库连接 管理数据库初始化 填充种子数据 LINQ to Entities详解 什么是LI ...

  2. python2.6.6安装Image模块

    python2.6.6安装Image模块1.下载Image模块源码地址:http://www.pythonware.com/products/pil/index.htm2.加压文件#tar zxvf ...

  3. socket-自我总结(2)

    这里总结下一个服务端与多个客户端之间的通信. 先看demo: #/usr/bin/env python #_*_coding:utf-8_*_ __author__ = 'ganzl' import ...

  4. 通过Dockerfile建立.NET Core mvc Image

    生成.NET core mvc code docker run -itd microsoft/dotnet:latestdocker psdocker attach containeridmkdir ...

  5. 国内npm镜像源推荐及使用

    NPM(Node Package Manager),是NodeJs的模块依赖管理工具.由于Npm源在国外,使用起来不方便, 故需要国内可靠的npm源可以使用,现整理如下: 一.国内镜像 1.淘宝NPM ...

  6. java 截取url的参数

    /** * 去掉url中的路径,留下请求参数部分 * @param strURL url地址 * @return url请求参数部分 * @author lzf */ private static S ...

  7. DB2 syntax error

    Error infomation:  An unexpected token "JOIN" was found following "". Expected t ...

  8. EF外键关联

    客户里面存在客服外键 基类模型 public class ModelBase { public ModelBase() { CreateTime = DateTime.Now; } [Key] pub ...

  9. Content is not allowed in prolog ---UTF-8 无bom

  10. Java中Timer的用法

    现在项目中用到需要定时去检查文件是否更新的功能.timer正好用于此处. 用法很简单,new一个timer,然后写一个timertask的子类即可. 代码如下: package comz.autoup ...