Processing math: 100%
Ancestor - MarisaOJ: Marisa Online Judge

Ancestor

Time limit: 1000 ms
Memory limit: 256 MB
Given a tree with n vertices rooted at 1. There are q queries, each consisting of two vertices u and v. Determine whether u is an ancestor of v, v is an ancestor of u, or neither. Define ancestor: Let pi be the immediate parent of i in the tree. We have pu as the ancestor of u, ppu as the ancestor of u, pppu as the ancestor of u, and so on. In other words, the parent of u is the ancestor of u, the parent of the parent of u is the ancestor of u, and so on. ### Input - The first line contains two integers n and q. - The next n−1 lines each contain two integers u and v, indicating there is an edge between u and v. - The next q lines each contain two integers u and v, representing a query. Ensure u≠v. ### Output - For each query, print: + MA if u is an ancestor of v. + RI if v is an ancestor of u. + SA otherwise. ### Constraints - 1≤n,q≤105. - 1≤u,v≤n. ### Example Input: 663445563251564362513564 Output: MAMASARIRISA