QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 1024 MB Total points: 100

#6252. Tree or Bush

Statistics

Sadegh and Mahan, influenced by the story of the Grinch and the holiday spirit, have decided to steal Amir's Christmas tree. They drove a van to Tehranpars and snuck into Amir's house. However, upon leaving, they encountered a puzzle that Amir had used to lock his tree. Mahan, who is an Olympiad student, quickly understood the puzzle and explained it to Sadegh as follows:

We have a tree with $n$ vertices, where each edge is labeled with a lowercase English letter. We are also given a secret string $s = s_1 s_2 \dots s_m$. We want to find a path in this tree (without repeating edges) such that by traversing it and concatenating the characters on the edges, we produce the secret string. In other words, we want to find a path with edges $e_{i_1}, e_{i_2}, \dots, e_{i_m}$ such that the string $c_{i_1} c_{i_2} \dots c_{i_m}$ is equal to $s_1 s_2 \dots s_m$, where $c_{i_j}$ is the character written on edge $e_{i_j}$.

You must find this path or declare that no such path with the specified property exists.

Input

The first line of input contains the integer $n$, representing the number of vertices in the tree. In each of the next $n-1$ lines, $u_i$, $v_i$, and $c_i$ are given, representing an edge between vertices $u_i$ and $v_i$ with the character $c_i$. It is guaranteed that $c_i$ is a lowercase English letter. It is also guaranteed that the given edges form a tree. The last line contains the string $s$, which consists only of lowercase English letters.

Output

In the only line of output, print two space-separated integers representing the starting vertex and the ending vertex of the path, respectively. If no such path exists, print "-1 -1".

Constraints

  • $2 \le n \le 10^5$
  • $1 \le u_i, v_i \le n$
  • $1 \le |s| \le n - 1$

Examples

Input 1

4
1 2 a
2 3 b
3 4 c
bc

Output 1

2 4

Input 2

4
1 2 a
2 3 b
3 4 c
ac

Output 2

-1 -1

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.