QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#406592 | #6822. Bracket Query | james1BadCreeper | WA | 0ms | 3872kb | C++17 | 1.6kb | 2024-05-07 14:51:08 | 2024-05-07 14:51:10 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 3e3 + 5;
int n, q;
int fa[N], d[N], inq[N], cnt[N];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
vector<pair<int, int>> G[N];
vector<tuple<int, int, int>> chk;
int main(void) {
ios::sync_with_stdio(0);
cin >> n >> q;
for (int i = 1; i <= n; ++i) G[i].emplace_back(i - 1, 1), G[i - 1].emplace_back(i, 1);
for (int i = 1; i < n; ++i) G[i].emplace_back(0, 0);
G[0].emplace_back(n, 0); G[n].emplace_back(0, 0);
for (int i = 1; i <= n; ++i) fa[i] = i;
for (int i = 1; i <= q; ++i) {
int x, y, z; cin >> x >> y >> z;
if ((x + y + 1) % 2 != z % 2) return cout << "?", 0;
if (find(x - 1) != find(y))
fa[find(x - 1)] = find(y),
G[y].emplace_back(x - 1, -z),
G[x - 1].emplace_back(y, z);
else chk.emplace_back(x, y, z);
}
memset(d, 0x3f, sizeof d); queue<int> q;
d[0] = 0; inq[0] = 1; q.push(0);
while (!q.empty()) {
int u = q.front(); q.pop(); inq[u] = 0;
for (auto [v, w] : G[u]) if (d[v] > d[u] + w) {
d[v] = d[u] + w; cnt[v] = cnt[u] + 1;
if (cnt[v] > n + 2) return cout << "?\n", 0;
if (!inq[v]) q.push(v);
}
}
for (auto [x, y, z] : chk)
if (d[y] - d[x - 1] != z) return cout << "?\n", 0;
cout << "! ";
for (int i = 1; i <= n; ++i)
if (d[i] > d[i - 1]) cout << '(';
else cout << ')';
cout << "\n";
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3656kb
input:
4 1 1 2 0
output:
! ()()
result:
ok ok
Test #2:
score: 0
Accepted
time: 0ms
memory: 3872kb
input:
4 1 1 2 2
output:
! (())
result:
ok ok
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 3608kb
input:
2 2 1 1 1 2 2 -1
output:
?
result:
wrong answer participant reports no solution but jury has one