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

  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. Forwards on Weibo (30)

    BFS,题意比较难懂,是求离query L层的总共人数 #include <stdio.h> #include <string.h> #include <iostream ...

  9. PAT1076. Forwards on Weibo(标准bfs模板)

    //标准的层次遍历模板 //居然因为一个j写成了i,debug半天.....解题前一定要把结构和逻辑想清楚,不能着急动手,理解清楚题意,把处理流程理清楚再动手,恍恍惚惚的写出来自己慢慢debug吧 # ...

随机推荐

  1. 【BZOJ4456】[Zjoi2016]旅行者 分治+最短路

    [BZOJ4456][Zjoi2016]旅行者 Description 小Y来到了一个新的城市旅行.她发现了这个城市的布局是网格状的,也就是有n条从东到西的道路和m条从南到北的道路,这些道路两两相交形 ...

  2. oninput事件(解决onkeyup无法监听到复制黏贴)

    change事件需要两个条件触发: a)当前对象属性改变,并且是由键盘或鼠标事件激发的(脚本触发无效) b)当前对象失去焦点(onblur)  keypress  能监听键盘事件,但鼠标复制黏贴操作就 ...

  3. CodeForces 17E Palisection(回文树)

    E. Palisection time limit per test 2 seconds memory limit per test 128 megabytes input standard inpu ...

  4. Kubernetes资源创建yml语法

    前言 在是用kubernetes中,我们对资源的创建大部分都是通过 1 kubelet create -f RESOURCE.yaml 刚开看的时候不免有一些迷茫,看不懂语法,不知道怎么写:今天本文就 ...

  5. android问题总结

    1.当打开eclipse时出现如下窗口(内容如下) Error when loading the SDK: Error: Error parsing \Android\adt-bundle-windo ...

  6. JS eval()函数

    js  eval()函数   这个函数可以把一个字符串当作一个JavaScript表达式一样去执行它.   举个小例子:    //执行表达式  var the_unevaled_answer = & ...

  7. Neutron相关资料链接

    1.OpenStack Neturon 官方文档: https://docs.openstack.org/mitaka/networking-guide/ 2.Neturon理解系列文章: http: ...

  8. Django - 权限(1)

    一.权限表结构设计 1.认识权限 生活中处处有权限,比如,腾讯视频开会员才有观看某个最新电影的权限,你有房间钥匙就有了进入这个房间的权限,等等.同样,程序开发过程中也有权限,我们今天说的权限指的是we ...

  9. oracle入门(1)——安装oracle 11g x64 for windows

    [本文简介] 最近因为一个项目的需要,从零学习起了oracle,现在把学到的东西记录分享一下. 首先是安装篇,在win8 装10G 一直失败,网上各种方法都试过了,最后不得不放弃,选择了11G. 11 ...

  10. const,var,let区别(转载)

    1.const定义的变量不可以修改,而且必须初始化. const b = 2;//正确 // const b;//错误,必须初始化 console.log('函数外const定义b:' + b);// ...