"尚学堂杯"哈尔滨理工大学第七届程序设计竞赛 D(2328).Distinct Package Manager(思路)

115 篇文章 0 订阅
68 篇文章 0 订阅

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
1
2
1
0
2
install 1
uninstall 0
Hint
2
2
Source
“尚学堂杯”哈尔滨理工大学第七届程序设计竞赛


思路:
用动态二维数组v存储安装每一个软件之前需要安装的软件,v[x][y]代表安装x之前需要安装y,v[x].size()代表安装x之前需要安装的软件个数;
同理用动态二维数组rev存储卸载每一个软件之前需要卸载的软件。

然后根据命令进行查询就好了

代码:

#include<stdio.h>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std;

#define maxn 105
vector<int>v[maxn];
vector<int>rev[maxn];
bool installed[maxn];

int install(int x)
{
    int ans=0;
    if(installed[x])
        return 0;
    installed[x]=true;
    for(int i=v[x].size()-1; ~i; --i)
        ans+=install(v[x][i]);
    return ans+1;
}

int uninstall(int x)
{
    int ans=0;
    if(!installed[x])
        return 0;
    installed[x]=false;
    for(int i=rev[x].size()-1; ~i; --i)
        ans+=uninstall(rev[x][i]);
    return ans+1;
}

int main()
{
    int T,n,m,t,x;
    char str[10];
    scanf("%d",&T);
    while(T--)
    {
        memset(installed,false,sizeof(installed));
        scanf("%d",&n);
        for(int i=0; i<n; ++i)
            v[i].clear(),rev[i].clear();
        for(int i=1; i<n; ++i)
        {
            scanf("%d",&t);
            for(int j=1; j<=t; ++j)
            {
                scanf("%d",&x);
                v[i].push_back(x);
                rev[x].push_back(i);
            }
        }
        scanf("%d",&m);
        for(int i=0; i<m; ++i)
        {
            scanf("%s %d",str,&x);
            printf("%d\n",str[0]=='i'?install(x):uninstall(x));
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值