[NOIP Simulation Test]: party (Hall Theorem + + minimal cut the DP)

Title Description

$ Treeland $ State $ n-$ cities, where $ 1 $ number city is the capital, the cities are connected to some of the one-way high-speed rail line, for urban $ i \ NEQ 1 $, there is a line $ from $ i to $ p_i (p_i < i) $. Each line is the same length, but also by spending time as long.
Every city in this country have a kind of specialty, the entire country has $ m $ kinds of specialty (different cities may turn the same specialty), in which the city $ i $ specialty is represented by $ a_i $.
Small $ C $ and several of his team $ A $ Ye friends (a total of $ c $ person, $ 2 \ leqslant c \ leqslant 5 $) is $ Treeland $ country to play, they are ready to be $ water party $ in a city. Held $ party $ city must satisfy everyone in attendance from each city can as soon as possible. Note Could someone in the same city.
Small $ C $ and his friends prepare their specialties to bring some $ party $. These specialty products must meet the following criteria:
    . $ \ Alpha with a number of specialty $ everybody must be the same.
    $ \ beta.party $ in any two can not have the same specialty.
    $ \ gamma. $ each person can only take him through the city's specialty.
For each inquiry, calculated up to $ party $ There are many kinds of specialty.


Input Format

The first row of three integers $ n, m, q $, each represent a number of cities, specialty species, the number of interrogation.
The second line has $ n-1 $ integers representing $ p_1, p_2, p_3, ... , p_n $.
The third row has $ n $ integers representing $ a_1, a_2, ..., a_n $.
The next $ q $ rows, each row represents a challenge. Each inquiry first integer $ c $ represent the number, then there is an integer $ c $ represent cities where the number of each individual.


Output Format

For each query output line an integer that represents the answer.


Sample

Sample input:

5 3 4
1 2 2 1
2 3 1 3 1
2 3 4
3 5 2 2
4 3 4 2 5
2 2 2

Sample output:

2
3
0
0


Data range and tips

For $ 100 \% $ of data, are met:

$2\leqslant n\leqslant 300,000,1\leqslant m\leqslant 1,000,0\leqslant q\leqslant 50,000,1\leqslant p_i<i,1\leqslant a_i\leqslant m,2\leqslant c\leqslant 5$。


answer

Continuation of the questions the idea, found that only a maximum of $ 5 $ personal, then consider the network flow problem.

Easy (I do not easily) found for everyone even a certain capacity from the source side (capacity utilization dichotomy can be), everyone to his color even edges, color again meeting point even the capacity of $ 1 $ edges can run over it is possible, minimum cut

Go back to sleep sleep ~

Guess you like

Origin www.cnblogs.com/wzc521/p/11415182.html