#P15295. [ROI 2012 Day 1] virus 病毒与杀毒软件

    ID: 17374 远端评测题 2000ms 256MiB 尝试: 0 已通过: 0 显示难度提高 上传者: 标签>2012树状数组树论ROI(俄罗斯)

[ROI 2012 Day 1] virus 病毒与杀毒软件

Background

Translation source: loj #5456. "ROI 2012 Day 1" Virus and Antivirus Software.

Problem Description

An antivirus IT company has an official management hierarchy. In this structure, there is a boss who is the only employee without a supervisor. Every other employee has exactly one direct supervisor. Each supervisor can have multiple subordinates, and can pass orders to any subordinate either directly or through a chain of subordinates. Orders can only be passed along chains from supervisors to subordinates.

In this hierarchy, if employee AA can pass orders to employee BB either directly or through a chain of subordinates, then AA is said to have a higher status than BB. The boss has a higher status than any employee.

However, it is discovered that all employees also form a similar secret hierarchy used to develop computer viruses. In this secret structure, the boss and the supervisor relationships may be different.

We call a pair of employees AA and BB a stable pair if AA has a higher status than BB in both the official hierarchy and the secret hierarchy.

You need to write a program to compute the number of stable pairs in the company.

Input Format

The first line of the input file contains an integer NN (1≤N≤100000)(1 \leq N \leq 100000), representing the number of employees in the company.

The second line contains NN integers aia_i. If employee ii is the boss in the official hierarchy, then ai=0a_i = 0; otherwise, aia_i is the index of this employee’s direct supervisor.

The third line contains NN integers bib_i. If employee ii is the boss in the secret hierarchy, then bi=0b_i = 0; otherwise, bib_i is the index of this employee’s direct supervisor.

Employees are numbered starting from 11, in the order mentioned in the input file.

Output Format

The output file should contain one integer, the number of stable pairs.

3
0 3 1
0 1 1

2
5
2 0 1 3 4
3 1 0 2 4
7

Hint

The detailed additional constraints and scores for each subtask are shown in the table below:

Subtask Score Additional Constraints
11 2525 Number of employees N≤100N \leq 100
22 Number of employees N≤2000N \leq 2000
33 5050 Number of employees N≤100000N \leq 100000

Translated by ChatGPT 5