結果

問題 No.3539 Parentheses Square
コンテスト
ユーザー V_Melville
提出日時 2026-05-08 23:34:03
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
TLE  
実行時間 -
コード長 2,631 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,098 ms
コンパイル使用メモリ 362,260 KB
実行使用メモリ 9,984 KB
最終ジャッジ日時 2026-05-08 23:34:30
合計ジャッジ時間 7,436 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 6 TLE * 1 -- * 34
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>

using namespace std;

int N;
string T[205];
vector<string> completions[205];

void dfs(int id, int depth, int balance, string& cur) {
    if (completions[id].size() == (size_t)N) return;
    
    if (balance < 0 || balance > N - depth) return;
    
    if (depth == N) {
        if (balance == 0) {
            completions[id].push_back(cur);
        }
        return;
    }
    
    char c = T[id][depth];
    
    if (c == '(' || c == '.') {
        cur.push_back('(');
        dfs(id, depth + 1, balance + 1, cur);
        cur.pop_back();
    }
    
    if (completions[id].size() == (size_t)N) return;
   
    if (c == ')' || c == '.') {
        cur.push_back(')');
        dfs(id, depth + 1, balance - 1, cur);
        cur.pop_back();
    }
}

bool dfs_match(int u, vector<int>& vis, vector<int>& match_right, const vector<vector<int>>& adj) {
    for (int v : adj[u]) {
        if (vis[v]) continue;
        vis[v] = 1;
        if (match_right[v] == -1 || dfs_match(match_right[v], vis, match_right, adj)) {
            match_right[v] = u;
            return true;
        }
    }
    return false;
}

int main() {
    cin >> N;
    
    for (int i = 0; i < N; ++i) {
        cin >> T[i];
    }
    
    if (N % 2 != 0) {
        cout << -1 << "\n";
        return 0;
    }
    
    for (int i = 0; i < N; ++i) {
        string cur = "";
        dfs(i, 0, 0, cur);
    }
    
    vector<string> all_strings;
    for (int i = 0; i < N; ++i) {
        for (const string& s : completions[i]) {
            all_strings.push_back(s);
        }
    }
    
    sort(all_strings.begin(), all_strings.end());
    all_strings.erase(unique(all_strings.begin(), all_strings.end()), all_strings.end());
    
    int V = all_strings.size();
   
    vector<vector<int>> adj(N);
    for (int i = 0; i < N; ++i) {
        for (const string& s : completions[i]) {
            int u = lower_bound(all_strings.begin(), all_strings.end(), s) - all_strings.begin();
            adj[i].push_back(u);
        }
    }
    
    vector<int> match_right(V, -1);
    vector<int> match_left(N, -1);
    int matches = 0;
    
    for (int i = 0; i < N; ++i) {
        vector<int> vis(V, 0);
        if (dfs_match(i, vis, match_right, adj)) {
            matches++;
        }
    }
    
    if (matches < N) {
        cout << -1 << "\n";
    } else {
        for (int v = 0; v < V; ++v) {
            if (match_right[v] != -1) {
                match_left[match_right[v]] = v;
            }
        }
        for (int i = 0; i < N; ++i) {
            cout << all_strings[match_left[i]] << "\n";
        }
    }
    
    return 0;
}
0