UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#205043#3648. T1nullptr_qwq1002673ms2376kbC++113.1kb2024-06-22 10:08:382024-06-22 13:41:44

answer

// 私は猫です

#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define mkp make_pair
#define fi first
#define se second
#define inf 1000000000
#define infll 1000000000000000000ll
#define pii pair<int,int>
#define rep(i,a,b,c) for(int i=(a);i<=(b);i+=(c))
#define per(i,a,b,c) for(int i=(a);i>=(b);i-=(c))
#define F(i,a,b) for(int i=(a);i<=(b);i++)
#define dF(i,a,b) for(int i=(a);i>=(b);i--)
#define wh(lzm) while(lzm--)
#define cmh(sjy) while(sjy--)
#define lowbit(x) (x&(-x))
#define HH printf("\n")
#define eb emplace_back
using namespace std;
ll read(){
	ll x=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-') f=-1;c=getchar();}
	while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar();
	return x*f;
}
const int mod=998244353,maxn=200005;
inline void chkmax(int &x,int y){ x=max(x,y); }
inline void chkmin(int &x,int y){ x=min(x,y); }
int n,m,id[105][105];
char a[105][105];
namespace flow{
	int tot=1,to[maxn],w[maxn],hd[maxn],nxt[maxn],N;
	void add(int u,int v,int x){
	    to[++tot]=v,w[tot]=x,nxt[tot]=hd[u],hd[u]=tot;
	}
	void addedge(int u,int v,int x){
		add(u,v,x),add(v,u,0);
	}
	int now[maxn],d[maxn],S,T;
	int bfs(){
	    queue<int>q; q.push(S);
	    F(i,1,N) d[i]=inf;
	    d[S]=0,now[S]=hd[S];
	    while(!q.empty()){
	        int u=q.front();
	        q.pop();
	        for(int i=hd[u];i;i=nxt[i]){
	            int v=to[i],x=w[i];
	            if(x>0&&d[v]>=inf){
	                q.push(v);
	                now[v]=hd[v],d[v]=d[u]+1;
	                if(v==T) return 1;
	            }
	        }
	    }
	    return 0;
	}
	int dfs(int u,int sum){
	    if(u==T||!sum) return sum;
	    int k=0,fl=0;
	    for(int i=now[u];i;i=nxt[i]){
	        now[u]=i;
	        int v=to[i],x=w[i];
	        if(x>0&&d[v]==d[u]+1){
	            k=dfs(v,min(sum-fl,x));
	            w[i]-=k;
	            w[i^1]+=k;
	            fl+=k;
	            if(fl==sum) return fl;
	        }
	    }
	    if(!fl) d[u]=inf;
	    return fl;
	}
	void clear(){
		F(i,0,N) now[i]=hd[i]=0;
		F(i,1,tot) nxt[i]=w[i]=to[i]=0;
		tot=1;
	}
	int dinic(){
	    int ans=0;
	    for(;bfs();){
	    	for(;;){
	    		int x=dfs(S,inf);
	    		if(!x) break;
	    		ans+=x;
	    	}
	    }
	    return ans;	
	}
	void check(){
		F(i,1,N) for(int j=hd[i];j;j=nxt[j]) cout<<i<<' '<<to[j]<<' '<<w[j]<<endl;
	}
}
using namespace flow;
void solve(){
	n=read(),m=read(),N=0;
	F(i,0,n+1) scanf("%s",a[i]+1);
	F(i,0,n+1) F(j,1,m) id[i][j]=++N;
	S=++N,T=++N,clear();
	F(i,1,m) if(a[0][i]=='*') addedge(S,id[0][i],1);
	F(i,1,m) if(a[n+1][i]=='*') addedge(id[n+1][i],T,1);
	F(i,1,n) F(j,1,m) if(a[i][j]=='.'){
		if(j!=m&&a[i][j+1]=='.') addedge(id[i][j],id[i][j+1],1),addedge(id[i][j+1],id[i][j],1);
		if(i!=n&&a[i+1][j]=='.') addedge(id[i][j],id[i+1][j],1),addedge(id[i+1][j],id[i][j],1); 
	}
	F(i,1,m) if(a[0][i]=='*'&&a[1][i]=='.') addedge(id[0][i],id[1][i],1);
	F(i,1,m) if(a[n+1][i]=='*'&&a[n][i]=='.') addedge(id[n][i],id[n+1][i],1);
	printf("%d\n",dinic());
}
signed main(){
	int sjy=read();
	cmh(sjy) solve();
}

