#P15533. 【MYCOI R1】那友谊连成的树

    ID: 14921 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>倍增洛谷原创O2优化洛谷月赛

【MYCOI R1】那友谊连成的树

Description

In Xiao Che and Xiao Meng's school, there are nn students. They are divided into two types: introverted and extroverted.

There are some friendships among the students. Define uvu \leftrightarrow v to mean that student uu and student vv are good friends.

For two students x,yx, y, if there exists a sequence of students a1=x,a2,,ak=ya_1 = x, a_2, \ldots, a_k = y such that a1a2a_1 \leftrightarrow a_2, a2a3a_2 \leftrightarrow a_3, \ldots, ak1aka_{k-1} \leftrightarrow a_k, then k1k-1 is called a "spacing" between xx and yy. The "friend distance" d(x,y)d(x, y) is defined as the minimum "spacing" between xx and yy.

Thus, the "friend distance" between good friends is 11.

Upon entering school (the morning of the first day), the nn students, for various reasons, have formed n1n-1 pairs of good friends, and there exists a "friend distance" between any two students. In other words, if we regard the students as nodes on a tree and the good friend relationships as edges between nodes, the initial relationships among the nn students form a tree.

The students enjoy making friends. Each day, for any three students a,b,ca, b, c, if aa and bb are both "extroverted", and on the morning of that day aa and bb are good friends, and bb and cc are good friends, then at noon that day aa and cc will become good friends (note that cc can be "introverted"). As a result, the "friend distance" between students will gradually decrease.

However, as time goes on, the pressure of academic courses increases, and students spend more time studying rather than socializing. Therefore, on the evening of day tt, the "social cost" between xx and yy is d(x,y)+td(x, y) + t.

Xiao Che and Xiao Meng want to know, if they are students uu and vv, what is the minimum "social cost" among the TT evenings from day 11 to day TT?

Input Format

The first line contains two positive integers n,qn, q, representing the number of students and the number of queries.

The second line contains nn characters. The ii-th character represents the personality of student ii (I for introverted, E for extroverted).

The next n1n-1 lines each contain two positive integers u,vu, v, indicating that uu and vv are good friends on the morning of the first day.

The last qq lines each contain three positive integers u,v,Tu, v, T, as described above.

Output Format

For each query, output a line containing a positive integer representing the answer.

7 3
IEEIEIE
1 2
4 6
5 7
1 3
3 7
3 4
4 5 1
4 5 3
2 5 10
3
3
4

Hint

This problem uses bundled testing.

::cute-table{tuack}

Subtask Special Properties Points
Subtask 1 n,q,T1145n, q, T \le 1145 30
Subtask 2 Initially, exactly 22 students have exactly 11 good friend, and the remaining n2n-2 students have exactly 22 good friends. 5
Subtask 3 Initially, there exists a student who has n1n-1 "good friends". 10
Subtask 4 All students are extroverted. 15
Subtask 5 T1T \le 1
Subtask 6 No special properties 25

For 100%100\% of the data: 3n1053 \le n \le 10^5, 1q1051 \le q \le 10^5, 1u,vn1 \le u, v \le n, 1T1051 \le T \le 10^5.