PAT 1076 Forwards on Weibo
#include <cstdio>
#include <cstdlib> #include <vector>
#include <queue>
#include <unordered_set> using namespace std; class User {
public:
vector<int> fellowers;
bool visited;
User(): visited(false){}
}; int query(int idx, vector<User>& users, int L) {
int level = L;
unordered_set<int> walked;
queue<int> level_queue; walked.insert(idx);
level_queue.push(idx); int last_size = level_queue.size(); while (!level_queue.empty() && level > ) {
while(last_size-- > ) {
int uid = level_queue.front(); User& user = users[uid];
level_queue.pop();
int len = user.fellowers.size();
for (int i=; i<len; i++) {
int u = user.fellowers[i];
// already visited
if (walked.count(u)) continue;
// not visited
//printf("visit %d\n", u);
walked.insert(u);
level_queue.push(u);
}
} last_size = level_queue.size();
level--;
} return walked.size() - ;
} int main() {
int N = , L = ;
scanf("%d%d", &N, &L); vector<User> users(N + ); for (int i=; i<=N; i++) {
int n = ;
scanf("%d", &n);
for (int j=; j<n; j++) {
int uidx = ;
scanf("%d", &uidx);
users[uidx].fellowers.push_back(i);
}
} int K = ;
scanf("%d", &K);
for (int i=; i<K; i++) {
int uidx = ;
scanf("%d", &uidx);
int cnt = query(uidx, users, L);
printf("%d\n", cnt);
}
return ;
}
最后一个case用时有1800ms,不过还是在范围之内,应该有可以再优化的地方
PAT 1076 Forwards on Weibo的更多相关文章
- PAT 1076 Forwards on Weibo[BFS][一般]
1076 Forwards on Weibo (30)(30 分) Weibo is known as the Chinese version of Twitter. One user on Weib ...
- 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 ...
- PAT甲级1076. Forwards on Weibo
PAT甲级1076. Forwards on Weibo 题意: 微博被称为中文版的Twitter.微博上的一位用户可能会有很多关注者,也可能会跟随许多其他用户.因此,社会网络与追随者的关系形成.当用 ...
- 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 分)
1076 Forwards on Weibo (30 分) Weibo is known as the Chinese version of Twitter. One user on Weibo ma ...
- 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 ...
随机推荐
- [jvm]基于jvm的线程实现
一.线程的实现 学过操作系统的肯定都知道: 进程:是并发执行的程序在执行过程中分配和管理资源的基本单位,是一个动态概念,竞争计算机系统资源的基本单位. 线程:是进程的一个执行单元,是进程内可调度实体. ...
- 浅谈C#中的委托、事件与异步
从刚接触c#编程到现在,差不多快有一年的时间了.在学习过程中,有很多地方始终似是而非,直到最近才弄明白. 本文将先介绍用法,后评断功能. 一.委托 基本用法: 1.声明一个委托类型.委托就像是‘类'一 ...
- 洛谷 P2286 [HNOI2004]宠物收养场
题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...
- @ApiParam @PathVariable @RequestParam三者区别
转载:https://www.cnblogs.com/xu-lei/p/7803062.html @ApiParam @PathVariable @RequestParam三者区别 1.@ApiPar ...
- 【转】IntelliJ Idea取消Could not autowire. No beans of 'xxxx' type found的错误提示
1.问题描述 在Idea的spring工程里,经常会遇到Could not autowire. No beans of 'xxxx' type found的错误提示.但程序的编译和运行都是没有问题的, ...
- 【转】intellij idea使用指南—— 导入Eclipse的Web项目
通常一个团队中可能有人用eclipse,有人用intelliJ,那么经常会出现需要导入别人用eclipse建好的web项目.而IntelliJ提供了多种项目类型的导入方式,其中就有eclipse. 在 ...
- Hero
Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Description When pl ...
- 教你搭建SpringSecurity3框架(附源码)
源码下载地址:http://pan.baidu.com/s/1qWsgIg0 一.web.xml <?xml version="1.0" encoding="UTF ...
- 使用Git向GitHub上上传代码
参考:http://www.cnblogs.com/yxhblogs/p/6130995.html 如果遇到[git无法pull仓库refusing to merge unrelated histor ...
- HihoCoder - 1044 状压DP 初步
本题主要难在状态的转移 定义\(dp[i][j]:\)前\(i\)个中\(j\)集合范围内的最优解 \(j\)定义为\(p_1,p_2,...,p_{m-1}\),若第\(i-j+1\)个选定,则\( ...