使用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. 网络虚拟化之FlowVisor:网络虚拟层(中)

    上一篇博客网络虚拟化之FlowVisor:网络虚拟层(上)主要对比了计算机虚拟化和网络虚拟化,引出了FLowVisor网络虚拟层,介绍了其一些特性,这篇博文深入讲解FLowVisor的技术. 一. 概 ...

  2. [LintCode] 第k大元素

    基于快速排序: class Solution { public: /* * param k : description of k * param nums : description of array ...

  3. ES6入门概览二--数组

    一 数组 1. Array.from() 将两类对象转为真的数组 : 类似数组的对象(伪数组,如arguments.document.getElementsByTagNames等)和可遍历对象(包括E ...

  4. Java获取任意时间、时间字符串

    /* * 获取时间字符串*/public String getCurrentTime() { SimpleDateFormat sdf = new SimpleDateFormat("yyy ...

  5. Intellij IDEA Ultimate Edition 14.1 破解

    key:IDEA value:61156-YRN2M-5MNCN-NZ8D2-7B4EW-U12L4 (2) key:huangwei value:97493-G3A41-0SO24-W57LI-Y2 ...

  6. 踩坑之jinja2注释问题(Flask中)

    报错信息  jinja2.exceptions.TemplateSyntaxError  jinja2.exceptions.TemplateSyntaxError: Expected an expr ...

  7. 我的Android进阶之旅------>解决Android Studio全局搜索搜不到结果的问题

    1.问题描述 今天使用Android Studio时,想通过使用快捷键Ctrl+Shift+F来进行全局搜索指定字符串,如下图所示:想搜索字符串"码农偷懒了", 打开string. ...

  8. 服务系统 server端

    from django.shortcuts import render,HttpResponse import json # Create your views here. from reposito ...

  9. 001-project基本使用

    一.概述 Project工具一般用来管理一个项目,制定项目的执行计划.这个项目可以是临时性的工作,可以是IT项目.工程类项目,也可是结婚这一事情,项目的特点是产生唯一性的成果或最终结果. 项目的三要素 ...

  10. CoreThink开发(十一)首页控制器判断移动设备还是PC并做相应处理

    在home模块Index控制器添加判断代码 application\Home\Controller\IndexController.class.php <?php // +----------- ...