PAT1076. Forwards on Weibo (30)
使用DFS出现超时,改成bfs
DFS
#include <iostream>
#include <vector>
#include <set>
using namespace std;
int n;
int l;
int i,j;
int m,k;
int follower[1001][1001];
vector<int> user_list[1001];
set<int> user_s;
void queryU(int a,int depth){
if(depth==l){
return ;
}
for(int i=0;i<user_list[a].size();i++){
queryU(user_list[a][i],depth+1);
user_s.insert(user_list[a][i]);
} return ;
}
int main()
{
cin>>n>>l;
int a;
for(i=1;i<=n;i++){
cin>>m;
for(j=0;j<m;j++){
cin>>a;
user_list[a].push_back(i);
}
}
cin>>k;
int sum;
for(i=0;i<k;i++){
cin>>a;
user_s.clear();
user_s.insert(a);
queryU(a,0);
cout<<user_s.size()-1<<endl;
}
return 0;
}
bfs
最后一个测试用例一直MLE,后来用指针优化,就报超时了。
后来参考 https://www.zhihu.com/question/28264519,把队列删除语句省掉,set省掉,终于AC了!
#include <iostream>
#include <vector>
#include <set>
using namespace std;
int n;
int l;
int i,j;
int m,k;
vector<int> user_list[1010];
vector<int> que;
int v[1005];
int main()
{
cin>>n>>l;
int a;
for(i=1;i<=n;i++){
cin>>m;
for(j=0;j<m;j++){
cin>>a;
user_list[a].push_back(i);
}
}
cin>>k;
for(i=1;i<=k;i++){
cin>>a;
v[a]=i;
int depth=0;
que.clear();
que.push_back(a);
int levelSize=0;
int count=1;
for(int z=0;z<que.size();z++){
int num=que[z];
count--;
vector<int> *tmp=&user_list[num];
levelSize+=tmp->size();
for(int y=0;y<tmp->size();y++){
int temp_num=(*tmp)[y];
if(v[temp_num]!=i){
que.push_back(temp_num);
v[temp_num]=i;
}
}
if(count==0){
count=levelSize;
levelSize=0;
depth++;
}
if(depth==l){
break;
}
}
cout<<que.size()-1<<endl;
}
return 0;
}
PAT1076. 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 ...
- Forwards on Weibo (30)
BFS,题意比较难懂,是求离query L层的总共人数 #include <stdio.h> #include <string.h> #include <iostream ...
- PAT1076. Forwards on Weibo(标准bfs模板)
//标准的层次遍历模板 //居然因为一个j写成了i,debug半天.....解题前一定要把结构和逻辑想清楚,不能着急动手,理解清楚题意,把处理流程理清楚再动手,恍恍惚惚的写出来自己慢慢debug吧 # ...
随机推荐
- HDU 3695 / POJ 3987 Computer Virus on Planet Pandora
Computer Virus on Planet Pandora Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1353 ...
- 【BZOJ1511】[POI2006]OKR-Periods of Words next数组
[BZOJ1511][POI2006]OKR-Periods of Words Description 一个串是有限个小写字符的序列,特别的,一个空序列也可以是一个串. 一个串P是串A的前缀, 当且仅 ...
- GO语言中使用OpenCV
GO语言中使用OpenCV - OpenCV China :图像处理,计算机视觉库,Image Processing, Computer Vision http://wiki.opencv.org.c ...
- CentOS安装Apache-2.4.25+安全配置
注:以下所有操作均在CentOS 6.5 x86_64位系统下完成. #准备工作# 在安装Nginx之前,请确保已经使用yum安装了各基础组件,并且配置了www用户和用户组,具体见<CentOS ...
- php5.4 的 php-fpm 的重启
php 5.3.3以后 源码中已经内嵌了 php-fpm,不用象以前的php版本一样专门打补丁了,只需要在configure的时候添加编译参数即可. 关于php-fpm的编译参数有 –enable-f ...
- flask大全
django:重武器,内部包含了非常多组件:ORM.Form.ModelForm.缓存.Session,中间件,信号等... flask:短小精悍.内部没有太多组件,第三方组件非常丰富 1.初识fla ...
- 初识python (一)
初识Python(一) python2和python3的一些区别 Python2 和 Python3 区别汇总:http://www.cnblogs.com/bigtreei/p/7806288.ht ...
- CodeForces 215B Olympic Medal(数学啊)
题目链接:http://codeforces.com/problemset/problem/215/B Description The World Programming Olympics Medal ...
- 正则表达式 - JavaScript描述
正则表达式 - JavaScript描述 概述 正则表达式是被用来匹配字符串中的字符组合的模式.在JavaScript中,正则表达式也是对象. 创建正则表达式 var re = /abc/; // 使 ...
- Python(面向对象编程—1)
class tst: l=[] x=1 a=tst() b=tst() a.l.append('a') b.l.append('b') a.x='a' b.x='b' print(a.l,a.x) # ...