Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define endl "\n"
- #define ll long long
- #define PI acos(-1.0)
- #define test cout<<"\n****\n"
- #define LCM(a,b) ((a/__gcd(a,b))*b)
- #define READ(f) freopen(f, "r", stdin)
- #define WRITE(f) freopen(f, "w", stdout)
- #define precise fixed(cout);cout<<setprecision(20)
- #define fast ios_base :: sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
- #define MAX 30
- vector<int>graph[MAX];
- int distan[MAX];
- int BFS(int from,int to){
- memset(distan,-1,sizeof(distan));
- int root = from;
- distan[root] = 0;
- queue<int>que;
- que.push(root);
- while (!que.empty()){
- int parent = que.front();
- que.pop();
- for(int i=0;i<graph[parent].size();i++){
- int child = graph[parent][i];
- if(distan[child]==-1){
- distan[child] = distan[parent] + 1;
- que.push(child);
- }
- }
- }
- return distan[to];
- }
- int main(){
- //WRITE("outhudai.txt");
- //fast;
- int n,casee = 1;
- while(cin>>n){
- for(int i=0;i<n;i++){
- int edge;
- cin>>edge;
- graph[1].push_back(edge);
- graph[edge].push_back(1);
- }
- for(int i=2;i<20;i++){
- cin>>n;
- for(int j=0;j<n;j++){
- int edge;
- cin>>edge;
- graph[i].push_back(edge);
- graph[edge].push_back(i);
- }
- }
- int query;
- cin>>query;
- cout<<"Test Set #"<<casee<<endl;
- for(int i=0;i<query;i++){
- int from,to;
- cin>>from>>to;
- printf("%2d to %2d: %d\n",from,to,BFS(from,to));
- }
- cout<<endl;
- for(auto& it: graph){
- it.clear();
- }
- casee++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement