系统依赖 System Dependencies UVA506

题目描述

输入格式

输出格式

 

【输入样例】

DEPEND TELNET TCPIP NETCARD
DEPEND TCPIP NETCARD
DEPEND DNS TCPIP NETCARD
DEPEND BROWSER TCPIP HTML
INSTALL NETCARD
INSTALL TELNET
INSTALL foo
REMOVE NETCARD
INSTALL BROWSER
INSTALL DNS
LIST
REMOVE TELNET
REMOVE NETCARD
REMOVE DNS
REMOVE NETCARD
INSTALL NETCARD
REMOVE TCPIP
REMOVE BROWSER
REMOVE TCPIP
END

【输出样例】

DEPEND TELNET TCPIP NETCARD
DEPEND TCPIP NETCARD
DEPEND DNS TCPIP NETCARD
DEPEND BROWSER TCPIP HTML
INSTALL NETCARD
Installing NETCARD
INSTALL TELNET
Installing TCPIP
Installing TELNET
INSTALL foo
Installing foo
REMOVE NETCARD
NETCARD is still needed.
INSTALL BROWSER
Installing HTML
Installing BROWSER
INSTALL DNS
Installing DNS
LIST
NETCARD
TCPIP
TELNET
foo
HTML
BROWSER
DNS
REMOVE TELNET
Removing TELNET
REMOVE NETCARD
NETCARD is still needed.
REMOVE DNS
Removing DNS
REMOVE NETCARD
NETCARD is still needed.
INSTALL NETCARD
NETCARD is already installed.
REMOVE TCPIP
TCPIP is still needed.
REMOVE BROWSER
Removing BROWSER
Removing HTML
Removing TCPIP
REMOVE TCPIP
TCPIP is not installed.
END
#include <iostream>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
#include <sstream>
using namespace std;

const int maxn = 10000 + 10;
vector<int> depend[maxn], depend2[maxn];
vector<int> installed;
map<string, int> ID;
string name[maxn];
int cnt;
int status[maxn];

int find_ID(string &item)
{
    if (!ID.count(item))
    {
        ID[item] = cnt;
        name[cnt++] = item;
    }
    return ID[item];
}

bool needed(int id)
{
    for (int i = 0; i < depend2[id].size(); i++)
    {
        if (status[depend2[id][i]])
            return true;
    }
    return false;
}

void List()
{
    for (int i = 0; i < installed.size(); i++)
    {
        cout << "   " << name[installed[i]] << endl;
    }
}

void install(int id, bool toplevel)
{
    if (!status[id])
    {
        for (int i = 0; i < depend[id].size(); i++)
        {
            install(depend[id][i], false);
        }
        status[id] = toplevel ? 1 : 2;
        cout << "   Installing " << name[id] << endl;
        installed.push_back(id);
    }
}

void Remove(int id, bool toplevel)
{
    if ((toplevel || status[id] == 2) && !needed(id))
    {
        installed.erase(remove(installed.begin(), installed.end(), id), installed.end());
        cout << "   Removing " << name[id] << endl;
        status[id] = 0;
        for (int i = 0; i < depend[id].size(); i++)
        {
            Remove(depend[id][i], false);
        }
    }
}

int main()
{
    //freopen("input.txt","r",stdin);
    cnt = 0;
    string ope;
    memset(status, 0, sizeof(status));
    while (getline(cin, ope))
    {
        cout << ope << endl;
        stringstream ss(ope);
        ss >> ope;
        if (ope[0] == 'E')
            break;
        string item1, item2;
        if (ope[0] == 'L')
            List();
        else
        {
            ss >> item1;
            int id = find_ID(item1);
            if (ope[0] == 'D')
            {
                while (ss >> item2)
                {
                    int id2 = find_ID(item2);
                    depend[id].push_back(id2);
                    depend2[id2].push_back(id);
                }
            }
            else if (ope[0] == 'I')
            {
                if (status[id])
                    cout << "   " << item1 << " is already installed." << endl;
                else
                    install(id, true);
            }
            else
            {
                if (!status[id])
                    cout << "   " << item1 << " is not installed." << endl;
                else if (needed(id))
                    cout << "   " << item1 << " is still needed." << endl;
                else
                    Remove(id, true);
            }
        }
    }
    system("pause");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/stu-jyj3621/p/13395502.html