例题6-21 uva506 System Dependencies 模拟

来源:互联网 发布:windows经典桌面壁纸 编辑:程序博客网 时间:2024/05/17 07:22

题目链接:https://vjudge.net/problem/UVA-506

Code:

#include <bits/stdc++.h>using namespace std;const int maxn = 1e5+666;map<string,int>mp;map<int,string>lis;int status[maxn];std::vector<int> depend[maxn]; std::vector<int> depended[maxn];std::vector<int> installed;int cnt = 0;int getid( string s ){if( mp.count(s) ) return mp[s];else{mp[s] = ++cnt ;lis[cnt] = s;return cnt;}}void INSTALL( int u , bool lim ){if( status[u] && lim ) {cout << "   " << lis[u] << " is already installed." << endl;return ;}if( status[u] ) return ;for( int i = 0 ; i < depend[u].size() ; i ++ ){INSTALL( depend[u][i] , false );}cout << "   Installing " << lis[u] << endl;status[u] = ( lim ? 1 : 2 );installed.push_back(u);} bool need( int u ){for( int i = 0; i < depended[u].size() ; i ++ )if( status[depended[u][i]] ) return true;return false;}void REMOVE( int u , bool lim ){if( lim  && !status[u] ) {cout << "   " << lis[u] << " is not installed." << endl; return;}if( ( lim || status[u] == 2 ) && !need(u) ){status[u] = 0;installed.erase( remove(installed.begin(),installed.end(),u) ,installed.end() );cout << "   Removing " << lis[u] << endl;for( int i = 0 ; i < depend[u].size() ; i++ )REMOVE( depend[u][i] ,false ) ;return ;}if( lim ) cout << "   " << lis[u] << " is still needed." << endl;}int main(){//freopen("506.txt","r" , stdin);string op;memset(status,0,sizeof(status));while( getline(cin , op) ){cout << op << endl;if( op == "END" ) break;stringstream ss(op);string s1 , s2;ss >> s1;if( s1[0] == 'I' ){while( ss >> s2)INSTALL( getid(s2) , true );}else if( s1[0] == 'D'  ){ss >> s2;int item = getid(s2);string s3; while( ss >> s3 ){int id = getid(s3);depend[item].push_back(id);depended[id].push_back(item);}}else if( s1[0] == 'R'  ){ss >> s2 ;REMOVE(getid(s2),true);}else if( s1[0] == 'L' ) {for( int i = 0 ; i < installed.size() ; i++ ){cout << "   " << lis[installed[i]] << endl;}}}return 0;}

原创粉丝点击