結果

問題 No.3539 Parentheses Square
コンテスト
ユーザー nauclhlt
提出日時 2026-03-04 11:48:31
言語 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
結果
MLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,152 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,439 ms
コンパイル使用メモリ 229,284 KB
実行使用メモリ 697,288 KB
最終ジャッジ日時 2026-05-08 20:50:50
合計ジャッジ時間 6,480 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 6 MLE * 1 -- * 34
権限があれば一括ダウンロードができます

ソースコード

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;
    vector<string> T(N);
    for (int i = 0; i < N; i++)
    {
        cin >> T[i];
    }

    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