QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#132661 | #5202. Dominoes | Swarthmore# | AC ✓ | 419ms | 124632kb | C++17 | 6.9kb | 2023-07-31 00:03:57 | 2023-07-31 00:04:00 |
Judging History
answer
#include "bits/stdc++.h"
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
template<class T> using pq = priority_queue<T>;
template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a,x) for (auto& a : x)
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef DEBUG
#define dbg(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << endl;
#else
#define dbg(x...)
#endif
const int MOD = 1000000007;
const char nl = '\n';
const int MX = 100001;
struct Edge {
int v, u; ll cap, flow = 0;
Edge(int v, int u, ll cap) : v(v), u(u),cap(cap) {}
};
struct Dinic {
const ll flow_inf = 1e18;
vector<Edge> edges;
vector<vi> adj;
int n, m = 0;
int s, t;
vi level, ptr;
queue<int> q;
Dinic(int n, int s, int t) : n(n), s(s), t(t) {
adj.resize(n); level.resize(n); ptr.resize(n);
}
void add_edge(int v, int u, ll cap) {
edges.emplace_back(v, u, cap);
edges.emplace_back(u, v, 0);
adj[v].pb(m); adj[u].pb(m+1); m += 2;
}
bool bfs() {
while (!q.empty()) {
int v = q.front(); q.pop();
trav(id, adj[v]) {
if (edges[id].cap - edges[id].flow < 1) {
continue;
}
if (level[edges[id].u] != -1) continue;
level[edges[id].u] = level[v] + 1;
q.push(edges[id].u);
}
}
return level[t] != -1;
}
ll dfs(int v, ll pushed) {
if (pushed == 0) return 0;
if (v == t) return pushed;
for (int& cid = ptr[v]; cid < (int)adj[v].size(); cid++) {
int id = adj[v][cid];
int u = edges[id].u;
if (level[v] + 1 != level[u] || edges[id].cap - edges[id].flow < 1) continue;
ll tr = dfs(u, min(pushed, edges[id].cap - edges[id].flow));
if (tr == 0) continue;
edges[id].flow += tr;
edges[id^1].flow -= tr;
return tr;
}
return 0;
}
ll flow() {
ll f = 0;
while (true) {
fill(level.begin(), level.end(), -1);
level[s] = 0;
q.push(s);
if (!bfs()) break;
fill(ptr.begin(), ptr.end(), 0);
while (ll pushed = dfs(s, flow_inf)) f += pushed;
}
return f;
}
};
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
int N, M;
bool valid(int i, int j) { return i >= 0 && i < N && j >= 0 && j < M; }
void solve() {
cin >> N >> M;
string S[N]; F0R(i, N) cin >> S[i];
ll ans = 0;
F0R(i, N) F0R(j, M) ans += (S[i][j] == '.');
ans /= 2;
if (ans * (ans-1) > 1000000) {
cout << 1000000 << nl; return;
}
ans = ans * (ans-1) + ans * ans;
Dinic din(N*M+2, N*M, N*M+1);
F0R(i, N) {
F0R(j, M) {
if ((i+j)%2) {
din.add_edge(i*M+j, N*M+1, 1);
} else {
din.add_edge(N*M, i*M+j, 1);
}
}
}
F0R(i, N) {
F0R(j, M) {
if ((i+j)%2 == 0) {
if (S[i][j] == '#') continue;
F0R(k, 4) {
int x = i + dx[k], y = j + dy[k];
if (valid(x, y) && S[x][y] == '.') {
din.add_edge(i*M+j, x*M+y, 1);
}
}
}
}
}
din.flow();
map<int, int> vs;
F0R(i, N) {
F0R(j, M) {
if ((i+j)%2 && S[i][j] == '.') {
vs[i*M+j] = sz(vs);
}
}
}
int match[N][M];
F0R(i, N) {
F0R(j, M) {
match[i][j] = -1;
}
}
trav(a, din.edges) {
if (a.v < N*M && a.u < N*M && a.flow == 1) {
match[a.v/M][a.v%M] = vs[a.u];
}
}
/*F0R(i, N) {
F0R(j, M) {
cout << match[i][j] << " ";
}
cout << endl;
}*/
//dbg(vs);
vector<vi> graph(sz(vs));
trav(a, vs) {
int x = a.f/M, y = a.f%M;
F0R(i, 4) {
int nx = x + dx[i], ny = y + dy[i];
if (valid(nx, ny) && S[nx][ny] == '.') {
graph[a.s].pb(match[nx][ny]);
}
}
}
F0R(r, sz(vs)) {
bool vis[sz(vs)]; F0R(i, sz(vs)) vis[i] = false;
vis[r] = true; ans--;
queue<int> q; q.push(r);
while (!q.empty()) {
int v = q.front(); q.pop();
trav(a, graph[v]) {
if (!vis[a]) {
vis[a] = true; ans--;
q.push(a);
}
}
}
}
cout << min(ans, (ll) 1000000) << nl;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int T = 1;
// cin >> T;
while(T--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3548kb
input:
3 6 ...#.. ...... #...##
output:
52
result:
ok 1 number(s): "52"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3556kb
input:
2 2 .. ..
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3604kb
input:
2 2 #. #.
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3604kb
input:
4 5 ###.. .###. .##.. #...#
output:
34
result:
ok 1 number(s): "34"
Test #5:
score: 0
Accepted
time: 1ms
memory: 3628kb
input:
11 12 .#######..## .##..#.....# #####..##.#. #..##...#... ###..#..##.. ####..###... .#....##..## .#####....## .###..###### .######...## #....##...##
output:
1674
result:
ok 1 number(s): "1674"
Test #6:
score: 0
Accepted
time: 2ms
memory: 3876kb
input:
50 45 ####...#.#####..#..#.#########.##.#####..#..# ##.#.....#..#####....##..###...##.....##....# ##.#...####.##.#..#...####.#....##.###....... ...#...#..#..#.##.######...##..#...###..####. ##.....#.###.####.###..#....##..##..##.###... ..#.##.......##...##.........#..##.###.###... ###..##.###...###....
output:
496312
result:
ok 1 number(s): "496312"
Test #7:
score: 0
Accepted
time: 4ms
memory: 3776kb
input:
34 65 ...............#####.#..##..############.....###....#..########## .........#......#.......##..############.##..##........########## ..............#.......#.....##########..............##.########## ...##............#..............######.......#......#..########## ......#....#.....##......#.......
output:
279744
result:
ok 1 number(s): "279744"
Test #8:
score: 0
Accepted
time: 3ms
memory: 3928kb
input:
44 44 ............................................ ............................................ ............................................ ............................................ ............................................ ............................................ ...........................
output:
936056
result:
ok 1 number(s): "936056"
Test #9:
score: 0
Accepted
time: 11ms
memory: 4044kb
input:
45 45 ............................................. ............................................. ............................................. ............................................. ............................................. ............................................. .....................
output:
999000
result:
ok 1 number(s): "999000"
Test #10:
score: 0
Accepted
time: 8ms
memory: 4048kb
input:
59 59 ...#.......#.......#...#...#...................#........... .#.#.#####.#.#####.#.#.#.#.#.#################.#.#########. .#.#.#.....#.#...#.#.#.#.#.#.#...............#.#.#...#...#. .#.#.#.#####.#.#.#.#.#.#.#.#.#.#############.#.#.#.#.#.#.#. .#.#.#.#...#.#.#.#...#...#...#.#...#.........#...#.#.#...
output:
809100
result:
ok 1 number(s): "809100"
Test #11:
score: 0
Accepted
time: 9ms
memory: 4092kb
input:
39 99 ...#.......#...#...................#...#...#...#...#...........#...#.......#....................... .#.#.#####.#.#.#.#################.#.#.#.#.#.#.#.#.#.#########.#.#.#.#####.#.#####################. .#.#.....#.#.#.#.........#.........#.#.#.#.#.#.#.#.#.#...#.....#.#.#.#.....#.....#.......#...#...
output:
999000
result:
ok 1 number(s): "999000"
Test #12:
score: 0
Accepted
time: 10ms
memory: 3988kb
input:
99 39 .......#.......#....................... .#####.#.#####.#.#####################. .....#.#.....#.#.#.......#............. ####.#.#####.#.#.#.#####.#.############ .....#.#.....#...#.#.....#.#........... .#####.#.#########.#.#####.#.#########. .....#.#.....#...#.#.....#.#.....#..... ####.#.#####.#...
output:
999000
result:
ok 1 number(s): "999000"
Test #13:
score: 0
Accepted
time: 9ms
memory: 3932kb
input:
45 45 #.......................................###.. .........................................##.. ............................................. ............................................. ............................................. ............................................. .....................
output:
999000
result:
ok 1 number(s): "999000"
Test #14:
score: 0
Accepted
time: 3ms
memory: 10744kb
input:
132 453 ###########################################################..####################..###################################################################.#################################################..############################..############################################################...
output:
1000000
result:
ok 1 number(s): "1000000"
Test #15:
score: 0
Accepted
time: 12ms
memory: 4256kb
input:
312 14 ##........#... ##............ ...#...#...... .............. .............. ......#....... .............. ......##...... .............. ...#.......... .............. .............. .............. .............. .............. .............. .............. .............. .............. ...........
output:
1000000
result:
ok 1 number(s): "1000000"
Test #16:
score: 0
Accepted
time: 1ms
memory: 3588kb
input:
1 2 ..
output:
0
result:
ok 1 number(s): "0"
Test #17:
score: 0
Accepted
time: 1ms
memory: 3532kb
input:
2 1 . .
output:
0
result:
ok 1 number(s): "0"
Test #18:
score: 0
Accepted
time: 2ms
memory: 3840kb
input:
1 1000 ........................................................................................................................................................................................................................................................................................................
output:
374250
result:
ok 1 number(s): "374250"
Test #19:
score: 0
Accepted
time: 2ms
memory: 3808kb
input:
1000 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....
output:
374250
result:
ok 1 number(s): "374250"
Test #20:
score: 0
Accepted
time: 100ms
memory: 124632kb
input:
1000 1000 ###############################################################################################.##################################################################################################################################################################################################...
output:
1000000
result:
ok 1 number(s): "1000000"
Test #21:
score: 0
Accepted
time: 419ms
memory: 123924kb
input:
1000 1000 ##################################################################################################################################################################################################################################################################################################...
output:
1000000
result:
ok 1 number(s): "1000000"
Test #22:
score: 0
Accepted
time: 4ms
memory: 4660kb
input:
1000 1000 ##################################################################################################################################################################################################################################################################################################...
output:
1000000
result:
ok 1 number(s): "1000000"
Test #23:
score: 0
Accepted
time: 4ms
memory: 4588kb
input:
1000 1000 ##################################################################################################################################################################################################################################################################################################...
output:
1000000
result:
ok 1 number(s): "1000000"
Test #24:
score: 0
Accepted
time: 4ms
memory: 4580kb
input:
999 999 .......#...............#...................................#.......#...............#.......#...............#.......#...#...#...............................#...#...........#.......#...........................#.......#...........#...........#.......#...#.......#.......#...#...#...................
output:
1000000
result:
ok 1 number(s): "1000000"