QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#650754#4773. Piece it togetherSGColin#AC ✓25ms19156kbC++203.0kb2024-10-18 16:22:512024-10-18 16:23:11

Judging History

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

  • [2024-10-18 16:23:11]
  • 评测
  • 测评结果:AC
  • 用时:25ms
  • 内存:19156kb
  • [2024-10-18 16:22:51]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;

inline int rd() {
    int x = 0;
    bool f = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) f |= (c == '-');
    for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
    return f ? -x : x;
}

#define eb emplace_back
#define all(s) (s).begin(), (s).end()
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)

template<typename T>
void getmin(T &a, T b) {a = (a > b ? b : a);}

template <const int V, const int inf = 1000000000>
struct Hopcroft_Karp {
    bool vis[V];
    vector<int> e[V];
    int nl, nr, ml[V], mr[V], dl[V], dr[V];
    inline void reset(int _nl, int _nr) {
        nl = _nl; rep(i, 0, nl) ml[i] = 0, e[i].clear();
        nr = _nr; rep(i, 0, nr) mr[i] = 0, vis[i] = false;
    }
    inline bool bfs() {
        static int q[V], hd, tl, dT; hd=1; tl=0; dT=inf;
        memset(dl,-1,sizeof(int) * (nl + 1));
        memset(dr,-1,sizeof(int) * (nr + 1));
        for (int i=1; i<=nl; ++i) if (!ml[i]) {dl[i] = 0; q[++tl] = i;}
        for (int u;hd<=tl;) {
            if(dl[u=q[hd++]]>=dT) break;
            for (auto v:e[u])
                if (dr[v] == -1) {
                    dr[v] = dl[u] + 1;
                    if(!mr[v]) getmin(dT, dr[v] + 1);
                    else {dl[mr[v]] = dr[v] + 1; q[++tl] = mr[v];}
                }
        }
        return dT != inf;
    }
    bool dfs(int u) {
        for(auto v:e[u]) {
            if (vis[v]||dl[u]+1!=dr[v]) continue;
            vis[v]=true;
            if(!mr[v] || dfs(mr[v])) {
                mr[v]=u; ml[u]=v; return true;
            }
        }
        return false;
    }
    inline void add(int u,int v) {e[u].push_back(v);}
    inline int max_match() {
        int ans=0;
        while(bfs()) {
            memset(vis,0,sizeof(bool)*(nr+1));
            for(int i=1;i<=nl;++i) if(!ml[i]) ans+=dfs(i);
        }
        return ans;
    }
};

Hopcroft_Karp<250000> G;

const int N = 507;

char s[N][N];

int id[N][N];

inline void work() {
    int n = rd(), m = rd();
    int cntW = 0, cntB = 0;
    rep(i, 1, n) rep(j, 1, m) {
        char c = getchar();
        while (c != '.' && !isalpha(c)) c = getchar();
        s[i][j] = c;
        if (c == 'B') id[i][j] = ++cntB;
        if (c == 'W') id[i][j] = ++cntW;
    }
    if (cntW != cntB * 2) {puts("NO"); return;}
    G.reset(cntW, cntW);
    rep(i, 1, n) rep(j, 1, m)
        if (s[i][j] == 'B') {
            int ud = (id[i][j] << 1);
            if (s[i - 1][j] == 'W') G.add(ud, id[i - 1][j]);
            if (s[i + 1][j] == 'W') G.add(ud, id[i + 1][j]);
            int lr = ud - 1;
            if (s[i][j - 1] == 'W') G.add(lr, id[i][j - 1]);
            if (s[i][j + 1] == 'W') G.add(lr, id[i][j + 1]);
        }
    puts(G.max_match() == cntW ? "YES" : "NO");
}

int main() {
    per(t, rd(), 1) work();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 13976kb

input:

2
3 4
BWW.
WWBW
..WB
3 3
W..
BW.
WBW

output:

YES
NO

result:

ok 2 token(s): yes count is 1, no count is 1

Test #2:

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

input:

70
3 4
BWW.
WWBW
..WB
3 3
W..
BW.
WBW
1 1
B
3 3
...
.W.
...
2 2
W.
BW
2 3
.W.
WBW
1 3
WBW
2 5
.W.W.
WB.BW
2 2
WW
.B
2 2
WB
..
3 3
WWB
BWW
WWB
3 5
.W.WB
WBW.W
...WB
4 5
..W..
.WBW.
WBWBW
.WBW.
3 9
BWW...W..
WWBW..BW.
..WB..WBW
4 12
BWWBWWBWWBWW
WWBWWBWWBWWB
BWWBWWBWWBWW
WWBWWBWWBWWB
7 7
BWWBBWW
WBWWW...

output:

YES
NO
NO
NO
YES
NO
NO
YES
NO
NO
NO
NO
YES
NO
YES
YES
NO
YES
YES
YES
NO
YES
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
YES
YES
NO
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
NO
YES
YES
YES
NO
YES
NO
YES
NO
NO
NO
NO
NO
YES
NO
YES
YES
NO
NO
NO
NO
NO

result:

ok 70 token(s): yes count is 34, no count is 36