-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathgrafxy.cpp
52 lines (51 loc) · 1.01 KB
/
grafxy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# include <cstdio>
# include <queue>
using namespace std;
vector <int> v[100005];
queue <int> q;
int i,j,n,m,X,Y,x,y;
int var[100005],ap[100005],a[100005];
void BFS ()
{
int i,x;
while (! q.empty())
{
x=q.front(); q.pop();
for (i=0; i<v[x].size(); ++i)
{
if (ap[v[x][i]]>ap[x]+1)
{
ap[v[x][i]]=ap[x]+1;
q.push(v[x][i]);
}
}
}
}
int main ()
{
freopen ("grafxy.in", "r", stdin);
freopen ("grafxy.out", "w", stdout);
scanf ("%d%d", &n, &m);
for (i=1; i<=n; ++i)
ap[i]=999999;
for (i=1; i<=m; ++i)
{
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
}
scanf ("%d", &X);
for (i=1; i<=X; ++i)
scanf ("%d", &a[i]);
scanf ("%d", &Y);
for (i=1; i<=Y; ++i)
{
scanf ("%d", &var[i]);
ap[var[i]]=0;
q.push(var[i]);
}
BFS ();
for (i=1; i<=X; ++i)
printf ("%d\n", ap[a[i]]);
return 0;
}