Details

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 5
Accepted

Test #1:

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

input:

100
4 3
*.*
...
...
...
...
*..
3 4
..*.
....
..#.
.##.
**..
3 4
*.**
....
#...
...#
*..*
3 3
***
.....

output:

1
1
1
0
1
1
1
1
0
3
1
2
0
3
0
0
1
1
0
2
0
1
1
1
0
1
0
1
1
0
1
1
1
0
0
3
1
1
2
1
1
0
1
1
1
1
2
1
3
0
...

result:

ok 100 numbers

Subtask #2:

score: 5
Accepted

Test #2:

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

input:

100
4 4
*...
....
....
..#.
....
*.*.
4 4
..**
#...
....
###.
##..
**..
4 3
*.*
...
...
...
..#
.*.
...

output:

1
0
1
1
0
1
1
1
3
1
1
1
0
0
2
1
2
0
1
1
0
2
1
3
3
2
1
2
1
2
1
0
1
1
1
0
0
1
1
2
2
1
2
0
2
0
0
0
1
0
...

result:

ok 100 numbers

Subtask #3:

score: 5
Accepted

Test #3:

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

input:

100
4 4
**.*
....
....
....
#.#.
.*..
4 3
*..
...
...
...
...
.*.
3 4
***.
....
....
.#.#
....
3 4
*...

output:

1
1
0
1
1
0
1
2
0
1
0
2
0
1
2
0
1
1
1
0
0
1
1
1
0
1
0
1
0
2
1
2
0
2
2
0
2
1
1
2
1
1
1
2
2
2
2
1
1
0
...

result:

ok 100 numbers

Subtask #4:

score: 5
Accepted

Test #4:

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

input:

100
4 3
*.*
...
...
...
...
***
3 4
.*.*
###.
#...
#.#.
*..*
4 4
****
....
....
....
....
***.
4 4
....

output:

2
1
3
1
1
1
1
1
1
2
1
2
1
0
1
2
3
0
2
1
1
1
1
1
2
1
1
1
0
1
1
0
1
2
1
2
3
1
1
0
1
2
2
1
0
1
2
3
2
2
...

result:

ok 100 numbers

Subtask #5:

score: 5
Accepted

Test #5:

score: 5
Accepted
time: 50ms
memory: 2368kb

input:

100
100 100
...................................*.......................................................

output:

1
1
1
1
1
1
1
1
1
1
1
0
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
0
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
0
...

result:

ok 100 numbers

Subtask #6:

score: 5
Accepted

Test #6:

score: 5
Accepted
time: 55ms
memory: 2372kb

input:

100
100 100
.......*...................................................................................

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

ok 100 numbers

Subtask #7:

score: 5
Accepted

Test #7:

score: 5
Accepted
time: 71ms
memory: 2372kb

input:

100
100 100
.***..*.*..******.*.......********.**..*...***..****.*..***.**....**.*..*.**.****.*****....

output:

1
0
0
1
0
1
1
0
0
0
1
0
1
0
0
1
1
0
1
1
0
0
0
0
0
1
0
1
0
0
1
1
0
0
1
1
0
0
1
0
0
0
1
1
1
1
0
1
0
0
...

result:

ok 100 numbers

Subtask #8:

score: 5
Accepted

Test #8:

score: 5
Accepted
time: 76ms
memory: 2372kb

input:

100
100 100
*.....**..**...*...**.*...*....*.****..**..*.....****..*****....***...**.*..*..****..*.*...

output:

1
1
0
1
1
0
1
0
0
0
0
0
0
0
0
0
0
0
1
0
1
1
1
1
0
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
1
0
0
1
...

result:

ok 100 numbers

Subtask #9:

score: 5
Accepted

Test #9:

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

input:

20
10 10
.*.**.**..
..........
...#......
..........
.........#
..#....#..
..........
...#.#....
..#...

output:

3
2
3
4
2
2
4
2
5
3
3
5
3
4
3
3
3
4
3
3

result:

ok 20 numbers

Subtask #10:

score: 5
Accepted

Test #10:

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

input:

20
10 10
..*..***..
..#..#....
#.........
.#........
..#.......
.....#..#.
......#...
.........#
......

output:

2
4
3
4
4
4
3
4
3
5
3
5
3
4
4
5
5
3
5
2

result:

ok 20 numbers

