Friday, 18 September 2015

UVA - 567 Risk (UVALive - 5498, ZOJ - 1221, POJ - 1603, Regionals 1997 >> North America - South Central USA)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

vector<int>adj[22];
int dist[22];

void bfs(int source, int dest)
{
    SET(dist);
    queue<int>Q;
    Q.push(source);
    dist[source]=0;
    while(!Q.empty())
    {
        int u = Q.front();
        for(int i=0;i<adj[u].size();i++)
        {
            int v = adj[u][i];
            if(dist[v]==-1)
            {
                dist[v]=dist[u]+1;
                Q.push(v);
            }
        }
        Q.pop();
    }
return;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n, v, a, b, x;
    string s;
    char ch;
    while(cin>>x)
    {
        for(int i=0;i<x;i++)
        {
            cin>>v;
            adj[1].push_back(v);
            adj[v].push_back(1);
        }
        for(int i=2;i<20;i++)
        {
            cin>>x;
            for(int j=0;j<x;j++)
            {
                cin>>v;
                adj[i].push_back(v);
                adj[v].push_back(i);
            }
        }

        cout<<"Test Set #"<<kk++<<"\n";

        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>a>>b;
            bfs(a, b); //(source, destination)
            cout<<setw(2)<<a<<" to "<<setw(2)<<b<<": "<<dist[b]<<"\n";
        }
        cout<<"\n";
        for(int i=1;i<=20;i++) adj[i].clear();
    }
return 0;
}

No comments:

Post a Comment