結果

問題 No.100 直列あみだくじ
ユーザー codershifthcodershifth
提出日時 2015-09-01 23:24:28
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,861 bytes
コンパイル時間 2,504 ms
コンパイル使用メモリ 152,076 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-25 21:50:05
合計ジャッジ時間 5,734 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 WA -
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 2 ms
4,384 KB
testcase_38 AC 1 ms
4,384 KB
testcase_39 AC 2 ms
4,384 KB
testcase_40 WA -
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 WA -
testcase_44 AC 2 ms
4,376 KB
testcase_45 AC 1 ms
4,380 KB
testcase_46 AC 2 ms
4,376 KB
testcase_47 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In member function ‘bool SeriesAmidakuji::dfs(std::vector<int>&, const std::vector<bool>&, const std::vector<int>&)’:
main.cpp:25:17: warning: ‘beg’ may be used uninitialized in this function [-Wmaybe-uninitialized]
             int beg;
                 ^~~
main.cpp: In member function ‘void SeriesAmidakuji::solve()’:
main.cpp:25:17: warning: ‘beg’ may be used uninitialized in this function [-Wmaybe-uninitialized]

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;


class SeriesAmidakuji {
public:
    bool dfs(vector<int> &s, const vector<bool> &used, const vector<int> &A) {
            int N = s.size();
            int n = count_if(RANGE(s), [&](int v){return (v>=0);});
            if (n==N)
            {
                REP(i,N) // 整合性チェック
                    if (A[i] != s[s[i]])
                        return false;
                return true;
            }
            int beg;
            REP(i,N)
            {
                if (s[i] < 0)
                {
                    beg = i;
                    break;
                }
            }
            // O(N^2)
            REP(i,N)
            {
                if (used[i])
                    continue;
                vector<bool> w(used);
                bool ok = true;

                // ii -> i に決め打ちしてためしてみる
                if (beg == i) // s[beg] = beg のとき
                {
                    s[beg] = i;
                    if (s[s[beg]] != A[beg])
                        continue;
                    w[i] = true;
                }
                else
                {
                    int cur = beg;
                    s[cur]  = i;
                    w[i] = true;
                    do {
                        if (s[cur] == A[cur])
                        {
                            ok = false;
                            break;
                        }
                        s[s[cur]] = A[cur];
                        w[A[cur]] = true;
                        cur = s[cur];
                    } while (cur != beg);
                }
                if (ok && dfs(s, w, A))
                    return true;
            }
            return false;
    }
    void solve(void) {
            int N;
            cin>>N;
            vector<int> A(N);
            REP(i,N)
            {
                cin>>A[i];
                --A[i];
            }

            // 1,...,N の置換 s のうち
            // s^2 = A なるものを見つける
            //
            // 置換は部分置換の直積として表現できる。
            // s = s1*s2*...*sr
            // 部分置換の個数は最大 N なので全探索でできる。
            //
            vector<int> s(N,-1);
            vector<bool> used(N,false);
            if (dfs(s, used, A))
                cout<<"Yes"<<endl;
            else
                cout<<"No"<<endl;
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new SeriesAmidakuji();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0