QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#88444#4107. 墙上的句子Ptilopsis_w100 ✓102ms5480kbC++145.4kb2023-03-16 11:24:142023-03-16 11:24:17

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-16 11:24:17]
  • 评测
  • 测评结果:100
  • 用时:102ms
  • 内存:5480kb
  • [2023-03-16 11:24:14]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

const int INF1 = 1e4;
const int INF2 = 1e8;
const int N = 405;
const int M = N*N*10;

namespace netflow {
    struct edge {
        int to, flow, next;
    } e[M*2];
    int head[N*N], ecnt = 1;
    int dis[N*N], cur[N*N];
    int S, T;
    int dinic();
    bool bfs(int S, int T);
    int dfs(int x, int flow);
    void add_edge(int a, int b, int f);
    void clear();
} using namespace netflow;

int n, m;
int a[N], b[N];
bool ta[N], tb[N];
char s[N][N];

void solve();
string rvs(string a);

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int T; cin >> T;
    while(T --> 0) solve();
}

inline int line(int x, int t) { return n*t + x; }
inline int col(int x, int t) { return n*2 + m*t + x; }

void solve()
{
    netflow::clear();
    memset(ta, 0, sizeof(ta));
    memset(tb, 0, sizeof(tb));
    
    int ans = 0;
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    for(int i = 1; i <= m; i++)
        cin >> b[i];
    for(int i = 1; i <= n; i++)
        cin >> (s[i]+1);
    
    map<string, int> mp;
    int cnt = n*2+m*2;
    for(int i = 1; i <= n; i++)
    {
        string tmp;
        for(int j = 1; j <= m; j++)
        {
            if(s[i][j] != '_')
                tmp += s[i][j];
            if((s[i][j] == '_' or j == m) and !tmp.empty())
            {
                string x = tmp, y = rvs(tmp);
                tmp.clear();
                if(x == y)
                {
                    if(!mp.count(x))
                        mp[x] = -1, ans++;
                    continue;
                }
                if(x > y) swap(x, y), ta[i] = true;
                if(!mp.count(x)) mp[x] = ++cnt;
                if(!mp.count(y)) mp[y] = ++cnt;
                add_edge(line(i,0), mp[x], INF2);
                add_edge(mp[y], line(i,1), INF2);
            }
        }
    }
    
    for(int j = 1; j <= m; j++)
    {
        string tmp;
        for(int i = 1; i <= n; i++)
        {
            if(s[i][j] != '_')
                tmp += s[i][j];
            if((s[i][j] == '_' or i == n) and !tmp.empty())
            {
                string x = tmp, y = rvs(tmp);
                tmp.clear();
                if(x == y)
                {
                    if(!mp.count(x))
                        mp[x] = -1, ans++;
                    continue;
                }
                if(x > y) swap(x, y), tb[j] = true;
                if(!mp.count(x)) mp[x] = ++cnt;
                if(!mp.count(y)) mp[y] = ++cnt;
                add_edge(col(j,0), mp[x], INF2);
                add_edge(mp[y], col(j,1), INF2);
            }
        }
    }
    
    for(const auto &i : mp)
    {
        string x = i.first, y = rvs(i.first);
        if(x < y) add_edge(mp[x], mp[y], 1);
    }
    
    S = cnt+1, T = cnt+2;
    
    for(int i = 1; i <= n; i++)
    {
        if(a[i] != 1)
        {
            if(ta[i] == 0) add_edge(S, line(i,0), INF1);
            else add_edge(line(i,1), T, INF1);
        }
        if(a[i] != -1)
        {
            if(ta[i] == 0) add_edge(line(i,1), T, INF1);
            else add_edge(S, line(i,0), INF1);
        }
        add_edge(line(i,0), line(i,1), INF2);
    }
    
    
    for(int j = 1; j <= m; j++)
    {
        if(b[j] != 1)
        {
            if(tb[j] == 0) add_edge(S, col(j,0), INF1);
            else add_edge(col(j,1), T, INF1);
        }
        if(b[j] != -1)
        {
            if(tb[j] == 0) add_edge(col(j,1), T, INF1);
            else add_edge(S, col(j,0), INF1);
        }
        add_edge(col(j,0), col(j,1), INF2);
    }
    
    cout << (ans+dinic()*2)%INF1 << "\n";
}

string rvs(string a)
{
    reverse(a.begin(), a.end());
    return a;
}

