QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#108442 | #6396. Puzzle: Kusabi | berarchegas# | WA | 2ms | 3488kb | C++17 | 7.5kb | 2023-05-25 01:29:28 | 2023-05-25 01:29:32 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
mt19937 rng((int) chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 5;
const int INF = 1e9;
struct dinic {
const bool scaling = false;
int lim;
struct edge {
int to, cap, rev, flow;
bool res;
edge(int to_, int cap_, int rev_, bool res_)
: to(to_), cap(cap_), rev(rev_), flow(0), res(res_) {}
};
vector<vector<edge>> g;
vector<int> lev, beg;
ll F;
dinic(int n) : g(n), F(0) {}
void add(int a, int b, int c) {
g[a].emplace_back(b, c, g[b].size(), false);
g[b].emplace_back(a, 0, g[a].size()-1, true);
}
bool bfs(int s, int t) {
lev = vector<int>(g.size(), -1); lev[s] = 0;
beg = vector<int>(g.size(), 0);
queue<int> q; q.push(s);
while (q.size()) {
int u = q.front(); q.pop();
for (auto& i : g[u]) {
if (lev[i.to] != -1 or (i.flow == i.cap)) continue;
if (scaling and i.cap - i.flow < lim) continue;
lev[i.to] = lev[u] + 1;
q.push(i.to);
}
}
return lev[t] != -1;
}
int dfs(int v, int s, int f = INF) {
if (!f or v == s) return f;
for (int& i = beg[v]; i < g[v].size(); i++) {
auto& e = g[v][i];
if (lev[e.to] != lev[v] + 1) continue;
int foi = dfs(e.to, s, min(f, e.cap - e.flow));
if (!foi) continue;
e.flow += foi, g[e.to][e.rev].flow -= foi;
return foi;
}
return 0;
}
ll max_flow(int s, int t) {
for (lim = scaling ? (1<<30) : 1; lim; lim /= 2)
while (bfs(s, t)) while (int ff = dfs(s, t)) F += ff;
return F;
}
// arestas com fluxo
vector<pii> flow_edges(int s, int t) {
max_flow(s, t);
vector<pii> ans;
int n = g.size();
for (int i = 0; i < n; i++) {
for (auto edge : g[i]) {
if (!edge.res && edge.flow)
ans.emplace_back(i, edge.to);
}
}
return ans;
}
};
int v[155][155], n, m, dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0}, qtd[155][155];
char ans[155][155];
int coorToId(int x, int y) {
return m * x + y;
}
pii idToCoord(int id) {
return {id / m, id % m};
}
bool valid(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m;
}
int main () {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
char c;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> c;
v[i][j] = c - '0';
if (j < m - 1)
cin >> c;
}
if (i < n - 1)
for (int j = 0; j < 2 * m - 1; j++) cin >> c;
}
dinic g(n * m + 2);
int source = n * m, sink = n * m + 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if ((i == 0 || i == n - 1) && (j == 0 || j == m - 1)) {
// corner
if (v[i][j] != 2) continue;
if ((i + j) % 2 == 0) {
g.add(source, coorToId(i, j), 1);
}
else {
g.add(coorToId(i, j), sink, 1);
}
for (int k = 0; k < 4; k++) {
int nx = i + dx[k], ny = j + dy[k];
if (valid(nx, ny) && (v[nx][ny] == 2 || v[nx][ny] == 4)) {
if ((i + j) % 2 == 0) {
g.add(coorToId(i, j), coorToId(nx, ny), 1);
}
else {
g.add(coorToId(nx, ny), coorToId(i, j), 1);
}
}
}
}
else if (i == 0 || i == n - 1 || j == 0 || j == m - 1) {
// borda
if (v[i][j] != 4) continue;
if ((i + j) % 2 == 0) {
g.add(source, coorToId(i, j), 1);
}
else {
g.add(coorToId(i, j), sink, 1);
}
for (int k = 0; k < 4; k++) {
int nx = i + dx[k], ny = j + dy[k];
if (valid(nx, ny) && (v[nx][ny] == 2 || v[nx][ny] == 4)) {
if (v[nx][ny] == 4 && ((nx != i && (nx == 0 || nx == n - 1)) || (ny != j && (ny == 0 || ny == m - 1)))) continue;
if ((i + j) % 2 == 0) {
g.add(coorToId(i, j), coorToId(nx, ny), 1);
}
else {
g.add(coorToId(nx, ny), coorToId(i, j), 1);
}
}
}
}
else {
// resto
if (v[i][j] != 7) continue;
if ((i + j) % 2 == 0) {
g.add(source, coorToId(i, j), 1);
}
else {
g.add(coorToId(i, j), sink, 1);
}
for (int k = 0; k < 4; k++) {
int nx = i + dx[k], ny = j + dy[k];
if (valid(nx, ny) && v[nx][ny] == 7) {
if ((i + j) % 2 == 0) {
g.add(coorToId(i, j), coorToId(nx, ny), 1);
}
else {
g.add(coorToId(nx, ny), coorToId(i, j), 1);
}
}
}
}
}
}
vector<pii> edges = g.flow_edges(source, sink);
for (pii x : edges) {
qtd[idToCoord(x.first).first][idToCoord(x.first).second]++;
qtd[idToCoord(x.second).first][idToCoord(x.second).second]++;
}
bool ok = true;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if ((i == 0 || i == n - 1) && (j == 0 || j == m - 1)) {
// corner
if (v[i][j] != 2) continue;
ok &= qtd[i][j] == 2;
}
else if (i == 0 || i == n - 1 || j == 0 || j == m - 1) {
// borda
if (v[i][j] != 4) continue;
ok &= qtd[i][j] == 2;
}
else {
// resto
if (v[i][j] != 7) continue;
ok &= qtd[i][j] == 2;
}
}
}
if (!ok) cout << "NO\n";
else {
cout << "YES\n";
for (int i = 0; i < 2 * n - 1; i++) {
for (int j = 0; j < 2 * m - 1; j++) {
ans[i][j] = '#';
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ans[2 * i][2 * j] = v[i][j] + '0';
}
}
for (pii x : edges) {
if (x.first == source || x.second == source || x.first == sink || x.second == sink) continue;
pii p = idToCoord(x.first);
pii s = idToCoord(x.second);
if (s.first == p.first + 1) {
ans[2 * p.first + 1][2 * p.second] = '.';
}
else if (s.first == p.first - 1) {
ans[2 * p.first - 1][2 * p.second] = '.';
}
else if (s.second == p.second + 1) {
ans[2 * p.first][2 * p.second + 1] = '.';
}
else if (s.second == p.second - 1) {
ans[2 * p.first][2 * p.second - 1] = '.';
}
}
for (int i = 0; i < 2 * n - 1; i++) {
for (int j = 0; j < 2 * m - 1; j++) cout << ans[i][j];
cout << '\n';
}
}
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 3488kb
input:
8 2 1 - 3 1 - 4 2 Tong 5 2 Tong 6 3 Duan 7 3 - 8 7 Chang
output:
YES 1#3 ### 2#o ### 2#o ### 3#u ### 3#8 ### a#g ### g#g ### g#g
result:
wrong output format Expected integer, but "1#3" found