Monday, 16 March 2015

UVA 10507 Waking up brain

///     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 100010

int connectionCount[30], waken[30], year, adjMat[30][30];
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m, tc, kk=1;
    string s;
    cin>>n>>m;
    cin>>s;
    for(int i=0; i<3; i++)
    {
        connectionCount[s[0]-'A']=connectionCount[s[1]-'A']=connectionCount[s[2]-'A']=3;
        waken[s[0]-'A']=waken[s[1]-'A']=waken[s[2]-'A']=1;
    }
    int cnt=3;
    for(int i=0; i<m; i++)
    {
        cin>>s;
        adjMat[s[0]-'A'][s[1]-'A']=1;
        adjMat[s[1]-'A'][s[0]-'A']=1;
    }
    getline(cin, s);
    while(1)
    {
        if(cnt==n) break;
        if(year==30) break;
        CLR(connectionCount);
        vector<int>v;
        for(int i=0; i<26; i++)
            for(int j=0; j<26; j++)
            {
                if(adjMat[i][j])
                    if(waken[i] && !waken[j])
                    {
                        connectionCount[j]++;
                        if(connectionCount[j]==3)
                            v.push_back(j);
                    }
            }
        for(int i=0; i<v.size(); i++)
            waken[v[i]]=1;
        cnt+=v.size();
        year++;
    }

    if(year==30) cout<<"THIS BRAIN NEVER WAKES UP\n";
    else cout<<"WAKE UP IN, "<<year<<", YEARS\n";

    while(getline(cin, s))
    {
        CLR(waken);
        year=0;
        CLR(adjMat);
        CLR(connectionCount);
        cin>>n>>m;
        cin>>s;
        for(int i=0; i<3; i++)
        {
            connectionCount[s[0]-'A']=connectionCount[s[1]-'A']=connectionCount[s[2]-'A']=3;
            waken[s[0]-'A']=waken[s[1]-'A']=waken[s[2]-'A']=1;
        }
        int cnt=3;
        for(int i=0; i<m; i++)
        {
            cin>>s;
            adjMat[s[0]-'A'][s[1]-'A']=1;
            adjMat[s[1]-'A'][s[0]-'A']=1;
        }
        getline(cin, s);
        while(1)
        {
            if(cnt==n) break;
            if(year==30) break;
            CLR(connectionCount);
            vector<int>v;
            for(int i=0; i<26; i++)
                for(int j=0; j<26; j++)
                {
                    if(adjMat[i][j])
                        if(waken[i] && !waken[j])
                        {
                            connectionCount[j]++;
                            if(connectionCount[j]==3)
                                v.push_back(j);
                        }
                }
            for(int i=0; i<v.size(); i++)
                waken[v[i]]=1;
            cnt+=v.size();
            year++;
        }

        if(year==30) cout<<"THIS BRAIN NEVER WAKES UP\n";
        else cout<<"WAKE UP IN, "<<year<<", YEARS\n";
    }
    return 0;
}

No comments:

Post a Comment