QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#346124 | #3201. Cairo Corridor | PetroTarnavskyi# | AC ✓ | 21ms | 18756kb | C++20 | 4.8kb | 2024-03-07 20:53:59 | 2024-03-07 20:54:00 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
const int N = 274;
const int N2 = N * N * 2;
int c[N2];
VI g[N2];
bool used[N2];
int val[N2];
int tin[N2];
int low[N2];
int par[N2];
int con[N2];
int T = 0;
int n, m;
int dfs(int v)
{
used[v] = true;
int res = val[v];
for (auto to : g[v])
{
if (c[to] == 0 && !used[to])
res |= dfs(to);
}
return res;
}
void dfs2(int v, int p = -1)
{
used[v] = 1;
par[v] = p;
low[v] = tin[v] = T++;
int cnt = 0;
con[v] = 0;
for (auto to : g[v])
{
if (to == p)
continue;
if (c[to] == 1)
continue;
if (!used[to])
{
cnt++;
dfs2(to, v);
low[v] = min(low[v], low[to]);
if ((par[v] == -1 && cnt > 1) ||
(par[v] != -1 && low[to] >= tin[v]))
con[v] = 1;
}
else
{
low[v] = min(low[v], tin[to]);
}
}
}
void clear()
{
FOR (i, 0, n * m)
{
val[i] = 0;
c[i] = 0;
used[i] = false;
g[i].clear();
low[i] = 0;
tin[i] = 0;
par[i] = 0;
con[i] = 0;
}
T = 0;
}
void solve()
{
cin >> n >> m;
FOR (i, 0, n)
{
string s;
cin >> s;
FOR (j, 0, m)
{
c[i * 2 * m + j * 2] = s[j * 2] - '0';
c[i * 2 * m + j * 2 + 1] = s[j * 2 + 1] - '0';
}
}
// v = i * m + j * 2 + (0 ? 1)
FOR (i, 0, n)
{
FOR (j, 0, m)
{
if ((i + j) % 2 == 0)
{
int v1 = i * 2 * m + j * 2;
int v2 = i * 2 * m + j * 2 + 1;
g[v1].PB(v2);
g[v2].PB(v1);
if (i)
{
int u = (i - 1) * 2 * m + j * 2 + 1;
g[v1].PB(u);
g[v2].PB(u);
g[u].PB(v1);
g[u].PB(v2);
}
if (i + 1 < n)
{
int u = (i + 1) * 2 * m + j * 2;
g[v1].PB(u);
g[v2].PB(u);
g[u].PB(v1);
g[u].PB(v2);
}
}
else
{
int v1 = i * 2 * m + j * 2;
int v2 = i * 2 * m + j * 2 + 1;
g[v1].PB(v2);
g[v2].PB(v1);
if (j)
{
int u = i * 2 * m + (j - 1) * 2 + 1;
g[v1].PB(u);
g[v2].PB(u);
g[u].PB(v1);
g[u].PB(v2);
}
if (j + 1 < m)
{
int u = i * 2 * m + (j + 1) * 2;
g[v1].PB(u);
g[v2].PB(u);
g[u].PB(v1);
g[u].PB(v2);
}
}
}
}
// top
FOR (j, 0, m)
{
if (j & 1)
val[j * 2] |= 1;
else
{
val[j * 2] |= 1;
val[j * 2 + 1] |= 1;
}
}
// left
FOR (i, 0, n)
{
if (i & 1)
{
val[i * 2 * m] |= 2;
val[i * 2 * m + 1] |= 2;
}
else
val[i * 2 * m] |= 2;
}
// bottom
int rws = (n + 1) & 1;
FOR (j, 0, m)
{
if ((j & 1) ^ rws)
val[(n - 1) * 2 * m + j * 2 + 1] |= 4;
else
{
val[(n - 1) * 2 * m + j * 2] |= 4;
val[(n - 1) * 2 * m + j * 2 + 1] |= 4;
}
}
// right
int cls = (m + 1) & 1;
FOR (i, 0, n)
{
if ((i & 1) ^ cls)
{
val[i * 2 * m + (m - 1) * 2] |= 8;
val[i * 2 * m + (m - 1) * 2 + 1] |= 8;
}
else
val[i * 2 * m + (m - 1) * 2 + 1] |= 8;
}
m *= 2;
int v = -1;
FOR (i, 0, n * m)
{
if (!used[i] && c[i] == 0)
{
int r = dfs(i);
if (r == 15)
{
v = i;
break;
}
}
}
if (v == -1)
{
cout << "NO MINIMAL CORRIDOR\n";
clear();
return;
}
fill(used, used + n * m, false);
//cerr << v << '\n';
//clear();
//return;
dfs(v);
VI leaf;
FOR (u, 0, n * m)
{
if (!used[u])
continue;
int cnt = 0;
for (auto to : g[u])
{
if (used[to] && c[to] == 0)
cnt++;
}
if (cnt == 1)
leaf.PB(u);
}
//cerr << SZ(leaf) << '\n';
if (SZ(leaf) > 4)
{
cout << "NO MINIMAL CORRIDOR\n";
clear();
return;
}
fill(used, used + n * m, false);
dfs2(v);
int cnt = 0;
int all = 0;
FOR (i, 0, n * m)
{
if (used[i] && !con[i])
cnt++;
all += used[i];
}
//cerr << SZ(leaf) << ' ' << cnt << '\n';
assert(cnt >= SZ(leaf));
if (cnt != SZ(leaf))
{
cout << "NO MINIMAL CORRIDOR\n";
clear();
return;
}
bool ok = true;
for (auto l : leaf)
{
c[l] = 1;
fill(used, used + n * m, false);
int u = v;
if (v == l)
{
for (auto to : g[v])
if (c[to] == 0)
u = to;
assert(u != v);
}
int x = dfs(u);
if (x == 15)
ok = false;
//cerr << l << ' ' << v << ' ' << x << ' ' << ok << '\n';
c[l] = 0;
}
clear();
if (!ok)
{
cout << "NO MINIMAL CORRIDOR\n";
return;
}
else
cout << all << '\n';
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3880kb
input:
2 6 6 010101001001 001000101100 110101001101 010010000100 001110110010 001001101010 6 6 010010110010 001100100111 000110100101 011001100101 100100011100 011010001101
output:
17 NO MINIMAL CORRIDOR
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 1ms
memory: 3604kb
input:
1 3 4 11110111 01000000 11110111
output:
9
result:
ok single line: '9'
Test #3:
score: 0
Accepted
time: 1ms
memory: 3888kb
input:
7 1 1 00 1 1 10 1 1 01 2 1 00 00 2 1 10 00 3 1 10 00 01 3 1 10 00 00
output:
2 NO MINIMAL CORRIDOR NO MINIMAL CORRIDOR NO MINIMAL CORRIDOR 3 4 NO MINIMAL CORRIDOR
result:
ok 7 lines
Test #4:
score: 0
Accepted
time: 0ms
memory: 3892kb
input:
9 3 3 000100 100100 001110 3 3 000101 100100 000010 2 3 101111 010001 3 3 000101 100100 001100 3 3 011100 001001 001000 1 56 0001001000010010000100100001001000010010000100100001001000010010000100100001001000010010000100100001001000010010 1 56 000100100001001000010010000100100001001000010010000100100...
output:
NO MINIMAL CORRIDOR 7 5 NO MINIMAL CORRIDOR NO MINIMAL CORRIDOR 84 NO MINIMAL CORRIDOR 79 NO MINIMAL CORRIDOR
result:
ok 9 lines
Test #5:
score: 0
Accepted
time: 1ms
memory: 3608kb
input:
4 6 6 010101001001 011000101100 110101001101 110010000100 001110000010 001001101010 6 6 101010010010 001010001010 011011101011 001001001011 101010011001 110101001101 6 6 010010110010 001110100111 000110101101 011001100101 100100011100 011010001101 6 6 000010101000 000011010100 110110011101 010000111...
output:
NO MINIMAL CORRIDOR NO MINIMAL CORRIDOR NO MINIMAL CORRIDOR NO MINIMAL CORRIDOR
result:
ok 4 lines
Test #6:
score: 0
Accepted
time: 1ms
memory: 3648kb
input:
8 10 9 101111001001111101 110001101110101101 101101001010010111 011111011100010101 100101001011101101 010110011111011001 101000100001001000 011110110010101110 100100110110100001 000101100111011111 11 11 0110100101010010111101 1011000110100101110100 0011000100111100011010 1111110101100101000100 00011...
output:
29 NO MINIMAL CORRIDOR NO MINIMAL CORRIDOR NO MINIMAL CORRIDOR 43 NO MINIMAL CORRIDOR 55 NO MINIMAL CORRIDOR
result:
ok 8 lines
Test #7:
score: 0
Accepted
time: 0ms
memory: 3568kb
input:
4 2 2 0010 0011 3 3 111000 010111 100111 11 10 11111111011111111111 11111111001111111111 11111111101111111111 11111111001111111111 11111111011111100001 11111111000010011111 11111001011111111111 11100111111111111111 10001111111111111111 01101111111111111111 10011111111111111111 6 6 010101001001 00100...
output:
NO MINIMAL CORRIDOR 7 29 NO MINIMAL CORRIDOR
result:
ok 4 lines
Test #8:
score: 0
Accepted
time: 9ms
memory: 9476kb
input:
2 125 125 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000...
output:
NO MINIMAL CORRIDOR NO MINIMAL CORRIDOR
result:
ok 2 lines
Test #9:
score: 0
Accepted
time: 14ms
memory: 15388kb
input:
1 249 249 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
745
result:
ok single line: '745'
Test #10:
score: 0
Accepted
time: 20ms
memory: 15512kb
input:
1 249 250 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
746
result:
ok single line: '746'
Test #11:
score: 0
Accepted
time: 7ms
memory: 15516kb
input:
1 250 249 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
746
result:
ok single line: '746'
Test #12:
score: 0
Accepted
time: 13ms
memory: 15544kb
input:
1 250 250 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
748
result:
ok single line: '748'
Test #13:
score: 0
Accepted
time: 5ms
memory: 6460kb
input:
2 100 100 00100000101011101100100000001100011100100011000100010100010000000000101110100100001010111000101011011000101111111101011100110100001100001100100101100010001011110011110011110100001100001110011011001010 11110111011101110111111101110111011111110111011111111111111101110111011111110111111101110...
output:
7378 NO MINIMAL CORRIDOR
result:
ok 2 lines
Test #14:
score: 0
Accepted
time: 7ms
memory: 14008kb
input:
1 200 200 00100100000110001100010110101101000011100011001111010011000101111010111000011000010011011100110010010101100110010100000101001101001110100010011000100000101101001101111111010010110101010010001101010111010011110101100001000100000010010010000001110110101010110000100100111110101100000001010011...
output:
29753
result:
ok single line: '29753'
Test #15:
score: 0
Accepted
time: 10ms
memory: 11788kb
input:
1 200 200 11010011001011011100011000100111100001110000000110001100111010101011111110011001011011000101100010110110100100101110001101010111111000010010001100110100000110001110011001100110111101000111010101100111001000001110000110101101011000101110011111111000000011000111001100010101100001110111101001...
output:
NO MINIMAL CORRIDOR
result:
ok single line: 'NO MINIMAL CORRIDOR'
Test #16:
score: 0
Accepted
time: 21ms
memory: 18756kb
input:
1 240 240 10010001010111000111011011000100010110100111011011001111011011000110001001010110111101111000110110000101110011100101111100110110010100110111001011101001111100101101110101011101000111010010101010011110010101011010011000111011000011100010111011111000110001011001111000111010011000010110010000...
output:
42903
result:
ok single line: '42903'
Test #17:
score: 0
Accepted
time: 5ms
memory: 15560kb
input:
1 240 240 10100111110110011001001111000000011011101011001011001010101110101010111010000110001011111000001100010110111001001000000010101001111101111100101110100010011001110011011111001101010011000110101001011010001101111100110111101001101110000001011000001000111000101001101100110110101001001010010100...
output:
NO MINIMAL CORRIDOR
result:
ok single line: 'NO MINIMAL CORRIDOR'
Test #18:
score: 0
Accepted
time: 3ms
memory: 4456kb
input:
6 40 40 01000000000000000000000000000000000000000000000000000000000000000000000000000000 00100000000000000000000000000000000000000000000000000000000000000000000000000000 01000000000000000000000000000000000000000000000000000000000000000000000000000000 0010000000000000000000000000000000000000000000000...
output:
118 118 NO MINIMAL CORRIDOR NO MINIMAL CORRIDOR NO MINIMAL CORRIDOR NO MINIMAL CORRIDOR
result:
ok 6 lines
Test #19:
score: 0
Accepted
time: 1ms
memory: 3920kb
input:
9 27 27 000000000000000000000000000000000000000000000000000010 000000000000000000000000000000000000000000000000000100 000000000000000000000000000000000000000000000000000010 000000000000000000000000000000000000000000000000000100 000000000000000000000000000000000000000000000000000010 00000000000000000...
output:
79 79 79 NO MINIMAL CORRIDOR NO MINIMAL CORRIDOR NO MINIMAL CORRIDOR NO MINIMAL CORRIDOR NO MINIMAL CORRIDOR NO MINIMAL CORRIDOR
result:
ok 9 lines