使用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. 【BZOJ4675】点对游戏 树分治+期望

    [BZOJ4675]点对游戏 Description 桑尼.露娜和斯塔在玩点对游戏,这个游戏在一棵节点数为n的树上进行. 桑尼.露娜和斯塔三人轮流从树上所有未被占有的节点中选取一点,归为己有,轮流顺序 ...

  2. iOS 去掉navgationbar 底部线条

    [[UINavigationBar appearance] setBarStyle:UIBarStyleBlackTranslucent]; [[UINavigationBar appearance] ...

  3. ipad4丢失查找攻略

    如果不幸你的ipad丢失了,你可以通过find my iphone的软件来.它会帮你定位你ipad的位置.还有一种方式是登录你的icloud里面有个功能是查找我的iphone. 你还可以点击下面这个链 ...

  4. Linux挂载:VMware tools for Linux安装

    安装VMTools [root@localhost ~]# mkdir /mnt/cdrom [root@localhost ~]# mount /dev/cdrom /mnt/cdrom/ [roo ...

  5. Ad Exchange

    品友互动-基于大数据技术的人工智能决策平台 http://www.ipinyou.com.cn/about?flag=milestones

  6. ps如果换背景

    一.魔棒工具 魔棒工具主要是使用户可以选择颜色一致的区域(例如,一朵红花)或者背景比较单调的图片,而不必跟踪其轮廓.魔棒工具选择的颜色范围可以由工具选项栏中的误差范围 参数值来控制. 二.钢笔工具 路 ...

  7. 两款高性能并行计算引擎Storm和Spark比較

    对Spark.Storm以及Spark Streaming引擎的简明扼要.深入浅出的比較,原文发表于踏得网. Spark基于这种理念.当数据庞大时,把计算过程传递给数据要比把数据传递给计算过程要更富效 ...

  8. 011-JDK可视化监控工具-Jstat

    一.概述 Jstat 是JDK自带的一个轻量级小工具.全称“Java Virtual Machine statistics monitoring tool”,它位于java的bin目录下,主要利用JV ...

  9. sqlite时间段查询

    同样的SQL语句,查不出数据来 select * from table1 where t1>='2017-6-1' and t1<='2017-6-5' 改成 select * from ...

  10. xpath(待补充)

    from lxml import etree html=""" <div> <ul> <li>1</li> <li ...