結果

問題 No.1647 Travel in Mitaru city 2
ユーザー trineutrontrineutron
提出日時 2021-08-13 22:45:48
言語 C++23(draft)
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 161 ms / 2,500 ms
コード長 1,902 bytes
コンパイル時間 3,901 ms
コンパイル使用メモリ 285,128 KB
実行使用メモリ 34,516 KB
最終ジャッジ日時 2024-04-21 04:55:33
合計ジャッジ時間 12,622 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 4 ms
5,376 KB
testcase_08 AC 149 ms
31,476 KB
testcase_09 AC 151 ms
28,496 KB
testcase_10 AC 157 ms
31,644 KB
testcase_11 AC 120 ms
24,424 KB
testcase_12 AC 126 ms
23,384 KB
testcase_13 AC 126 ms
25,464 KB
testcase_14 AC 155 ms
32,320 KB
testcase_15 AC 159 ms
31,980 KB
testcase_16 AC 145 ms
30,504 KB
testcase_17 AC 125 ms
25,884 KB
testcase_18 AC 153 ms
24,732 KB
testcase_19 AC 154 ms
32,688 KB
testcase_20 AC 145 ms
29,560 KB
testcase_21 AC 147 ms
33,380 KB
testcase_22 AC 151 ms
33,040 KB
testcase_23 AC 161 ms
33,196 KB
testcase_24 AC 148 ms
29,848 KB
testcase_25 AC 154 ms
30,876 KB
testcase_26 AC 158 ms
33,316 KB
testcase_27 AC 158 ms
33,468 KB
testcase_28 AC 159 ms
34,384 KB
testcase_29 AC 150 ms
34,384 KB
testcase_30 AC 152 ms
32,028 KB
testcase_31 AC 158 ms
34,516 KB
testcase_32 AC 160 ms
34,284 KB
testcase_33 AC 140 ms
30,396 KB
testcase_34 AC 157 ms
34,512 KB
testcase_35 AC 145 ms
30,700 KB
testcase_36 AC 148 ms
34,384 KB
testcase_37 AC 153 ms
34,388 KB
testcase_38 AC 134 ms
27,172 KB
testcase_39 AC 123 ms
25,036 KB
testcase_40 AC 153 ms
26,924 KB
testcase_41 AC 129 ms
24,404 KB
testcase_42 AC 131 ms
27,124 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 8 ms
9,140 KB
testcase_45 AC 108 ms
19,616 KB
testcase_46 AC 142 ms
17,372 KB
testcase_47 AC 117 ms
19,620 KB
testcase_48 AC 129 ms
18,736 KB
testcase_49 AC 110 ms
19,740 KB
testcase_50 AC 113 ms
19,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using graph = vector<vector<int>>;

vector<int> cycle;

void dfs(const graph &to, vector<bool> &seen, int v, int p)
{
    seen.at(v) = true;
    for (auto &&c : to.at(v))
    {
        if (not cycle.empty())
        {
            break;
        }
        if (c == p)
        {
            continue;
        }
        if (seen.at(c))
        {
            cycle.push_back(c);
            break;
        }
        dfs(to, seen, c, v);
    }
    if (not cycle.empty())
    {
        cycle.push_back(v);
    }
}

int main()
{
    int h, w, n;
    cin >> h >> w >> n;
    graph to(h + w + n);
    for (int i = 0; i < n; i++)
    {
        int x, y;
        cin >> x >> y;
        x--;
        y--;
        to.at(x).push_back(h + w + i);
        to.at(h + w + i).push_back(x);
        to.at(y + h).push_back(h + w + i);
        to.at(h + w + i).push_back(y + h);
    }

    vector<bool> seen(h + w + n);
    for (int i = 0; i < h + w + n; i++)
    {
        if (seen.at(i))
        {
            continue;
        }
        dfs(to, seen, i, -1);
    }

    if (cycle.empty())
    {
        cout << -1 << endl;
        return 0;
    }

    vector<int> ans;
    int len = cycle.size(), shift = -1;
    for (int i = 0; i < len; i++)
    {
        if (i and cycle.at(i) == cycle.at(0))
        {
            break;
        }
        if (cycle.at(i) >= h + w)
        {
            ans.push_back(cycle.at(i) - h - w);
        }
        else if (shift == -1)
        {
            shift = (ans.size() + (cycle.at(i) >= h)) % 2;
        }
    }
    rotate(ans.begin(), ans.begin() + shift, ans.end());
    int k = ans.size();
    cout << k << endl;
    for (int i = 0; i < k; i++)
    {
        cout << ans.at(i) + 1;
        if (i < k - 1)
        {
            cout << ' ';
        }
        else
        {
            cout << endl;
        }
    }

    return 0;
}
0