系统依赖 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值