UVa 506 System Dependencies (detail issue)

Source: Internet
Author: User

Test instructions: Enter several instructions to allow you to perform the simulation operation with the following instructions:

DEPEND item1 item2 (Item3 ...) Installing ITEM1 requires first installing item2 (, item3 ...). )
INSTALL item1 Install Item1, and if ITEM1 relies on other components, install the other components that it depends on (if it is installed without installing it)
REMOVE item1 Remove item1 and all components it relies on, if the component is dependent on another program, does not remove
LIST Output all components that are currently installed
END End Program

Analysis: See this problem, although it is placed in the data structure of this chapter, did not feel what data structure of things, the algorithm is not difficult, but the operation of a little trouble, but also adjusted for a long time, began to use the 1024vector,

Re, and then I increased 10 times times, there are some later I see the online sample is not the same, it is clear that I did not read test instructions, may be removed when the order is irrelevant.

The first thing is to convert each name into an ID number, I used two map, actually can not, then the time complexity may be a little higher, then to two vectors, a storage dependent component, a list of dependent components.

Then also use an array to record whether the installation is not installed, or whether it is installed implicitly or in the display. Recursive implementation is preferable when installing and deleting.

The main points to note are:

Output is cumbersome, if you have installed the explicit installation, to output has been installed, implicit directly ignored.

If you want to remove an explicit installation, in addition to itself, then the output is still required.

If you want to remove an explicit installation that does not exist, the output is not installed.

The code is as follows:

#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <sstream > #include <map> #include <algorithm>using namespace std;const int maxn = 10240;vector<int> V1[MAXN] ;vector<int> v2[maxn];vector<int> is_install;map<string, int> mp;map<int, string> item;int    CNT = 0;int status[maxn];//0 means not installed, 1 means implicit installation, 2 means explicitly installing int getid (const string &s) {//Get ID if (Mp.count (s)) return mp[s];    Mp[s] = ++cnt;    ITEM[CNT] = s; return CNT;} void Install (int t, bool OK) {if (status[t] && ok) {cout << "<< item[t] <<" is already  Installed.\n "; return; }//an explicit installation has already been installed, to output already installed if (status[t]) return;//implicit installation, ignoring for (int i = 0; i < v1[t].size (); ++i) Install (v1[t    ][i], false);//recursive lookup installation cout << "Installing" << item[t] << Endl; Status[t] = OK? 1:2;//determines whether the Is_install.push_back (t) is implicitly installed or explicitly installed; bool Need (int t) {for (int i = 0; i < V2[t].sizE (); ++i) if (Status[v2[t][i]]) return true;//This place I just started writing is 2 = = Status[v2[t][i]],//result is not correct, because first in the storage is determined, the deposit is explicitly installed , and do not have to re-determine,//As long as the decision is not removed is good, the other is in the installation of the experiment its state may change, so cannot write return false;} void remove1 (int t, bool OK) {if (ok &&!status[t]) {cout << "<< item[t] <<" is not ins  Talled.\n "; return;    }//Delete an explicit installation that does not exist, and the output is not installed.        if (ok | | status[t] = = 2) &&!need (t)) {status[t] = 0; Is_install.erase (Remove (Is_install.begin (), Is_install.end (), T), Is_install.end ()),//remove not really delete elements, but instead put them in the back,        and return to the cout << "removing" << item[t] << Endl;        for (int i = V1[t].size ()-1; I >= 0; i)//This place if starting from 0 is also right, should be I did not read test instructions remove1 (V1[t][i], false);    return; if (OK) cout << "" << Item[t] << "is still needed.\n";//delete explicit installation, except itself, then the output is still required.    }int Main () {//Freopen ("In.txt", "R", stdin);    string S;    memset (status, 0, sizeof (status)); while (Getline (CIN, s)) {        cout << s << endl;        if (s = = "END") break;        StringStream SS (s);        string s1, S2;        SS >> S1;            if (' D ' = = S1[0]) {SS >> S2;            int t = getid (S2);  while (SS >> s) {V1[t].push_back (GetID (s));  V2[getid (s)].push_back (t);        }} else if (' I ' = = S1[0]) {while (SS >> S2) Install (GetID (S2), true);  } else if (' R ' = = S1[0]) {SS >> S2;        Remove1 (GetID (S2), true);   } else if (' L ' = = S1[0]) {for (int i = 0; i < is_install.size (); ++i) cout << "        "<< Item[is_install[i]" << Endl; }} return 0;}

UVa 506 System Dependencies (detail issue)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.