結果

問題 No.100 直列あみだくじ
ユーザー suisen
提出日時 2021-04-23 18:36:34
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,912 bytes
コンパイル時間 2,251 ms
コンパイル使用メモリ 198,148 KB
最終ジャッジ日時 2025-01-20 23:23:41
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using int128 = __int128_t;

#define rep(i, n)         for (int i = 0; i < n; ++i)
#define reps(i, n, s)     for (int i = 0; i < n; i += s)
#define repl(i, l, r)     for (int i = l; i < r; ++i)
#define repsl(i, l, r, s) for (int i = l; i < r; i += s)

#define all(iterable) (iterable).begin(), (iterable).end()

constexpr int dx4[4] = {1, 0, -1, 0};
constexpr int dy4[4] = {0, 1, 0, -1};

constexpr int dx8[8] = {1, 1, 0, -1, -1, -1, 0, 1};
constexpr int dy8[8] = {0, 1, 1, 1, 0, -1, -1, -1};

template <typename T>
void print(const vector<T>& vec, const string sep = " ", const string end = "\n") {
    int n = vec.size();
    rep(i, n) {
        cout << vec[i];
        if (i < n - 1) cout << sep;
        else cout << end;
    }
}

template <typename T>
void read(vector<T>& a, int begin_index, int length) {
    if (a.size() < begin_index + length) a.resize(begin_index + length);
    while (length --> 0) cin >> a[begin_index++];
}
template <typename T>
void read(vector<T>& a) { read(a, 0, a.size()); }

int main() {
    size_t n;
    cin >> n;
    vector<int> a(n);
    read(a);
    for (int& v : a) --v;
    vector<int> p(n, -1);
    rep(i, n) {
        if (p[i] >= 0) continue;
        rep(j, n) {
            vector<pair<int, int>> tmp;
            p[i] = j;
            tmp.push_back({i, j});
            bool ok = false;
            while (true) {
                auto [u, v] = tmp.back();
                if (p[v] < 0) {
                    p[v] = a[u];
                    tmp.push_back({v, a[u]});
                } else {
                    ok = p[v] == a[u];
                    break;
                }
            }
            if (ok) break;
            for (auto& [u, v] : tmp) {
                p[u] = -1;
            }
        }
        if (p[i] < 0) {
            cout << "No\n";
            return 0;
        }
    }
    cout << "Yes\n";
}
0