結果
| 問題 | No.3539 Parentheses Square |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-05-08 22:31:29 |
| 言語 | C++23 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 4,417 bytes |
| 記録 | |
| コンパイル時間 | 3,941 ms |
| コンパイル使用メモリ | 366,184 KB |
| 実行使用メモリ | 11,552 KB |
| 最終ジャッジ日時 | 2026-05-08 22:32:09 |
| 合計ジャッジ時間 | 10,104 ms |
|
ジャッジサーバーID (参考情報) |
judge2_1 / judge1_1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 6 TLE * 1 -- * 34 |
ソースコード
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,s,n) for (int i = (s); i < (n); ++i)
#define rrep(i,g,n) for (int i = (n)-1; i >= (g); --i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define len(x) (int)(x).size()
#define dup(x,y) (((x)+(y)-1)/(y))
#define pb push_back
#define eb emplace_back
#define Field(T) vector<vector<T>>
using namespace std;
using ll = long long;
using ull = unsigned long long;
template<typename T> using pq = priority_queue<T,vector<T>,greater<T>>;
using P = pair<int,int>;
template<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}
template <typename T>
struct MaxFlow {
struct edge {
int to;
T cap;
int rev;
bool is_rev;
};
vector<vector<edge>> G;
vector<int> dist, iter;
MaxFlow() = default;
explicit MaxFlow(int n) : G(n) {}
void add_edge(int from, int to, T cap = 1) {
G[from].emplace_back(edge{to, cap, (int)G[to].size(), 0});
G[to].emplace_back(edge{from, 0, (int)G[from].size()-1, 1});
}
void debug() {
for (int i = 0; i < (int)G.size(); ++i) {
for (edge &e : G[i]) {
if (e.is_rev) continue;
edge &rev_e = G[e.to][e.rev];
cout << i << " -> " << e.to << " : " << "(" << rev_e.cap << "/" << e.cap + rev_e.cap << ")" << endl;
}
}
}
void bfs(const int &s) {
dist.assign(G.size(), -1);
dist[s] = 0;
queue<int> que;
que.push(s);
while(!que.empty()) {
int v = que.front();
que.pop();
for (edge &e : G[v]) {
if (e.cap == 0 || dist[e.to] >= 0) continue;
dist[e.to] = dist[v] + 1;
que.push(e.to);
}
}
}
T dfs(int v, const int &t, T f) {
if (v == t) return f;
T ret = 0;
for (int& i = iter[v]; i < (int)G[v].size(); ++i) {
edge& e = G[v][i];
if (e.cap == 0 || dist[v] >= dist[e.to]) continue;
T d = dfs(e.to, t, min(f - ret, e.cap));
if (d == 0) continue;
e.cap -= d;
G[e.to][e.rev].cap += d;
ret += d;
if (f == ret) break;
}
return ret;
}
T flow(int s, int t, T f_limit = numeric_limits<T>::max()) {
T ret = 0;
queue<int> que;
while(true) {
bfs(s);
if (dist[t] == -1) break;
iter.assign(G.size(), 0);
while(ret < f_limit) {
T f = dfs(s, t, f_limit - ret);
if (f == 0) break;
ret += f;
}
}
return ret;
}
vector<bool> min_cut(int s) {
vector<bool> used(G.size(), 0);
used[s] = 1;
queue<int> que;
que.push(s);
while(!que.empty()) {
int v = que.front();
que.pop();
for (edge &e : G[v]) {
if (e.cap > 0 && !used[e.to]) {
used[e.to] = 1;
que.push(e.to);
}
}
}
return used;
}
};
int main() {
int n;
cin >> n;
vector<string> t(n);
rep(i,0,n) cin >> t[i];
function<void(vector<string>&, string&, string&, int, int, int)> f = [&](vector<string> &v, string &s, string &t, int idx, int val, int cnt) {
if (len(v) > n+1) return;
if (idx == n) {
v.eb(s);
return;
}
if (t[idx] == '(' || t[idx] == '.') {
if (cnt < n/2) {
s[idx] = '(';
f(v, s, t, idx+1, val+1, cnt+1);
s[idx] = '.';
}
}
if (t[idx] == ')' || t[idx] == '.') {
if (idx-cnt < n/2 && val > 0) {
s[idx] = ')';
f(v, s, t, idx+1, val-1, cnt);
s[idx] = '.';
}
}
};
vector<vector<string>> u(n);
string s(n, '.');
rep(i,0,n) f(u[i], s, t[i], 0, 0, 0);
// rep(i,0,n) {
// for (string v : u[i]) cout << v << endl;
// cout << endl;
// }
vector<string> x;
map<string,int> mp;
rep(i,0,n) {
for (string &v : u[i]) {
x.eb(v);
}
}
sort(all(x));
x.erase(unique(all(x)), x.end());
rep(i,0,len(x)) mp[x[i]] = i;
// for (auto [v, cnt] : mp) {
// cout << v << " " << cnt << endl;
// }
MaxFlow<int> mf(n+len(mp)+2);
rep(i,0,n) {
mf.add_edge(n+len(mp), i);
for (string &v : u[i]) {
mf.add_edge(i, n+mp[v], 1);
}
}
rep(j,0,len(mp)) mf.add_edge(n+j, n+len(mp)+1);
if (mf.flow(n+len(mp), n+len(mp)+1) < n) {
cout << -1 << endl;
return 0;
}
rep(i,0,n) {
for (auto &e : mf.G[i]) {
if (e.is_rev) continue;
if (e.cap == 0) {
cout << x[e.to-n] << endl;
}
}
}
return 0;
}