Uva 280 Solution

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

int a[N][N], v[N], node;
vector<int>g;

void dfsvisit(int x)
{
    for(int i=1; i<=node; i++)
    {
        if(a[x][i] == 1 && v[i] == 0)
        {
            v[i] = 1;
            dfsvisit(i);
        }
    }
}

void clear1()
{
    for(int i=1; i<=node; i++)
    {
        v[i] = 0;
        for(int j=1; j<=node; j++)
        {
            a[i][j] = 0;
        }
    }
}

void clear2()
{
    for(int i=1; i<=node; i++)
        v[i] = 0;
}

void count1()
{
    for(int i=1; i<=node; i++)
    {
        if(v[i] == 0)
            g.push_back(i);
    }
}

int main()
{
    while(cin >> node && node != 0)
    {
        clear1();
        int x;
        while(cin >> x && x != 0)
        {
            int y;
            while(cin >> y && y != 0)
                a[x][y] = 1;
        }
        int n, st;
        cin >> n;
        while(n--)
        {
            cin >> st;
            clear2();

            dfsvisit(st);

            count1();

            cout << g.size();
            if(g.size())
cout << " ";
            for(int i=0; i<g.size(); i++)
            {
                cout << g[i];
                if(i < (g.size()-1))
                    cout << " ";
            }
            g.clear();

            cout << "\n";
        }
    }

    return 0;
}

Comments