-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuva_ 567Risk.cpp
89 lines (76 loc) · 1.71 KB
/
uva_ 567Risk.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include<bits/stdc++.h>
#define bug cout<<"bug";
#define pb(n) push_back(n)
#define lli long long int
#define fr(i,s,l) for(i=s;i<l;i++)
#define sf(n) scanf("%d",&n);
using namespace std;
int bfs(int f,int s,const vector< int > *adlist)
{
int level[30]= {};
bool vis[30]= {};
queue<int> show;
show.push(f);
level[f]=0;
vis[f]=1;
while(!show.empty())
{
int top=show.front();
show.pop();
for(int i=0; i<adlist[top].size(); i++)
{
int tmp=adlist[top][i];
if(!vis[tmp])
{
show.push(tmp);
vis[tmp]=1;
level[tmp]=level[top]+1;
}
}
}
return level[s];
}
int main()
{
//freopen ("myfile.txt","w",stdout);
int kase=1,how;
while(scanf("%d",&how)==1)
{
vector<int> adlist[30];
int query,f,s,num;
int i,j;
fr(j,0,how)
{
{
cin>>num;
adlist[1].pb(num);
adlist[num].pb(1);
}
}
fr(i,1,19)
{
cin>>how;
fr(j,0,how)
{
{
cin>>num;
adlist[i+1].pb(num);
adlist[num].pb(i+1);
}
}
}
cin>>query;
printf("Test Set #%d\n",kase++);
fr(j,0,query)
{
cin>>f>>s;
printf("%2d to %2d: %d\n",f,s,bfs(f,s,adlist));
}
cout<<endl;
for(int i=0; i<21; i++)
{
adlist[i].clear();
}
}
return 0;
}