結果

問題 No.3539 Parentheses Square
コンテスト
ユーザー nauclhlt
提出日時 2026-03-04 11:50:18
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
RE  
実行時間 -
コード長 2,217 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,205 ms
コンパイル使用メモリ 231,904 KB
実行使用メモリ 7,976 KB
最終ジャッジ日時 2026-05-08 20:50:47
合計ジャッジ時間 11,377 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 10 RE * 31
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <atcoder/maxflow>

using namespace std;

using ll = long long;

bool check(string candidate, string t)
{
    for (int i = 0; i < candidate.size(); i++)
    {
        if (t[i] != '.' && candidate[i] != t[i]) return false;
    }

    return true;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    cin >> N;
    assert(1 <= N && N <= 16);
    vector<string> T(N);
    for (int i = 0; i < N; i++)
    {
        cin >> T[i];
        assert(T[i].size() == N);
    }

    if (N % 2 == 1)
    {
        cout << "-1" << endl;
        return 0;
    }

    vector<string> candidates;

    auto f = [&](auto self, string body, int depth) -> void
    {
        if (body.size() == N)
        {
            if (depth == 0)
                candidates.push_back(body);
        }
        else
        {
            if (depth > 0)
                self(self, body + ")", depth - 1);
            if (depth < N / 2)
                self(self, body + "(", depth + 1);
        }
    };

    f(f, "", 0);

    if (candidates.size() < N)
    {
        cout << "-1" << endl;
        return 0;
    }

    int C = candidates.size();
    int source = 0;
    int sink = 1;
    int candHead = 2;
    int tHead = candHead + C;

    atcoder::mf_graph<int> g(2 + C + N);
    for (int i = 0; i < C; i++)
    {
        g.add_edge(source, candHead + i, 1);
    }
    for (int i = 0; i < N; i++)
    {
        g.add_edge(tHead + i, sink, 1);
    }

    for (int i = 0; i < C; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (check(candidates[i], T[j]))
            {
                g.add_edge(candHead + i, tHead + j, 1);
            }
        }
    }

    int maxflow = g.flow(source, sink);
    if (maxflow != N)
    {
        cout << "-1" << endl;
        return 0;
    }

    auto edges = g.edges();

    vector<string> S(N);

    for (int i = 0; i < edges.size(); i++)
    {
        if (edges[i].from == source || edges[i].to == sink) continue;

        if (edges[i].flow == 1)
        {
            S[edges[i].to - tHead] = candidates[edges[i].from - candHead];
        }
    }

    for (int i = 0; i < N; i++)
    {
        cout << S[i] << endl;
    }
}
0