namespace netflow {
    int dinic()
    {
        int maxflow = 0;
        while(bfs(S, T))
            maxflow += dfs(S, INF2);
        return maxflow;
    }
    bool bfs(int S, int T)
    {
        memcpy(cur, head, sizeof(cur));
        memset(dis, 0, sizeof(dis));
        queue<int> q; int x;
        q.push(S); dis[S] = 1;
        while(!q.empty())
        {
            x = q.front(), q.pop();
            for(int i = head[x]; i; i = e[i].next)
            {
                if(e[i].flow and !dis[e[i].to])
                {
                    dis[e[i].to] = dis[x]+1;
                    q.push(e[i].to);
                }
            }
        }
        return dis[T];
    }
    int dfs(int x, int flow)
    {
        if(x == T) return flow;
        int rest = flow, i;
        for(i = cur[x]; i; i = e[i].next)
        {
            if(e[i].flow and dis[e[i].to] == dis[x]+1)
            {
                int k = dfs(e[i].to, min(rest, e[i].flow));
                if(!k) dis[e[i].to] = -1;
                e[i].flow -= k;
                e[i^1].flow += k;
                if(!(rest -= k)) break;
            }
        }
        return cur[x] = i, flow-rest;
    }
    void add_edge(int a, int b, int f)
    {
        e[++ecnt] = {b, f, head[a]}; head[a] = ecnt;
        e[++ecnt] = {a, 0, head[b]}; head[b] = ecnt;
    }
    void clear()
    {
        memset(head, 0, sizeof(head));
        ecnt = 1;
    }
}


/*
3
2 10
0 0
0 0 0 0 0 0 0 0 0 0
ADA_JARVIS
ADA_SIVRAJ
2 10
0 0
0 0 0 0 0 0 0 0 0 0
ADA_JARVIS
ADA_SIVRAJ
2 10
0 0
0 0 0 0 0 0 0 0 0 0
ADA_JARVIS
ADA_SIVRAJ

*/

詳細信息

Test #1:

score: 10
Accepted
time: 10ms
memory: 5460kb

input:

64
9 9
0 0 0 0 1 0 0 0 0
0 0 0 0 0 -1 0 1 -1
BB_AC_BC_
BC_CC_CC_
_________
AB_AB_AC_
BC_BB_BC_
_________
AC_BC_BC_
CC_BC_BC_
_________
9 9
0 0 0 0 0 0 0 0 0
-1 0 0 0 0 0 0 0 0
AB_AA_AB_
BB_AA_BC_
_________
AB_AB_AC_
AB_BC_BC_
_________
AA_AB_AC_
AB_BB_AC_
_________
9 9
0 0 0 0 -1 0 0 0 0
0 -1 0 0 0 ...

output:

2
3
3
3
7
2
7
5
3
7
7
3
5
2
5
3
4
5
7
3
9
3
9
3
7
9
3
3
3
2
3
9
3
3
3
5
4
9
5
3
3
3
7
5
7
2
9
3
3
7
7
7
5
2
5
3
3
5
3
3
3
5
3
5

result:

ok 64 lines

Test #2:

score: 10
Accepted
time: 11ms
memory: 5468kb

input:

64
9 9
0 0 0 0 1 1 1 0 0
0 0 0 0 -1 0 0 0 -1
BD_AD_BC_
CD_DD_CD_
_________
AD_BD_BC_
BD_CD_BC_
_________
AD_AC_AC_
DD_AD_AC_
_________
9 9
0 0 -1 0 -1 0 0 0 -1
1 0 0 -1 0 -1 0 0 0
BC_BC_AC_
CD_CD_BD_
_________
CD_BC_BC_
DD_BC_CD_
_________
AD_AC_BB_
DD_CD_BD_
_________
9 9
0 0 0 1 0 0 0 0 0
0 1 0 -1...

output:

6
9
5
7
3
4
4
7
3
8
6
8
5
5
7
3
7
8
11
6
4
8
3
6
4
6
6
3
4
3
4
4
7
3
8
4
8
4
3
4
8
4
6
4
12
8
3
4
7
8
3
3
3
14
3
14
7
8
4
4
10
4
4
8

result:

ok 64 lines

Test #3:

score: 10
Accepted
time: 21ms
memory: 5324kb

input:

64
18 18
0 0 0 0 0 -1 0 0 1 0 0 0 0 0 0 0 0 0
0 -1 1 0 0 0 0 -1 0 1 0 0 0 1 0 0 0 1
AC_BD_FG_BE_AA_AG_
BE_CE_FG_CF_AD_GG_
__________________
BD_CC_BE_BE_BE_BE_
CF_CE_DE_CG_BG_CF_
__________________
BD_AE_AE_AC_CE_AF_
CG_EF_EG_CD_CF_BF_
__________________
CF_DE_BE_AB_AD_CF_
FF_DG_BG_BD_AF_EF_
_______...

output:

19
21
10
32
33
13
20
15
15
15
6
18
11
7
21
36
13
28
26
7
7
15
21
23
29
23
17
15
35
13
15
31
13
17
30
16
27
6
13
15
36
15
29
34
7
22
17
30
28
36
7
16
21
35
28
23
29
6
15
7
12
32
6
16

result:

ok 64 lines

Test #4:

score: 10
Accepted
time: 21ms
memory: 5328kb

input:

64
18 18
0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0
AG_EF_AF_AD_BE_BC_
BH_EF_FG_CH_CF_CE_
__________________
BE_AC_BC_EF_DD_AE_
DH_BH_BG_EH_DF_CH_
__________________
AE_AB_BB_AF_BE_EG_
DG_AE_BB_AF_EG_EH_
__________________
DE_AD_CD_BF_BG_AG_
EG_DE_CH_CG_EG_CG_
_________...

