1、
//对bool数组初始化false也用0 memset(v,0,N+1);//不能用sizeof(v),当N+1=8时,sizeof(v)=4
两个case没过:
#include#include #include using namespace std;int m[1001][1001];int N,L;bool * v;int dfs(int s){ queue q; q.push(s); int depth = 0; int count = 0; while(!q.empty()){ int cur = q.front(); q.pop(); v[cur] = true; int i; bool flag=true; for(i=1; i<=N; i++){ if(!v[i] && m[i][cur] == 1){ q.push(i); //printf("%d - ",i); count++; //v[i] = true; if(flag){ depth++; flag = false;} } } if(depth == L) break; } return count;}int main(){ //freopen("in.txt","r",stdin); scanf("%d %d",&N,&L); v = new bool[N+1]; //对bool数组初始化false也用0 memset(v,0,N+1);//不能用sizeof(v),当N+1=8时,sizeof(v)=4 memset(m,0,sizeof(m)); int i,j; //for(i=1;i<=N;i++){ // if(v[i]) {printf("%d\n",i);} // else{printf("0\n");} //} for(i=1; i<=N; i++){ //i为当前节点 int k; scanf("%d",&k); while(0 < k--){ int temp; scanf("%d",&temp); m[i][temp] = 1;//i被temp转发 } } int scount,s; scanf("%d",&scount); while(0 < scount--){ memset(v,0,N+1); scanf("%d",&s); int count = dfs(s); printf("%d\n",count); } return 0;}