QOJ.ac

QOJ

Limite de temps : 3 s Limite de mémoire : 512 MB Points totaux : 100 Hackable ✓

#6274. Traveling the World

Statistiques

You are given an undirected simple connected graph with $n$ vertices and $m-1$ edges. The weights of these edges are a permutation of $2, 3, \dots, m$.

The weight of a path is defined as the product of the weights of the edges on the path. What is the maximum possible weight of a path from $s$ to $t$ such that the weight does not exceed $m$? Note that paths are allowed to revisit vertices or edges.

You need to answer multiple queries.

Input

The first line contains three positive integers $n, m, q$.

The next $m-1$ lines each contain two positive integers $u, v$, representing the two vertices connected by the edge with weight $2, 3, \dots, m$ respectively.

The next $q$ lines each contain two positive integers $s, t$, representing a query.

Output

Output $q$ lines, each containing one number. If there exists a path with a weight not exceeding $m$, output the maximum such weight; otherwise, output $-1$.

Examples

Input 1

4 5 10
1 2
2 3
3 4
4 1
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4

Output 1

4
2
-1
5
4
3
-1
1
4
1

Subtasks

For $100\%$ of the data, it is guaranteed that $2 \leq n \leq m \leq 5 \times 10^5$ and $1 \leq q \leq 5 \times 10^5$.

For data point $1$, it is guaranteed that $m \leq 10$.

For data point $2$, it is guaranteed that $m \leq 100$.

For data points $3 \sim 4$, it is guaranteed that $m \leq 10^3$.

For data points $5 \sim 6$, it is guaranteed that $m \leq 10^4$.

For data points $7 \sim 9$, it is guaranteed that $m \leq 10^5$.

For data point $10$, there are no special restrictions.

Note

The input and output volume for this problem is large, so you may need to use I/O optimization. A template is provided below; note the location indicated in the comments for enabling file I/O:

using u32 = unsigned;
struct IO_Tp
{
    const static int _I_Buffer_Size = 30 << 20;
    char _I_Buffer[_I_Buffer_Size], *_I_pos = _I_Buffer;

    const static int _O_Buffer_Size = 8 << 20;
    char _O_Buffer[_O_Buffer_Size], *_O_pos = _O_Buffer;

    u32 m[10000];

    IO_Tp()
    {
//        freopen("tour.in", "r", stdin);
//        freopen("tour.out", "w", stdout);
        constexpr u32 e0 = '\0\0\0\1', e1 = '\0\0\1\0', e2 = '\0\1\0\0', e3 = '\1\0\0\0';
        int x = 0;
        for (u32 i = 0, c0 = '0000'; i != 10; ++i, c0 += e0)
            for (u32 j = 0, c1 = c0; j != 10; ++j, c1 += e1)
                for (u32 k = 0, c2 = c1; k != 10; ++k, c2 += e2)
                    for (u32 l = 0, c3 = c2; l != 10; ++l, c3 += e3)
                        m[x++] = c3;

        fread(_I_Buffer, 1, _I_Buffer_Size, stdin);
    }
    ~IO_Tp() { fwrite(_O_Buffer, 1, _O_pos - _O_Buffer, stdout); }

    IO_Tp &operator>>(int &res)
    {
        while (!isdigit(*_I_pos))
            ++_I_pos;
        res = *_I_pos++ - '0';
        while (isdigit(*_I_pos))
            res = res * 10 + (*_I_pos++ - '0');
        return *this;
    }

    IO_Tp &operator<<(int x)
    {
        if (x == -1)
        {
            *_O_pos++ = '-';
            *_O_pos++ = '1';
            return *this;
        }
        if (x == 0)
        {
            *_O_pos++ = '0';
            return *this; 
        }
        static char _buf[35];
        char *_pos = _buf + 35;
        while (x >= 10000)
            *--reinterpret_cast<u32 *&>(_pos) = m[x % 10000], x /= 10000;
        *--reinterpret_cast<u32 *&>(_pos) = m[x];
        _pos += (x < 1000) + (x < 100) + (x < 10);
        _O_pos = std::copy(_pos, _buf + 35, _O_pos);
        return *this;
    }

    IO_Tp &operator<<(char ch)
    {
        *_O_pos++ = ch;
        return *this;
    }
} IO;

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.