当前位置:网站首页>1076 forwards on Weibo (30 points)
1076 forwards on Weibo (30 points)
2022-06-30 14:54:00 【Xue Dongjing】
30min
1076 Forwards on Weibo (30 branch )
The question
Let's give a digraph , Yes n Nodes , For nodes, there are m A asked , Ask this node k The number of nodes that can be reached within a step ( Walking along one side is a step , The distance from this node is no more than k Number of nodes ).
Yes n That's ok , Each line gives the number i Personal side ( It is someone else who comes to this person's side ).
Ideas
vector Drawing ,dfs Traverse the queried points , Find a distance of no more than k All nodes of .
Can open an array storage distance .
Be careful vis And order Each iteration must be initialized
Code
#include<stdio.h>
#include<vector>
#include<stack>
#include<string.h>
using namespace std;
vector<int>mp[10007];
int vis[10007],order[10007],k;
int dfs(int x)
{
stack<int>st;
int next,sum=0;
memset(vis,0,sizeof(vis));
memset(order,0,sizeof(order));// Be careful vis And order Each iteration must be initialized
st.push(x);
vis[x]=1;
while(!st.empty()){
next=st.top();
st.pop();
if(order[next]>=k){
continue;
}
for(int i=0;i<mp[next].size();i++){
if(vis[mp[next][i]]==0){
vis[mp[next][i]]=1;
order[mp[next][i]]=order[next]+1;
st.push(mp[next][i]);
sum++;
}
}
}
return sum;
}
int main()
{
int n,m,x;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d",&m);
for(int j=0;j<m;j++){
scanf("%d",&x);
mp[x].push_back(i);
}
}
scanf("%d",&m);
for(int i=0;i<m;i++){
scanf("%d",&x);
printf("%d\n",dfs(x));
}
return 0;
}
边栏推荐
- IO interview questions
- Double pointer palindrome string
- How many questions can you answer for the interview of Mechanical Engineer?
- ES6 notes
- Svn password forgetting solution
- How does hbuilder display in columns?
- Laravel upload error
- The kth largest element in the sorted array
- 【BUUCTF】 Have Fun
- 【BUUCTF】[GXYCTF2019]Ping Ping Ping1
猜你喜欢

The first dark spring cup dnuictf

ThinkPHP show method parameter controllable command execution

Ctfshow getting started with the web (ThinkPHP topic)
![[extensive reading of papers] multimodal attribute extraction](/img/ec/546c107ac0d31deded7ca94fdf0e2d.jpg)
[extensive reading of papers] multimodal attribute extraction

Determine the number of digits of an integer in MATLAB (one line of code)

CCF Z-scan (full mark code + problem solving ideas) 201412-2

CCF window (Full Score code + problem solving idea) March 2, 2014

2021-05-12
![【BUUCTF】[GXYCTF2019]Ping Ping Ping1](/img/dc/4d87dfb0c2fa9cd75b54e092fd3971.jpg)
【BUUCTF】[GXYCTF2019]Ping Ping Ping1

Thinkphp5 log file contains trick
随机推荐
Matlab to find prime pairs within 100
Component communication mode
Programming of left-hand trapezoidal thread
Pseudocode writing specification
Analysis on the problems of irregular step hole on horizontal machining center
JS delete the objects in the array and specify to delete the objects
Computer screenshot how to cut the mouse in
PS tip: the video frame to Layer command cannot be completed because dynamiclink is not available
1135: paired base chain
【BUUCTF】[GXYCTF2019]Ping Ping Ping1
2021-05-12
PHP recursive multi-level classification, infinite classification
ctfshow nodejs
CCF date calculation (Full Score code + skill summary) February 2, 2015
2021 geek challenge Web
Solve the problem that codeblocks20.03 on win11 cannot run for the first time
Thinkphp5 log file contains trick
[buuctf] [actf2020 freshman competition]include
Shangpinhui knowledge points of large e-commerce projects
Ctfshow getting started with the web (ThinkPHP topic)