例题6-21 系统依赖(System Dependencies, ACM/ICPC World Finals 1997, UVa506)

STL函数remove的用法:
Transforms the range [first,last) into a range with all the elements that compare equal to val removed,
and returns an iterator to the new end of that range.
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cctype>
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define LL long long
#define maxn 10005
#define maxm 200005
#define mod 1000000007
#define INF 1000000007
#define eps 1e-5
#define PI 3.1415926535898
#define N 26
using namespace std;
//-------------------------CHC------------------------------//
vector<string> names;
map<string, int> id;
vector<int> before[maxn], after[maxn];
vector<int> installed;
int status[maxn];

int ID(string s) {
	if (id.count(s)) return id[s];
	names.push_back(s);
	return id[s] = names.size() - 1;
}

void install(int u) {
	for (int i = 0; i < before[u].size(); ++i)
		install(before[u][i]);
	if (!status[u]) {
		status[u] = 2;
		cout << "   Installing " << names[u] << endl;
		installed.push_back(u);
	}
}

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

void remove(int u) {
	status[u] = 0;
	installed.erase(remove(installed.begin(), installed.end(), u), installed.end());
	cout << "   Removing " << names[u] << endl;
	for (int i = 0; i < before[u].size(); ++i) {
		int v = before[u][i];
		if (status[v] == 2 && !needed(v))
			remove(v);
	}
}

int main() {
	//IN(); OUT();
	string in;
	while (getline(cin, in)) {
		cout << in << endl;
		if (in[0] == 'E') break;
		stringstream ss(in);
		string op;
		string name;
		ss >> op;
		if (op[0] == 'L') {
			for (int i = 0; i < installed.size(); ++i)
				cout << "   " << names[installed[i]] << endl;
		}
		else {
			ss >> name;
			int u = ID(name);
			if (op[0] == 'D') {
				while (ss >> name) {
					int v = ID(name);
					before[u].push_back(v);
					after[v].push_back(u);
				}
			}
			else if (op[0] == 'I') {
				if (status[u]) cout << "   " << name << " is already installed.\n";
				else {
					install(u);
					status[u] = 1;
				}
			}
			else if (op[0] == 'R') {
				if (status[u] == 0) cout << "   " << name << " is not installed.\n";
				else if (needed(u)) cout << "   " << name << " is still needed.\n";
				else remove(u);
			}
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值