#SRD0000091. Ghép cặp theo sở thích (Dating)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Ghép cặp theo sở thích (Dating)
Dating
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
The town of BubbleLand contains houses numbered from through . There are bidirectional roads, with exactly one simple path between any two houses.
Each house contains exactly one resident, either a boy or a girl. The resident of house has a favorite number . A valid couple consists of one boy and one girl from different houses with equal favorite numbers; the order of the two people does not matter.
For each query , consider only the residents of houses on the unique path from to , including both endpoints. Count how many valid couples can be formed using these residents.
Input
- The first line contains .
- The next line contains values , where denotes a boy and a girl at house .
- The next line contains favorite numbers .
- The following lines each contain , describing a bidirectional road between the houses.
- The next line contains .
- The last lines each contain query endpoints .
Output
For each query, print the number of boy–girl couples with matching favorite numbers on the corresponding path.
Subtasks
- Subtask 1 (10 points): , .
- Subtask 2 (20 points): , .
- Subtask 3 (30 points): , .
- Subtask 4 (40 points): , .
Complete conditions for all subtasks, subject to the tighter limits above: , , , .
Examples
Input
7
1 0 0 1 0 1 0
9 2 9 2 2 9 9
2 6
1 2
4 2
6 5
3 6
7 4
2
1 3
7 5
Output
2
3
Explanation
The path from house to house is . Its residents have pairs (gender, favorite number) . The girl in house matches the boys in houses and , yielding 2 couples.
The path from house to house is . Its three couples are , with favorite number , and , , with favorite number . Thus the second answer is 3.