Thursday, 4 December 2014

UVA 11902 Dominator Solution

Note: Graph can be disconnected.


///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank / uva / uvalive), 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[105];
bool vis[105], ans[105][105], visOrg[105];
int skip;

void dfs(int u)
{
    vis[u]=true;
    for(int i=0;i<adj[u].size();i++)
    {
        int v=adj[u][i];
        if(!vis[v] && v!=skip)
            dfs(v);
    }
return;
}

void dfsOrg(int u)
{
    //cout<<u<<endl;
    visOrg[u]=true;
    for(int i=0;i<adj[u].size();i++)
    {
        int v=adj[u][i];
        if(!visOrg[v])
            dfsOrg(v);
    }
return;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int kk=1, tc, n, m, x;
    string s;
    cin>>tc;
    while(tc--)
    {
        cin>>n;


        for(int i=0;i<n;i++)   adj[i].clear();
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                cin>>x;
                if(x) adj[i].push_back(j);
            }
        }

        /*for(int i=0;i<n;i++)
        {
            cout<<endl;
            for(int j=0;j<adj[i].size();j++)
                cout<<adj[i][j]<<" ";
        }*/

        CLR(ans);
        //for(int i=0;i<n;i++) ans[0][i]=true;
        CLR(visOrg);
        dfsOrg(0);
        for(int i=0;i<n;i++)
            if(visOrg[i])
                ans[0][i]=true;

        for(int i=1;i<n;i++)
        {
            skip=i;
            CLR(vis);
            dfs(0);
            for(int j=0;j<n;j++)
                if((!vis[j] && visOrg[j]))
                    ans[i][j]=true;
            //if(i==1) cout<<vis[1]<<" "<<visOrg[1]<<endl;
        }



        cout<<"Case "<<kk++<<":\n";
        for(int i=0;i<=n*2;i++)
        {
            if(i%2==0)
            {
                cout<<"+";
                for(int j=0;j<n+n-1;j++)
                    cout<<"-";
                cout<<"+";
            }
            else
            {
                cout<<"|";
                for(int j=0;j<n;j++)
                {

                    if(ans[i/2][j]) cout<<"Y";
                    else cout<<"N";
                    cout<<"|";
                }
            }
            cout<<"\n";

        }
    }

    return 0;
}

No comments:

Post a Comment