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 ...
随机推荐
- 移动端touch事件影响click事件以及在touchmove添加preventDefault导致页面无法滚动的解决方法
这两天自己在写一个手机网页,用到了触屏滑动的特效,就是往右滑动的时候左侧隐藏的菜单从左边划出来. 做完之后在手机原生浏览器中运行正常,但在QQ和微信中打开,发现touchmove只会触发一次,而且to ...
- yii + elasticsearch 手册
https://zhuowenji1.gitbooks.io/elasticsearch/content/an_zhuang_yii2.html
- Git 操作本地分支与远程分支
1 查看本地分支 git branch 2 查看远程分支 git branch -a 3 新建远程分支 git checkout -b developr git push origin develop ...
- php下载网络图片到服务器
/** * 下载二维码到服务器 * @param string $url 图片路径 * @param string $filestring 要保存的文件名 */ private function ...
- [小哥Allegro72讲速成视频]
http://v.qq.com/vplus/df932a993679cf80a0b6c87bb849e22c 第01讲 Allegro常用组件介绍 视频链接:http://v.qq.com/boke/ ...
- CSS z-index 属性
定义和用法 z-index 属性设置元素的堆叠顺序.拥有更高堆叠顺序的元素总是会处于堆叠顺序较低的元素的前面. 注释:元素可拥有负的 z-index 属性值. 注释:Z-index 仅能在定位元素上奏 ...
- C# 开源压缩组件比较
SevenZipSharp check()为检查压缩包,有BUG,360创建的zip压缩包有无密码,密码错对都返回true DotNetZip 提供的函数比较人性化,缺点是只支持zip SharpCo ...
- OpenLDAP双主
1:主A服务器 然后重新生成配置文件数据 主B服务器 注意:两个主服务器的rid必须得一样 在做主从的时候,必须得安装必要的软件包,comp ...
- 字符串截取函数-c语言
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 char* substring(char* ch,int pos,int length ...
- PTA Iterative Mergesort
How would you implement mergesort without using recursion? The idea of iterative mergesort is to sta ...