#P15654. [省选联考 2026] 工业系统

    ID: 17717 远端评测题 4000ms 1024MiB 尝试: 0 已通过: 0 显示难度NOI/NOI+/CTS 上传者: 标签>线段树各省省选O2优化树链剖分哈希 hashing分块2026

[省选联考 2026] 工业系统

Background

Recently, while doing archaeology in her own garden, Xiaofei found some industrial devices that looked very old. Out of curiosity, she suddenly decided to test the performance of these devices.

So she found some conveyor belts and, following the shape of a peach tree in the garden, connected these devices together to form an industrial system.

As for the specific testing method, you will naturally answer Xiaofei’s questions. Surely you will not refuse.

Problem Description

You are given an industrial system containing nn devices, numbered 1∼n1 \sim n. These devices are connected by n−1n-1 directed conveyor belts whose directions can be changed, forming an unrooted tree.

When using this system, you first need to choose a device xx (1≤x≤n1 \le x \le n) as the final device, and then set the direction of every conveyor belt to point toward that device. Formally, device xx is chosen as the root of the tree, and all conveyor belts are directed toward the root, forming an inward rooted tree. After the directions are fixed, all products will be transported along the conveyor directions.

Each device takes the products of all its descendants as input products, then produces a new product, and outputs it along the conveyor belt to all its ancestors. Formally, let the product of device yy (1≤y≤n1 \le y \le n) be ax,ya_{x,y}. Let all devices in the subtree of yy except itself be z1,…,zkz_1, \dots, z_k. Then its product can be represented as the multiset ax,y={ax,z1,…,ax,zk}a_{x,y} = \{a_{x,z_1}, \dots, a_{x,z_k}\}. In particular, if device yy is a leaf device, i.e. yy has no descendants, then ax,y=∅a_{x,y} = \varnothing.

To compare product quality, define the order between products as follows. When device xx (1≤x≤n1 \le x \le n) is chosen as the final device, for products ax,y,ax,za_{x,y}, a_{x,z} of devices y,zy, z (1≤y,z≤n1 \le y, z \le n), sort all elements in ax,ya_{x,y} and ax,za_{x,z} (i.e. all input products of devices y,zy, z) from large to small, obtaining two sequences. The lexicographic order of these two sequences is defined as the order between ax,ya_{x,y} and ax,za_{x,z}. Formally, let ax,y={b1,b2,…,bp}a_{x,y} = \{b_1, b_2, \dots, b_p\}, ax,z={c1,c2,…,cq}a_{x,z} = \{c_1, c_2, \dots, c_q\}, where b1≥b2≥⋯≥bpb_1 \ge b_2 \ge \dots \ge b_p and c1≥c2≥⋯≥cqc_1 \ge c_2 \ge \dots \ge c_q. Then:

  • ax,y>ax,za_{x,y} > a_{x,z} if and only if one of the following holds:
    • There exists a positive integer i∈[1,min⁡(p,q)]i \in [1, \min(p, q)] such that bi>cib_i > c_i, and for all 1≤j<i1 \le j < i we have bj=cjb_j = c_j.
    • For all 1≤i≤min⁡(p,q)1 \le i \le \min(p, q) we have bi=cib_i = c_i, and p>qp > q.
  • ax,y=ax,za_{x,y} = a_{x,z} if and only if p=qp = q and for all 1≤i≤p1 \le i \le p we have bi=cib_i = c_i.

After defining the order, we define the rank of a product. Specifically, define f(x,y)f(x, y) (1≤x,y≤n1 \le x, y \le n) as: when device xx is chosen as the final device, the rank of device yy’s product ax,ya_{x,y} among all nn products. Formally,

f(x,y)=1+∑z=1n[ax,z>ax,y].f(x, y) = 1 + \sum_{z=1}^{n} [a_{x,z} > a_{x,y}].

To fully analyze the role of each device in this industrial system, you need to answer mm queries. Each query is:

  • Given five parameters s,t,ox,oy,rs, t, o_x, o_y, r.
  • Define$$X = \begin{cases} \{s\}, & o_x = 0, \\ \text{ch}(s,t), & o_x = 1, \end{cases} \quad Y = \begin{cases} \{t\}, & o_y = 0, \\ \text{ch}(s,t), & o_y = 1, \end{cases}$$
  • where ch(s,t)\text{ch}(s,t) denotes the set of all devices on the simple path from device ss to device tt.
  • Find how many pairs (x,y)(x, y) satisfy x∈Xx \in X, y∈Yy \in Y, and f(x,y)≤rf(x, y) \le r.

