QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#109444#5202. Dominoesgigabuffoon#AC ✓123ms45392kbC++203.9kb2023-05-29 01:37:422023-05-29 01:37:47

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-29 01:37:47]
  • 评测
  • 测评结果:AC
  • 用时:123ms
  • 内存:45392kb
  • [2023-05-29 01:37:42]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define all(a) begin(a),end(a)
#define sz(a) int(size(a))
#define rep(i,a,b) for(int i=(a);i<(b);++i)
using vi = vector<int>;
using ll = long long int;

const ll MAXA = ll(1e6);

bool dfs(int a, int L, vector<vi>& g, vi& btoa, vi &A, vi &B) {
    if(A[a] != L) return 0;
    A[a] = -1;
    for(int b : g[a]) if(B[b] == L+1) {
        B[b] = 0;
        if(btoa[b] == -1 || dfs(btoa[b], L+1, g, btoa, A, B))
            return btoa[b] = a, 1;
    }
    return 0;
}

int hopcroftKarp(vector<vi>& g, vi& btoa) {
    int res = 0;
    vi A(g.size()), B(btoa.size()), cur, next;
    for(;;) {
        fill(all(A), 0);
        fill(all(B), 0);
        cur.clear();
        for(int a : btoa) if(a != -1) A[a] = -1;
        rep(a,0,sz(g)) if(A[a] == 0) cur.push_back(a);
        for(int lay = 1;; lay++) {
            bool islast = 0;
            next.clear();
            for(int a : cur) for(int b : g[a]) {
                if(btoa[b] == -1) {
                    B[b] = lay;
                    islast = 1;
                }
                else if(btoa[b] != a && !B[b]) {
                    B[b] = lay;
                    next.push_back(btoa[b]);
                }
            }
            if(islast) break;
            if(next.empty()) return res;
            for(int a : next) A[a] = lay;
            cur.swap(next);
        }
        rep(a,0,sz(g))
            res += dfs(a, 0, g, btoa, A, B);
    }
}

vi leftIDs, rightIDs;
int count(int side, int id, vector<vi>& g, vi& btoa, vector<vi>& vis) {
    vis[side][id] = true;
    if(side) {
        if(!vis[1-side][btoa[id]]) return count(1-side, btoa[id], g, btoa, vis);
        return 0;
    }
    int res = 1;
    for(int to : g[id]) if(btoa[to] != id and !vis[1-side][to])
        res += count(1-side, to, g, btoa, vis);
    return res;
}

void solve(int tc) {
    int N, M; cin >> N >> M;
    vector<string> grid(N);
    for(string &s : grid) cin >> s;

    vector<vi> id(N, vi(M));
    vector<vi> leftAdj(N*M);
    for(int r = 0; r < N; r++)
        for(int c = 0; c < M; c++) {
            if(grid[r][c] == '#') continue;
            if((r ^ c) & 1) {
                // right side cell
                id[r][c] = sz(rightIDs);
                rightIDs.push_back(M*r + c);
                if(r and grid[r-1][c] != '#') {
                    // up direction
                    leftAdj[id[r-1][c]].push_back(id[r][c]);
                }
                if(c and grid[r][c-1] != '#') {
                    // left direction
                    leftAdj[id[r][c-1]].push_back(id[r][c]);
                }
            } else {
                // left side cell
                id[r][c] = sz(leftIDs);
                leftIDs.push_back(M*r + c);
                if(r and grid[r-1][c] != '#') {
                    // up direction
                    leftAdj[id[r][c]].push_back(id[r-1][c]);
                }
                if(c and grid[r][c-1] != '#') {
                    // left direction
                    leftAdj[id[r][c]].push_back(id[r][c-1]);
                }
            }
        }
    
    assert(sz(leftIDs) == sz(rightIDs));

    int K = sz(leftIDs);
    ll sames = ll(K) * (K-1);
    if(sames >= MAXA) {
        cout << MAXA << '\n';
        return;
    }

    // otherwise, it's small enough to build and do a matching
    leftAdj.erase(begin(leftAdj) + K);
    vi btoa(K, -1);
    assert(hopcroftKarp(leftAdj, btoa) == K);

    vector<vi> vis(2, vi(K));
    ll addl = 0;
    for(int ri = 0; ri < K; ri++) {
        vis[0].assign(K, 0), vis[1].assign(K, 0);
        addl += count(1, ri, leftAdj, btoa, vis);
    }
    
    cout << min(sames + K*K - addl, MAXA) << '\n';

}

