Distinct Package Manager

Distinct Package Manager

Time Limit: 1000 MS

Memory Limit: 512000 K

Total Submit: 88(26 users)

Total Accepted: 29(19 users)

Rating:

Special Judge: No

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

"尚学堂杯"哈尔滨理工大学第七届程序设计竞赛

二维数组v存储安装每一个软件之前需要安装的软件,ha[x][y]代表安装x之前需要安装y,ha[x].size()代表安装x之前需要安装的软件个数;
同理用动态二维数组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));
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值