1076 Forwards on Weibo (30 分)

Keywords

Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all his/her followers can view and forward his/her post, which can then be forwarded again by their followers. Now given a social network, you are supposed to calculate the maximum potential amount of forwards for any specific user, assuming that only L levels of indirect followers are counted.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers: N (≤1000), the number of users; and L (≤6), the number of levels of indirect followers that are counted. Hence it is assumed that all the users are numbered from 1 to N. Then N lines follow, each in the format:
M[i] user_list[i]
where M[i] (≤100) is the total number of people that user[i] follows; and user_list[i] is a list of the M[i] users that followed by user[i]. It is guaranteed that no one can follow oneself. All the numbers are separated by a space.
Then finally a positive K is given, followed by K UserID’s for query.

Output Specification:

For each UserID, you are supposed to print in one line the maximum potential amount of forwards this user can trigger, assuming that everyone who can view the initial post will forward it once, and that only L levels of indirect followers are counted.

Sample Input:

7 3
3 2 3 4
0
2 5 6
2 3 1
2 3 4
1 4
1 5
2 2 6

Sample Output:

4
5

Note1

  1. 题意: 找到 i 深度不超过给定值的节点在这里插入图片描述
  2. 思路:从每个节点开始做一遍dfs,增加遍历过程中深度 < threshold 节点的followers个数
  3. 错误: StackOverflow: dfs本体函数里面忘记写visited[i] = 1
    逻辑错误: 两点之间有两条路若dfs第一次走了长的,短的那条就不会再走(visited = 1)
  4. 强行续命, 再对图镜像dfs一次,取结果中大的一个,有效果,但是最后一个点过不了

Code1

#include<iostream>
#include<vector>
#include<fstream>
using namespace std;
const int MAX = 100010;
int root;
int threshold;
int  visited[MAX];
int ans1[MAX] = {0}, ans2[MAX] = {0};
int debug;
vector <int> a[MAX];
void Dfs1(int n, int depth){
    if(visited[n] == 0){
		visited[n] = 1;
        if(depth <= threshold && n != root ) ans1[n]++;
        for(int i = 0; i < a[n].size(); i++){
            Dfs1(a[n][i], depth + 1);
			debug = i;
        }
    }
}
void Dfs2(int n, int depth){
    if(visited[n] == 0){
		visited[n] = 1;
        if(depth <= threshold && n != root ) ans2[n]++;
        for(int i = a[n].size() - 1 ; i >=0;  i--){
            Dfs2(a[n][i], depth + 1);
			debug = i;
        }
    }
}
int main(){
#ifdef _DEBUG
	ifstream cin("data.txt");
#endif
    int num, input[MAX] ;

    cin >> num  >> threshold;
    for(int i = 1; i <= num; i++){  
        int temp, k;
        cin >> k;
        for(int j = 0; j < k; j++){
            cin >> temp;
            a[i].push_back(temp);
        }
    }
	int qnum;
	cin >> qnum;
	for(int i  = 0; i < qnum; i++){
		int temp;
		cin >> temp;
		input[i] = temp;
	}
    for(int i = 1; i <= num; i++){
        if(visited[i] == 0){
            root = i;
            Dfs2(i, 0);
        }
        for(int j = 0; j <= num; j++){
            visited[j] = 0;
        }
    }
	for(int i = 1; i <= num; i++){
        if(visited[i] == 0){
            root = i;
            Dfs1(i, 0);
        }
        for(int j = 0; j <= num; j++){
            visited[j] = 0;
        }
    }
	for(int i  = 0; i < qnum; i++){
		int out =  ans1[input[i]] >  ans2[input[i]] ?  ans1[input[i]] :  ans2[input[i]];
		cout << out << endl;
	}

#ifdef _DEBUG
	cin.close();
#endif
	return 0;
}

Note2

  1. 把指向反过来,利用是bfs
  2. 问题1 函数每次调用大数组初始化的时候想要偷懒,结果报错
  3. 问题2 bfs的逻辑的逻辑错误,在这里插入图片描述

](https://img-blog.csdnimg.cn/20190710211807526.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM5MzM5NTc1,size_16,color_FFFFFF,t_70)

Code2

#include<iostream>
#include<vector>
#include<fstream>
#include<queue>
using namespace std;
const int MAX = 100010;
int root;
int threshold;
int layer[MAX] = { 0 };
int visited[MAX] = { 0 };
int ans1[MAX] = {0}, ans2[MAX] = {0};
int debug, num;
vector <int> a[MAX];
int Bfs(int n){
	queue<int> q;
	int cnt = 0;
	 for(int j = 0; j <= num; j++){
            visited[j] = 0;
		    layer[j] =  0 ;
     }
	q.push(n);
	visited[n] = 1;
	while(q.size() > 0){
		int temp = q.front();
		q.pop();
		for(int k = 0; k < a[temp].size(); k++){
			if(visited[a[temp][k]] == 0 && layer[temp] < threshold  ){
					q.push(a[temp][k]);
					visited[a[temp][k]] = 1;
					layer[a[temp][k]] = layer[temp] + 1;
					cnt++;
				}
			 }
	}
	return cnt;
}

int main(){
#ifdef _DEBUG
	ifstream cin("data.txt");
#endif
    int  input[MAX] ;

    cin >> num  >> threshold;
    for(int i = 1; i <= num; i++){  
        int temp, k;
        cin >> k;
        for(int j = 0; j < k; j++){
            cin >> temp;
            a[temp].push_back(i);
        }
    }
	int qnum;
	cin >> qnum;
	for(int i  = 0; i < qnum; i++){
		int temp;
		cin >> temp;
		
		int out = Bfs(temp);
		cout << out << endl;
	}

#ifdef _DEBUG
	cin.close();
#endif
	return 0;
}

Note3

  1. 根据ac代码改写自己的代码,发现逻辑上bfs仍旧还有问题
  2. 对比

a. 自己写的代码,有bug

int Bfs(int n){   //
	queue<int> q;
	int cnt = 0;
	 for(int j = 0; j <= num; j++){
            visited[j] = 0;
		    layer[j] =  0 ;
     }
	q.push(n);
	while(q.size() > 0){
		int temp = q.front();
		q.pop();
		visited[temp] = 1;
		if(layer[temp] <= threshold  )
			for(int k = 0; k < a[temp].size(); k++){
				if(visited[a[temp][k]] == 0){
					q.push(a[temp][k]);
					layer[a[temp][k]] = layer[temp] + 1;
					if( layer[a[temp][k]] <= threshold)
						cnt++;
				}
			 }
	}
	return cnt;
}

b. ac代码

int Bfs(int n){
	queue<int> q;
	int cnt = 0;
	 for(int j = 0; j <= num; j++){
            visited[j] = 0;
		    layer[j] =  0 ;
     }
	q.push(n);
	visited[n] = 1;
	while(q.size() > 0){
		int temp = q.front();
		q.pop();
		for(int k = 0; k < a[temp].size(); k++){
			if(visited[a[temp][k]] == 0 && layer[temp] < threshold  ){
					q.push(a[temp][k]);
					visited[a[temp][k]] = 1;
					layer[a[temp][k]] = layer[temp] + 1;
					cnt++;
				}
			 }
	}
	return cnt;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值