A 1076 Forwards on Weibo
Problem Description
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
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
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
题目大意:
给出n个人及其粉丝(后继节点)。一个人发微博,这条微博会被他的所有粉丝,以及他的粉丝的所有粉丝…以及…看到(以某个人为根节点,他的所有子树的节点都能看到他的微博)。然后k个查询,查询k个人每人在深度L内最多能看到多少个人的更新微博(每个人在层次深度L内最多有多少个不同的前驱节点)
解题思路:
用vis做标记,访问过的的不用访问了(去重),并且我们希望第一次访问某个节点时,该节点的深度尽可能浅,用bfs最佳,一开始没反应过来,dfs+set做了半天最后一个样例一直超时。
AC代码(bfs版117ms)
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;
struct node{
int data,lvl;
};
int vis[maxn],N,L,m,k,cnt;
vector<int> pre[maxn];
int bfs(int x,int level){
int num=0;
node now = {x,level};
queue<node> q;
q.push(now);
while(!q.empty()){
node next = q.front();
q.pop();
int p=next.data,high=next.lvl;
for(int i=0,len=pre[p].size();i<len;i++){
if(vis[pre[p][i]]!=cnt && high<L){
vis[pre[p][i]]=cnt;
num++;
q.push({pre[p][i],high+1});
}
}
}
return num;
}
int main(){
fill(vis,vis+maxn,0);
scanf("%d%d",&N,&L);
for(int i=1;i<=N;i++){
scanf("%d",&m);
while(m--){
scanf("%d",&k);
pre[k].emplace_back(i);
}
}
scanf("%d",&m);
for(cnt=1;cnt<=m;cnt++){
scanf("%d",&k);
vis[k]=cnt;
printf("%d\n",bfs(k,0));
}
return 0;
}
AC代码(dfs仿bfs剪枝版522ms)
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;
int vis[maxn],high[maxn],N,L,m,k,now,num,cnt;
vector<int> pre[maxn];
void dfs(int x,int level){
for(int i=0,len=pre[x].size();i<len;i++){
if(vis[pre[x][i]]!=cnt){
vis[pre[x][i]]=cnt;
num++;
if(level<L){
high[pre[x][i]]=min(high[pre[x][i]],level+1);
dfs(pre[x][i],level+1);
}
}else{
if(level<L && level+1<high[pre[x][i]]){
high[pre[x][i]]=level+1;
dfs(pre[x][i],level+1);
}
}
}
return ;
}
int main(){
fill(vis,vis+maxn,0);
scanf("%d%d",&N,&L);
for(int i=1;i<=N;i++){
scanf("%d",&m);
while(m--){
scanf("%d",&k);
pre[k].emplace_back(i);
}
}
scanf("%d",&m);
for(cnt=1;cnt<=m;cnt++){
scanf("%d",&k);
num=0;
vis[k]=cnt;
fill(high,high+N+1,L+1);
dfs(k,1);
printf("%d\n",num);
}
return 0;
}