浙大PAT甲级 1076
2016-08-28 15:49
274 查看
广度优先搜索。用queue来实现,且用mark来表示userid是否出现。
AC代码:
#include<iostream>
#include<map>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<list>
#include<set>
#include<stack>
#include<cmath>
#include<vector>
#define inf 999999999
using namespace std;
vector<int> v[1001];
int n,m;
int mark[1001];
struct node
{
int lay;
int data;
};
queue<node> q;
void bfs(int x)
{
node w;
w.lay=0;
w.data=x;
q.push(w);
while(!q.empty())
{
node tmp=q.front();
q.pop();
for(int i=0;i<v[tmp.data].size();i++)
{
node gg;
gg.data=v[tmp.data][i];
if(mark[gg.data]==1)
continue;
gg.lay=tmp.lay+1;
if(gg.lay!=m)
{
q.push(gg);
}
mark[gg.data]=1;
}
}
}
int main()
{
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
{
int d;
scanf("%d",&d);
for(int j=1;j<=d;j++)
{
int t;
scanf("%d",&t);
v[t].push_back(i);
}
}
int d;
scanf("%d",&d);
for(int i=0;i<d;i++)
{
memset(mark,0,sizeof(mark));
int t;
scanf("%d",&t);
bfs(t);
int ans=0;
for(int j=1;j<=n;j++)
{
if(j==t) continue;
if(mark[j]==1)
ans++;
}
printf("%d\n",ans);
}
}
AC代码:
#include<iostream>
#include<map>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<list>
#include<set>
#include<stack>
#include<cmath>
#include<vector>
#define inf 999999999
using namespace std;
vector<int> v[1001];
int n,m;
int mark[1001];
struct node
{
int lay;
int data;
};
queue<node> q;
void bfs(int x)
{
node w;
w.lay=0;
w.data=x;
q.push(w);
while(!q.empty())
{
node tmp=q.front();
q.pop();
for(int i=0;i<v[tmp.data].size();i++)
{
node gg;
gg.data=v[tmp.data][i];
if(mark[gg.data]==1)
continue;
gg.lay=tmp.lay+1;
if(gg.lay!=m)
{
q.push(gg);
}
mark[gg.data]=1;
}
}
}
int main()
{
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
{
int d;
scanf("%d",&d);
for(int j=1;j<=d;j++)
{
int t;
scanf("%d",&t);
v[t].push_back(i);
}
}
int d;
scanf("%d",&d);
for(int i=0;i<d;i++)
{
memset(mark,0,sizeof(mark));
int t;
scanf("%d",&t);
bfs(t);
int ans=0;
for(int j=1;j<=n;j++)
{
if(j==t) continue;
if(mark[j]==1)
ans++;
}
printf("%d\n",ans);
}
}
相关文章推荐
- 浙大PAT甲级-1013
- 浙大PAT甲级-1048
- 浙大PAT甲级 1041
- 浙大PAT甲级 1054
- **浙大PAT甲级 1057 树状数组+二分法
- *浙大PAT甲级 1072 最短路径dij
- 浙大PAT甲级 1092
- **浙大PAT甲级 1103 dfs+快速幂
- *浙大PAT甲级 1110 判断是否为完全二叉树
- 浙大PAT甲级-1032
- 浙大PAT甲级 1042
- *浙大PAT甲级 1049
- 浙大PAT甲级 1055
- 浙大PAT甲级 1084
- *浙大PAT甲级 1107 并查集
- 浙大PAT甲级-1006
- 浙大pat | 牛客网甲级1063 Count PAT's (25)字符串
- 浙大PAT甲级 1084
- 浙大PAT甲级 1104
- 浙大PAT甲级-1011