Input Format

The first line contains a non-negative integer cc, the test point ID. c=0c = 0 means this test point is the sample.

The second line contains a positive integer nn, the number of devices in the industrial system.

The (i+2)(i+2)-th line (1≤i≤n−11 \le i \le n-1) contains two positive integers ui,viu_i, v_i, meaning the ii-th conveyor belt connects devices uiu_i and viv_i.

The (n+2)(n+2)-th line contains a positive integer mm, the number of queries.

The (i+n+2)(i+n+2)-th line (1≤i≤m1 \le i \le m) contains five non-negative integers s,t,ox,oy,rs, t, o_x, o_y, r, the parameters of the ii-th query.

Output Format

Output mm lines. The ii-th line (1≤i≤m1 \le i \le m) contains a non-negative integer, the answer to the ii-th query.

0
3
1 2
2 3
6
1 2 0 0 2
1 3 0 0 2
2 3 0 0 2
2 1 0 0 2
3 1 0 0 2
3 2 0 0 2
1
0
1
1
0
1

Hint

Sample 1 Explanation.

  • When device 11 is the root:
    • Device 33 is a leaf device, so a1,3=∅a_{1,3} = \varnothing.
    • The descendants of device 22 are device 33, so a1,2={∅}a_{1,2} = \{\varnothing\}.
    • The descendants of device 11 are devices 2,32, 3, so a1,1={∅,{∅}}a_{1,1} = \{\varnothing, \{\varnothing\}\}.
    • Therefore a1,1>a1,2>a1,3a_{1,1} > a_{1,2} > a_{1,3}, i.e. f(1,1)=1f(1,1) = 1, f(1,2)=2f(1,2) = 2, f(1,3)=3f(1,3) = 3.
  • When device 22 is the root:
    • Devices 1,31, 3 are leaf devices, so a2,1=a2,3=∅a_{2,1} = a_{2,3} = \varnothing.
    • The descendants of device 22 are devices 1,31, 3, so a2,2={∅,∅}a_{2,2} = \{\varnothing, \varnothing\}.
    • Therefore a2,2>a2,1=a2,3a_{2,2} > a_{2,1} = a_{2,3}, i.e. f(2,2)=1f(2,2) = 1, f(2,1)=f(2,3)=2f(2,1) = f(2,3) = 2.
  • When device 33 is the root:
    • Device 11 is a leaf device, so a3,1=∅a_{3,1} = \varnothing.
    • The descendants of device 22 are device 11, so a3,2={∅}a_{3,2} = \{\varnothing\}.
    • The descendants of device 33 are devices 1,21, 2, so a3,3={∅,{∅}}a_{3,3} = \{\varnothing, \{\varnothing\}\}.
    • Therefore a3,3>a3,2>a3,1a_{3,3} > a_{3,2} > a_{3,1}, i.e. f(3,3)=1f(3,3) = 1, f(3,2)=2f(3,2) = 2, f(3,1)=3f(3,1) = 3.

Sample 2.

See industry/industry2.in and industry/industry2.ans in the contestant directory.

Sample 2 Explanation.

  • When device 11 is the root, a1,2=a1,6=a1,5=∅a_{1,2} = a_{1,6} = a_{1,5} = \varnothing, a1,4={∅}a_{1,4} = \{\varnothing\}, $a_{1,3} = \{\varnothing, \varnothing, \{\varnothing\}\}$, $a_{1,1} = \{\varnothing, \varnothing, \varnothing, \{\varnothing\}, \{\varnothing, \varnothing, \{\varnothing\}\}\}$, so $a_{1,1} > a_{1,3} > a_{1,4} > a_{1,2} = a_{1,5} = a_{1,6}$.
  • When device 22 is the root, $a_{2,2} > a_{2,4} > a_{2,3} > a_{2,1} > a_{2,5} = a_{2,6}$.
  • When device 33 is the root, $a_{3,3} > a_{3,1} = a_{3,4} > a_{3,2} = a_{3,5} = a_{3,6}$.
  • When device 44 is the root, $a_{4,4} > a_{4,3} > a_{4,1} > a_{4,2} = a_{4,5} = a_{4,6}$.
  • When device 55 is the root, $a_{5,5} > a_{5,1} > a_{5,3} > a_{5,4} > a_{5,2} = a_{5,6}$.
  • When device 66 is the root, $a_{6,6} > a_{6,3} > a_{6,1} = a_{6,4} > a_{6,2} = a_{6,5}$.

