Distinct Package Manager | ||||||
| ||||||
Description | ||||||
On Linux or OSX, we can install software by package manager. For example, apt-get in Ubuntu/Debian, yum in Fedora/CentOS and brew in OSX. All of them are great software-package manager. You determined to design your own software-package manager. The inevitable thing is you should solve dependences of these software-packages. • If package A depends package B, you should install package B before installing package A. • If you want to uninstall package B, then you must uninstall package A. Now, you already know all the dependences of all these software-packages. You can assume that 0-package don’t depend any other package. And all dependence-relationship won’t form a circle. Of course, no package depend itself. Your uses want to know how many packages’ install state will be changed when installing or uninstalling a package. NOTE: Install an installed package and uninstall an uninstalled package won’t change any packages’ state.
| ||||||
Input | ||||||
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. The first line of each test cases contains an integer n(1<=n<=100), indicating the number of software-packages. For each software-package(except 0-package) contains two lines, the first line contains an integer mi, indicating the number of dependences of i-package. The next mi integers indicating the serial numbers of dependences. The next line contains an integer q(1<=q<=200), indicating the number of queries. Each of the next q liens contains a string s and an integer k, indicating the action and serial number of software-package. s must be one of "install" and "uninstall". | ||||||
Output | ||||||
For each query, output a line contains the number of changed packages. | ||||||
Sample Output | ||||||
12102install 1uninstall 0 | ||||||
Hint | ||||||
22 | ||||||
Source | ||||||
"尚学堂杯"哈尔滨理工大学第七届程序设计竞赛 |
同理用动态二维数组haha存储卸载每一个软件之前需要卸载的软件。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<map>
#include<stdlib.h>
#include<vector>
using namespace std;
int aha[1000];
vector<int>ha[1000];
vector<int>haha[1000];
int sum3=0;
int sum4=0;
int la(int x)
{
if(aha[x]==1)
return 0;
int sum1=0;
aha[x]=1;
for(int i=ha[x].size()-1;i>=0;i--)
{
sum1+=la(ha[x][i]);
}
return sum1+1;
}
int lala(int x)
{
int sum2=0;
if(aha[x]==0)
return 0;
aha[x]=0;
for(int i=haha[x].size()-1;i>=0;i--)
{
sum2+=lala(haha[x][i]);
}
return sum2+1;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int we,ni;
for(int i=0; i<n; ++i)
ha[i].clear(),haha[i].clear();
memset(aha,0,sizeof(aha));
for(int i=1;i<n;i++)
{
scanf("%d",&we);
for(int j=0;j<we;j++)
{
scanf("%d",&ni);
ha[i].push_back(ni);
haha[ni].push_back(i);
}
}
int m;
scanf("%d",&m);
char s[100];
int ta;
for(int i=0;i<m;i++)
{
scanf("%s%d",s,&ta);
if(s[0]=='i')
printf("%d\n",la(ta));
else
printf("%d\n",lala(ta));
}
}
}