int main() {
    cin.tie(0)->sync_with_stdio(false);
    cin.exceptions(cin.failbit);
    int t = 1;
    // cin >> t;
    for(int i = 1; i <= t; i++) solve(i);
    cout.flush(); return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 3400kb

input:

3 6
...#..
......
#...##

output:

52

result:

ok 1 number(s): "52"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3460kb

input:

2 2
..
..

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 2ms
memory: 3452kb

input:

2 2
#.
#.

output:

0

result:

ok 1 number(s): "0"

Test #4:

score: 0
Accepted
time: 2ms
memory: 3484kb

input:

4 5
###..
.###.
.##..
#...#

output:

34

result:

ok 1 number(s): "34"

Test #5:

score: 0
Accepted
time: 2ms
memory: 3468kb

input:

11 12
.#######..##
.##..#.....#
#####..##.#.
#..##...#...
###..#..##..
####..###...
.#....##..##
.#####....##
.###..######
.######...##
#....##...##

output:

1674

result:

ok 1 number(s): "1674"

Test #6:

score: 0
Accepted
time: 1ms
memory: 3516kb

input:

50 45
####...#.#####..#..#.#########.##.#####..#..#
##.#.....#..#####....##..###...##.....##....#
##.#...####.##.#..#...####.#....##.###.......
...#...#..#..#.##.######...##..#...###..####.
##.....#.###.####.###..#....##..##..##.###...
..#.##.......##...##.........#..##.###.###...
###..##.###...###....

output:

496312

result:

ok 1 number(s): "496312"

Test #7:

score: 0
Accepted
time: 5ms
memory: 3548kb

input:

34 65
...............#####.#..##..############.....###....#..##########
.........#......#.......##..############.##..##........##########
..............#.......#.....##########..............##.##########
...##............#..............######.......#......#..##########
......#....#.....##......#.......

output:

279744

result:

ok 1 number(s): "279744"

Test #8:

score: 0
Accepted
time: 15ms
memory: 3604kb

input:

44 44
............................................
............................................
............................................
............................................
............................................
............................................
...........................

output:

936056

result:

ok 1 number(s): "936056"

Test #9:

score: 0
Accepted
time: 17ms
memory: 3584kb

input:

45 45
.............................................
.............................................
.............................................
.............................................
.............................................
.............................................
.....................

output:

999000

result:

ok 1 number(s): "999000"

Test #10:

score: 0
Accepted
time: 15ms
memory: 3736kb

input:

59 59
...#.......#.......#...#...#...................#...........
.#.#.#####.#.#####.#.#.#.#.#.#################.#.#########.
.#.#.#.....#.#...#.#.#.#.#.#.#...............#.#.#...#...#.
.#.#.#.#####.#.#.#.#.#.#.#.#.#.#############.#.#.#.#.#.#.#.
.#.#.#.#...#.#.#.#...#...#...#.#...#.........#...#.#.#...

output:

809100

result:

ok 1 number(s): "809100"

Test #11:

score: 0
Accepted
time: 14ms
memory: 3704kb

input:

39 99
...#.......#...#...................#...#...#...#...#...........#...#.......#.......................
.#.#.#####.#.#.#.#################.#.#.#.#.#.#.#.#.#.#########.#.#.#.#####.#.#####################.
.#.#.....#.#.#.#.........#.........#.#.#.#.#.#.#.#.#.#...#.....#.#.#.#.....#.....#.......#...#...

output:

999000

result:

ok 1 number(s): "999000"

Test #12:

score: 0
Accepted
time: 15ms
memory: 3696kb

input:

99 39
.......#.......#.......................
.#####.#.#####.#.#####################.
.....#.#.....#.#.#.......#.............
####.#.#####.#.#.#.#####.#.############
.....#.#.....#...#.#.....#.#...........
.#####.#.#########.#.#####.#.#########.
.....#.#.....#...#.#.....#.#.....#.....
####.#.#####.#...

output:

999000

result:

ok 1 number(s): "999000"

Test #13:

score: 0
Accepted
time: 16ms
memory: 3704kb

input:

45 45
#.......................................###..
.........................................##..
.............................................
.............................................
.............................................
.............................................
.....................

output:

999000

result:

ok 1 number(s): "999000"

Test #14:

score: 0
Accepted
time: 2ms
memory: 5796kb

input:

132 453
###########################################################..####################..###################################################################.#################################################..############################..############################################################...

output:

1000000

result:

ok 1 number(s): "1000000"

Test #15:

score: 0
Accepted
time: 13ms
memory: 3768kb

input:

312 14
##........#...
##............
...#...#......
..............
..............
......#.......
..............
......##......
..............
...#..........
..............
..............
..............
..............
..............
..............
..............
..............
..............
...........

output:

1000000

result:

ok 1 number(s): "1000000"

Test #16:

score: 0
Accepted
time: 2ms
memory: 3392kb

input:

1 2
..

output:

0

result:

ok 1 number(s): "0"

Test #17:

score: 0
Accepted
time: 2ms
memory: 3392kb

input:

2 1
.
.

output:

0

result:

ok 1 number(s): "0"

Test #18:

score: 0
Accepted
time: 3ms
memory: 3540kb

input:

1 1000
........................................................................................................................................................................................................................................................................................................

output:

374250

result:

ok 1 number(s): "374250"

Test #19:

score: 0
Accepted
time: 3ms
memory: 3588kb

input:

1000 1
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
....

output:

374250

result:

ok 1 number(s): "374250"

Test #20:

score: 0
Accepted
time: 29ms
memory: 39612kb

input:

1000 1000
###############################################################################################.##################################################################################################################################################################################################...

output:

1000000

result:

ok 1 number(s): "1000000"

Test #21:

score: 0
Accepted
time: 123ms
memory: 45392kb

input:

1000 1000
##################################################################################################################################################################################################################################################################################################...

output:

1000000

result:

ok 1 number(s): "1000000"

Test #22:

score: 0
Accepted
time: 4ms
memory: 32084kb

input:

1000 1000
##################################################################################################################################################################################################################################################################################################...

output:

1000000

result:

ok 1 number(s): "1000000"

Test #23:

score: 0
Accepted
time: 35ms
memory: 39516kb

input:

1000 1000
##################################################################################################################################################################################################################################################################################################...

output:

1000000

result:

ok 1 number(s): "1000000"

Test #24:

score: 0
Accepted
time: 25ms
memory: 41160kb

input:

999 999
.......#...............#...................................#.......#...............#.......#...............#.......#...#...#...............................#...#...........#.......#...........................#.......#...........#...........#.......#...#.......#.......#...#...#...................

output:

1000000

result:

ok 1 number(s): "1000000"