Tuesday 2 December 2014

UVa 280 - Vertex Solution (UVALive 5588, Regionals 1993 >> Europe - Southwestern)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank / 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 105

vector<int>adj[MX];
bool vis[MX];
int src, chk;

void dfs(int u)
{
    if(u!=src || chk!=-1)
        vis[u]=true;
    chk=1;
    int ul=adj[u].size();
    for(int i=0;i<ul;i++)
    {
        int v=adj[u][i];
        if(!vis[v])
            dfs(v);
    }
return;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n, tc, kk=1, v, u, q;
    string s;
    while(cin>>n && n)
    {

        while(1)
        {
            cin>>u;
            if(!u) break;
            while(cin>>v && v)
                adj[u].push_back(v);
        }
        cin>>q;
        while(q--)
        {
            cin>>src;
            chk=-1;
            CLR(vis);
            dfs(src);
            vector<int>cnt;
            for(int i=1;i<=n;i++)
                if(!vis[i])
                    cnt.push_back(i);
            int cntl=cnt.size();
            cout<<cntl;
            for(int i=0;i<cntl;i++)
                cout<<" "<<cnt[i];
            cout<<"\n";
        }
        for(int i=1;i<=n;i++) adj[i].clear();
    }
return 0;
}

No comments:

Post a Comment