#ds903. 软件包管理器
软件包管理器
Software Package Manager
Problem Source
NOI 2015
Problem Description
Linux users and OS X users are certainly familiar with package managers. With a package manager you can install a software package with a single command, and the manager then downloads it from the source and automatically resolves all dependencies (downloading and installing whatever other packages that package depends on), finishing all configuration. apt-get on Debian/Ubuntu, yum on Fedora/CentOS, and homebrew on OS X are all excellent package managers.
You decide to design your own package manager. Inevitably, you must solve the dependency problem among packages. If package depends on package , then before installing you must first install . Likewise, to uninstall you must first uninstall . You have already obtained all dependency relations between packages. Moreover, thanks to your previous work, every package other than package depends on exactly one other package, while package depends on none. There are no dependency cycles (a cycle would mean depends on , on , ..., on , and on , for some ), and of course no package depends on itself.
Now you must write the dependency resolver for your package manager. Based on user feedback, when installing or uninstalling a package users want to know quickly how many packages' installation states this operation actually changes (i.e. how many uninstalled packages an install operation installs, or how many installed packages an uninstall operation removes). Your task is to implement this part. Note that installing an already-installed package, or uninstalling an already-uninstalled package, changes no package's installation state — in those cases the number of changed packages is .
Input Format
The first line of the input file contains one positive integer , the total number of packages. Packages are numbered starting from .
The following line contains integers separated by single spaces, indicating the package that packages depend on, respectively.
The next line contains one positive integer , the number of queries.
Then follow lines, each describing one query. There are two kinds of queries:
install x: install package .uninstall x: uninstall package .
You must maintain the installation state of every package; initially all packages are uninstalled. For each operation, output how many packages' installation state this step changes, and then apply the operation (i.e. update the installation states you maintain).
It is guaranteed that .
Output Format
The output file contains lines. The -th line outputs one integer: the number of packages whose installation state changed in the -th operation.
Sample Input
7
0 0 0 1 1 5
5
install 5
install 6
uninstall 1
install 4
uninstall 0
Sample Output
3
1
3
2
3
Sample Explanation
Initially all packages are uninstalled.
Installing package requires installing packages — three packages.
Then installing package only requires installing package itself. At this point are installed.
Uninstalling package requires uninstalling packages . Now only package remains installed.
Then installing package requires installing packages . At this point are installed.
Finally, uninstalling package uninstalls all packages.