output:

16
13
20
11
15
31
14
16
8
41
16
15
30
21
43
35
27
8
26
21
23
38
33
23
17
5
17
16
29
6
7
5
15
38
17
16
34
16
11
37
26
16
16
23
20
6
30
7
35
7
10
6
33
18
42
39
13
22
23
7
7
41
6
14

result:

ok 64 lines

Test #5:

score: 10
Accepted
time: 21ms
memory: 5332kb

input:

64
24 24
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
ABC_AAC_ABC_AAB_AAB_AAB_
ACD_ABD_ABD_ABC_ABD_ABC_
CDD_BDD_BDD_ACD_ACD_ACD_
________________________
ABC_ABD_ABB_ABC_AAB_ACD_
BCD_ACD_ABD_ACD_ABD_BDD_
CCD_BDD_BCD_CCD_ABD_CDD_
___________________...

output:

7
3
32
28
18
9
4
12
3
11
18
16
28
23
25
20
24
10
3
11
19
10
18
22
10
8
12
4
16
16
26
12
18
25
13
21
19
23
18
24
12
4
12
16
22
30
4
10
15
24
16
30
25
25
3
15
20
16
4
19
22
12
4
14

result:

ok 64 lines

Test #6:

score: 10
Accepted
time: 20ms
memory: 5332kb

input:

64
24 24
-1 0 0 0 0 0 1 0 1 0 -1 0 -1 0 0 0 0 -1 0 1 0 1 -1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 1 1 -1 0 0
AAC_AAB_ABB_BBC_ABC_ACD_
ACD_ABC_ABE_BBE_BBE_CDD_
BDD_BBD_BBE_BDE_BCE_CDE_
________________________
ACD_ABC_ABD_ABC_ABD_ABB_
BDE_ACE_ACD_ABE_ADE_ABC_
CEE_CDE_CDD_BEE_CDE_BCC_
_____________...

output:

31
30
12
22
25
37
34
11
3
25
16
16
4
12
45
12
13
15
22
5
20
34
26
30
26
34
11
10
21
24
20
11
29
37
27
3
18
11
20
13
20
39
4
3
10
25
21
24
11
43
23
16
27
23
15
36
13
21
25
11
9
33
36
10

result:

ok 64 lines

Test #7:

score: 10
Accepted
time: 102ms
memory: 5480kb

input:

64
72 72
-1 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 -1 0 0 -1 0 1 0 0 0 0 0 0 0 -1 0 0 0 1 0 0 0 -1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 -1 0 0 -1 1 0 0 0 0 -1 0 0 0 0 -1 0 0 0 0 0 1 -1 0 0 0...

output:

182
350
392
176
293
247
106
224
98
248
96
297
350
224
220
258
266
258
150
280
63
247
364
138
375
229
198
136
320
108
210
284
254
256
164
296
326
321
198
272
292
318
98
346
314
273
266
133
194
286
340
264
330
204
385
406
326
267
265
208
256
205
144
226

result:

ok 64 lines

Test #8:

score: 10
Accepted
time: 42ms
memory: 5436kb

input:

64
72 72
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
0 1 0 0 0 -1 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 -1 0 0 ...

output:

47
57
37
53
41
55
61
49
59
49
51
63
55
17
59
55
47
49
39
59
57
61
45
61
59
43
59
55
49
57
57
51
41
59
59
59
23
57
63
57
47
57
53
55
61
37
61
57
53
49
61
59
55
59
55
59
61
51
61
53
61
63
55
57

result:

ok 64 lines

Test #9:

score: 10
Accepted
time: 53ms
memory: 5428kb

input:

64
72 72
0 0 -1 0 0 0 1 0 0 0 0 0 0 -1 0 0 0 1 -1 0 0 0 0 0 1 0 0 0 -1 0 0 -1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 -1 0 0 0 1 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0
0 -1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 -1 0 -1 0 0 0 0 0 1 0 0 0 -1 0 -1 0 0 0 0 0 0 0...

output:

88
78
78
96
72
62
82
84
96
78
86
90
65
76
84
66
96
60
82
62
84
66
90
78
84
88
74
74
98
88
58
90
93
72
74
58
44
90
60
94
86
82
96
68
88
69
90
66
82
98
61
62
76
58
94
56
78
66
60
58
76
76
70
78

result:

ok 64 lines

Test #10:

score: 10
Accepted
time: 50ms
memory: 5348kb

input:

64
70 70
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 -1 0 0 0 0 0 0 0 -1 0 0 0 0 -1 0 -1 -1 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 1 0 0 -1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 -1 0 0 0 0 0 0 0 0 1 0 0 -1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0
...

output:

25
27
27
23
21
27
25
25
23
21
23
23
25
25
25
17
27
21
21
17
23
23
25
23
23
23
23
19
25
25
27
25
15
25
25
27
17
25
21
23
25
25
25
23
23
23
27
25
25
21
27
23
25
25
21
19
25
19
21
27
23
21
27
25

result:

ok 64 lines