PTA甲級 1076 Forwards on Weibo (30分)

Camilleferros發表於2020-11-05

強烈推薦,刷P他的朋友都認識一下柳神–PTA解法大佬

本文由參考於柳神部落格寫成

柳神的CSDN部落格,這個可以搜尋文章

柳神的個人部落格,這個沒有廣告,但是不能搜尋

還有就是非常非常有用的 演算法筆記 全名是

演算法筆記  上級訓練實戰指南		//這本都是P他的題解
演算法筆記

PS 今天也要加油鴨

在這裡插入圖片描述

題目原文

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

生詞如下:

PS:關鍵詞沒有看懂就GG,大意就是給你一個圖,和連線關係.

然後再給出查詢的結點.問你?(我沒有看懂的地方)

關鍵句子:

maximum potential amount of forwards for any specific user, assuming that only L levels of indirect followers are counted.

假設僅計算L級間接關注者,則任何特定使用者的最大潛在轉發量

最大的潛在轉發量只計算L級的關注者.

indirect 間接

trigger 觸發

題目大意:

就是問你一個有向圖中,已某個點當中心,它下面L層一共有多少結點.

思路如下:

BFS或者DFS都可以簡單的解決問題

DFS比較麻煩一點.

要注意這種情況

5 3 
0 
1 1 
1 2 
2 1 3 
1 4
1 1 

正確答案要輸出

4

因為我們在遍歷了 1->2->3->4之後,5 就會被彈出.

但是在1-4-5的情況下是可以的.

我們需要對DFS的程式碼做修改

修改後的DFS程式碼如下:

#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
const int Max = 1024;
int n, l, fan, id, query = 0, t,SumRoot=0, level[Max];
bool vis[Max];
vector<int> G[Max];
void dfsTrave(int v, int depth) {
	vis[v] = true;
	level[v] = depth;
	if (depth < l) {		//剪枝的一個方法level[G[v][i]] > depth + 1 就是當出現
		for (int i = 0; i < G[v].size(); ++i) {
			if (!vis[G[v][i]] || level[G[v][i]] > depth + 1)	dfsTrave(G[v][i], depth + 1);
		}
	}
	return;
}
int main(void) {
	scanf("%d%d", &n, &l);
	for (int i = 1; i <= n; ++i) {
		scanf("%d", &fan);
		for (int j = 0; j < fan; ++j) {
			scanf("%d", &id);
			G[id].push_back(i);
		}
	}
	scanf("%d", &query);
	for (int i = 0; i < query; ++i) {
		scanf("%d", &t);
		SumRoot = 0;
		memset(vis, false, sizeof(vis));
		memset(level, -1, sizeof(level));
		dfsTrave(t, 0);
		for(int i=1;i<=n;++i)	 SumRoot+= level[i] > 0 ? 1 : 0;
		printf("%d\n", SumRoot);
	}
	return 0;
}

BFS程式碼如下:

#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
const int Max = 1024;
int n, l, fan, id, query = 0, t, SumRoot = 0, idlevel[Max];
bool vis[Max];
vector<int> G[Max];
void bfs(int v) {
	queue<int> q;
	vis[v] = true;
	q.push(v);
	idlevel[v] = 0;
	while (q.size()) {
		int u = q.front();
		q.pop();
		int next = idlevel[u] + 1;
		if (next > l)return;
		for (int i = 0; i < G[u].size(); ++i) {
			if (!vis[G[u][i]]){
				SumRoot++;
				vis[G[u][i]] = true;
				idlevel[G[u][i]] = next;
				q.push(G[u][i]);
			}
		}
	}
}
int main(void) {
	scanf("%d%d", &n, &l);
	for (int i = 1; i <= n; ++i) {
		scanf("%d", &fan);
		for (int j = 0; j < fan; ++j) {
			scanf("%d", &id);
			G[id].push_back(i);
		}
	}
	scanf("%d", &query);
	for (int i = 0; i < query; ++i) {
		scanf("%d", &t);
		SumRoot = 0;
		memset(vis, false, sizeof(vis));
		bfs(t);
		printf("%d\n", SumRoot);
	}
	return 0;
}

如果這篇文章對你有張幫助的話,可以用你高貴的小手給我點一個免費的贊嗎

相信我,你也能變成光.

在這裡插入圖片描述

如果你有任何建議,或者是發現了我的錯誤,歡迎評論留言指出.

相關文章