QOJ.ac

QOJ

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

#6274. 世界一周

Statistiques

$n$ 個の頂点と $m-1$ 本の辺からなる無向単純連結グラフが与えられます。これらの辺の重みは $2$ から $m$ までの置換となっています。

あるパスの重みを、そのパスに含まれる辺の重みの積と定義します。$s$ から $t$ へのパスの重みとして、 $m$ を超えない範囲で最大でいくつが可能か求めてください。 なお、パスは同じ頂点や同じ辺を複数回通っても構いません。

複数のクエリに答えてください。

入力

1 行目に 3 つの正整数 $n, m, q$ が与えられます。

続く $m-1$ 行の各行には、2 つの正整数 $u, v$ が与えられます。これは、重みが $2, 3, \dots, m$ である辺が頂点 $u$ と $v$ を結んでいることを表します。

続く $q$ 行の各行には、2 つの正整数 $s, t$ が与えられ、1 つのクエリを表します。

出力

$q$ 行に出力してください。各行には、重みが $m$ を超えないパスが存在する場合はその最大値を、存在しない場合は $-1$ を出力してください。

入出力例

入力 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

出力 1

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

小課題

すべてのデータセットにおいて、$2 \leq n \leq m \leq 5 \times 10^5$、$1 \leq q \leq 5 \times 10^5$ を満たします。

  • データセット 1: $m \leq 10$ を満たす。
  • データセット 2: $m \leq 100$ を満たす。
  • データセット 3〜4: $m \leq 10^3$ を満たす。
  • データセット 5〜6: $m \leq 10^4$ を満たす。
  • データセット 7〜9: $m \leq 10^5$ を満たす。
  • データセット 10: 特殊な制限はない。

注記

本問題は入出力の規模が大きいため、IO 最適化が必要になる場合があります。以下にテンプレートを提供します。コメント部分はファイル IO を有効にするためのものです。

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.