Sample 3.

See industry/industry3.in and industry/industry3.ans in the contestant directory.

Sample 4.

See industry/industry4.in and industry/industry4.ans in the contestant directory.

This sample satisfies ox=oy=0o_x = o_y = 0.

Sample 5.

See industry/industry5.in and industry/industry5.ans in the contestant directory.

This sample satisfies ox=0o_x = 0 and oy=1o_y = 1.

Sample 6.

See industry/industry6.in and industry/industry6.ans in the contestant directory.

This sample satisfies ox=1o_x = 1 and oy=0o_y = 0.

Sample 7.

See industry/industry7.in and industry/industry7.ans in the contestant directory.

This sample satisfies ox=oy=1o_x = o_y = 1.

Sample 8.

See industry/industry8.in and industry/industry8.ans in the contestant directory.

This sample satisfies the constraints of test point 11.

Sample 9.

See industry/industry9.in and industry/industry9.ans in the contestant directory.

This sample satisfies the constraints of test point 22.

Sample 10.

See industry/industry10.in and industry/industry10.ans in the contestant directory.

This sample satisfies the constraints of test points 3,43,4.

Sample 11.

See industry/industry11.in and industry/industry11.ans in the contestant directory.

This sample satisfies the constraints of test points 5,65,6.

Sample 12.

See industry/industry12.in and industry/industry12.ans in the contestant directory.

This sample satisfies the constraints of test points 7,87,8.

Sample 13.

See industry/industry13.in and industry/industry13.ans in the contestant directory.

This sample satisfies the constraints of test points 9∼119\sim 11.

Sample 14.

See industry/industry14.in and industry/industry14.ans in the contestant directory.

This sample satisfies the constraints of test points 12∼1412\sim 14.

Sample 15.

See industry/industry15.in and industry/industry15.ans in the contestant directory.

This sample satisfies the constraints of test points 15∼1715\sim 17.

Sample 16.

See industry/industry16.in and industry/industry16.ans in the contestant directory.

This sample satisfies the constraints of test points 18,1918,19.

Sample 17.

See industry/industry17.in and industry/industry17.ans in the contestant directory.

This sample satisfies the constraints of test points 20,2120,21.

Sample 18.

See industry/industry18.in and industry/industry18.ans in the contestant directory.

This sample satisfies the constraints of test points 22∼2422\sim 24.

Sample 19.

See industry/industry19.in and industry/industry19.ans in the contestant directory.

This sample satisfies the constraints of test point 2525.

Constraints

For all testdata:

  • 2≤n≤1052 \le n \le 10^5.
  • For all 1≤i≤n−11 \le i \le n-1, 1≤ui,vi≤n1 \le u_i, v_i \le n, and (u1,v1),…,(un−1,vn−1)(u_1, v_1), \dots, (u_{n-1}, v_{n-1}) form a tree.
  • 1≤m≤1051 \le m \le 10^5.
  • 1≤s,t,r≤n1 \le s, t, r \le n, ox,oy∈{0,1}o_x, o_y \in \{0,1\}.

::cute-table{tuack}

Test point ID n,m≤n, m \le oxo_x oyo_y rr Special property
11 10510^5 ∈{0,1}\in \{0,1\} ≤n\le n A
22 ^ ^ =1= 1 B
3,43, 4 =0= 0 =2= 2 ^
5,65, 6 1010 ^ ^ ≤n\le n
7,87, 8 2 0002\,000 ^
9∼119 \sim 11 10510^5
12∼1412 \sim 14 ^ =1= 1 None
15∼1715 \sim 17 =1= 1 =0= 0 ^
18,1918, 19 ^ =1= 1 B
20,2120, 21 5×1045 \times 10^4 ^ None
22∼2422 \sim 24 10510^5 ^
2525 ^ ∈{0,1}\in \{0,1\}

Special property A: There exists 1≤x≤n1 \le x \le n such that for all 1≤i≤n−11 \le i \le n - 1, we have ui=xu_i = x or vi=xv_i = x.

Special property B: The unrooted tree formed by all devices is generated uniformly at random among all labeled unrooted trees on nn nodes.

Translated by ChatGPT 5