Subtask #11:

score: 5
Accepted

Test #11:

score: 5
Accepted
time: 1ms
memory: 1268kb

input:

20
10 10
.*..*....*
..........
..........
..........
..#.#.....
..........
.....#....
..........
......

output:

3
4
3
4
3
2
3
5
2
5
2
4
4
3
2
3
3
4
4
1

result:

ok 20 numbers

Subtask #12:

score: 5
Accepted

Test #12:

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

input:

20
10 10
*..*.*.*..
..........
...#.#....
...#......
.#.#......
..........
..........
..........
......

output:

4
4
4
6
4
2
5
4
5
4
1
1
3
2
2
5
3
3
2
4

result:

ok 20 numbers

Subtask #13:

score: 5
Accepted

Test #13:

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

input:

20
10 10
*.*.*..***
..........
.......#..
.........#
#.#.......
..#......#
........#.
..#.......
..#...

output:

5
5
4
2
5
3
5
5
3
4
1
3
3
4
5
4
5
4
1
6

result:

ok 20 numbers

Subtask #14:

score: 5
Accepted

Test #14:

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

input:

20
10 10
.****...**
.......#..
.#....#...
..#......#
.....#....
......#.#.
.....#....
..........
......

output:

4
3
3
4
5
3
5
4
2
4
3
4
3
6
5
2
5
1
6
3

result:

ok 20 numbers

Subtask #15:

score: 5
Accepted

Test #15:

score: 5
Accepted
time: 427ms
memory: 2368kb

input:

100
100 100
**.*..**..**.*.*.*....***.**.*.*..*...**.*...*.......**.***..**...*.*.***..**.**.**........

output:

42
38
12
7
10
38
48
10
7
43
5
49
39
32
30
1
6
21
17
4
25
11
22
41
1
40
14
23
6
51
45
19
6
15
4
38
52...

result:

ok 100 numbers

Subtask #16:

score: 5
Accepted

Test #16:

score: 5
Accepted
time: 408ms
memory: 2360kb

input:

100
99 100
.*......*.....*.**.**.**....*.****.**...*.**..*****.*.*..*.*.*.*..****..*******...*.........

output:

24
46
5
45
43
18
45
24
30
47
44
7
8
14
43
3
1
31
9
3
18
30
10
21
3
46
44
36
3
16
36
49
3
0
2
43
9
2
...

result:

ok 100 numbers

Subtask #17:

score: 5
Accepted

Test #17:

score: 5
Accepted
time: 402ms
memory: 2372kb

input:

100
100 100
..*..*.*.**....*..**..*.*.**......*.***..*..*.*...*.*.*...***.**...*****.*..**.*.*.*.**....

output:

44
44
42
4
36
53
2
22
16
5
34
4
14
16
23
46
2
50
2
51
43
1
13
37
8
41
27
3
7
20
52
2
14
13
45
19
13
...

result:

ok 100 numbers

Subtask #18:

score: 5
Accepted

Test #18:

score: 5
Accepted
time: 419ms
memory: 2376kb

input:

100
100 99
**...*..**.****..**.....***....**.**.*.**...**.*..*..***..****......*.*..**..**.**.*.*.*....

output:

2
2
45
53
50
20
51
13
4
11
26
16
49
2
4
15
49
1
44
29
34
46
3
50
10
42
8
23
1
2
49
50
45
30
6
26
20
...

result:

ok 100 numbers

Subtask #19:

score: 5
Accepted

Test #19:

score: 5
Accepted
time: 395ms
memory: 2364kb

input:

100
100 100
*.*..**.***.*.**.***.*.*....**..**.....****.*.**...........*...*..*..*.*.***.*..*..*..**...

output:

2
26
17
9
48
29
2
37
29
42
24
2
0
7
46
16
46
16
29
50
19
45
0
10
4
49
4
8
54
3
35
2
44
29
44
8
1
45
...

result:

ok 100 numbers

Subtask #20:

score: 5
Accepted

Test #20:

score: 5
Accepted
time: 369ms
memory: 2368kb

input:

100
100 100
...*..****..**...*..*.....***....**.*.....**.**..*..**.***......***..**.*.***...*...*.*....

output:

43
23
6
3
48
46
5
44
1
27
5
44
3
2
2
44
5
9
54
13
52
13
26
6
8
42
3
6
4
4
1
51
6
5
8
48
1
41
6
43
5
...

result:

ok 100 numbers