Submission #50072514
Source Code Expand
#include <bits/stdc++.h>
#include <atcoder/modint>
const int MOD=998244353;
using mint = atcoder::static_modint<MOD>;
using namespace std;
#define rep(i,a,n) for (int i=a;i<(int)(n);i++)
int read(){int r;scanf("%d",&r);return r;}
const int N=1010,M=1000010;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};
int h,w;
int dfn[N][N],low[N][N];
int f[N][N]; // 相连的 点-双连通分量 数量 -1
int cnt[M]; // 单连通块[b]大小
int bl[N][N]; // bl[x][y] = 单连通块id
char s[N][N];
void tarjan(int x,int y,int fx,int fy,int b,int dep=1){
low[x][y]=dfn[x][y]=dep; // 有向图 ++tick;, 无向图可以直接dep
cnt[bl[x][y]=b]++;
rep(i,0,4) {
int nx=x+dx[i];
int ny=y+dy[i];
if(nx<1||nx>h||ny<1||ny>w||s[nx][ny]=='.') continue;
if(!dfn[nx][ny]){
tarjan(nx,ny,x,y,b,dep+1);
low[x][y]=min(low[x][y],low[nx][ny]);
if(low[nx][ny]>=dfn[x][y]) f[x][y]++;
}else if(nx != fx or ny != fy){ // 非回边
low[x][y]=min(low[x][y],dfn[nx][ny]); // 这里不是low 是dfn, 同样是8字形的问题
}
}
if(x==fx and y==fy) f[x][y]--; // root
}
int main(){
h=read();
w=read();
rep(i,1,h+1) scanf("%s",s[i]+1);
int t=0;
rep(i,1,h+1) rep(j,1,w+1) if(s[i][j]=='#' && !dfn[i][j]) tarjan(i,j,i,j,++t);
mint a1=0;
mint a2=0;
rep(i,1,h+1) rep(j,1,w+1) if(s[i][j]=='#'){
a1 ++;
a2 += (cnt[bl[i][j]]==1)?-1:f[i][j];
}
printf("%d\n",(a2*a1.pow(MOD-2)+t).val()); // a1可能为0 不要inv()
return 0;
}
Submission Info
Submission Time
2024-02-07 13:16:05+0900
Task
G - Christmas Color Grid 2
User
cromarmot
Language
C++ 20 (gcc 12.2)
Score
650
Code Size
1480 Byte
Status
AC
Exec Time
98 ms
Memory
110488 KiB
Compile Error
Main.cpp: In function ‘int read()’:
Main.cpp:7:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
7 | int read(){int r;scanf("%d",&r);return r;}
| ~~~~~^~~~~~~~~
Main.cpp: In function ‘int main()’:
Main.cpp:37:21: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
37 | rep(i,1,h+1) scanf("%s",s[i]+1);
| ~~~~~^~~~~~~~~~~~~
Judge Result
Set Name
Sample
All
Score / Max Score
0 / 0
650 / 650
Status
Set Name
Test Cases
Sample
sample00.txt, sample01.txt, sample02.txt
All
sample00.txt, sample01.txt, sample02.txt, testcase00.txt, testcase01.txt, testcase02.txt, testcase03.txt, testcase04.txt, testcase05.txt, testcase06.txt, testcase07.txt, testcase08.txt, testcase09.txt, testcase10.txt, testcase11.txt, testcase12.txt, testcase13.txt, testcase14.txt, testcase15.txt, testcase16.txt, testcase17.txt, testcase18.txt, testcase19.txt, testcase20.txt, testcase21.txt, testcase22.txt, testcase23.txt, testcase24.txt, testcase25.txt, testcase26.txt, testcase27.txt, testcase28.txt, testcase29.txt, testcase30.txt
Case Name
Status
Exec Time
Memory
sample00.txt
AC
1 ms
3700 KiB
sample01.txt
AC
1 ms
3716 KiB
sample02.txt
AC
1 ms
3964 KiB
testcase00.txt
AC
14 ms
20684 KiB
testcase01.txt
AC
16 ms
22428 KiB
testcase02.txt
AC
4 ms
4628 KiB
testcase03.txt
AC
4 ms
4708 KiB
testcase04.txt
AC
39 ms
61456 KiB
testcase05.txt
AC
42 ms
67320 KiB
testcase06.txt
AC
10 ms
20184 KiB
testcase07.txt
AC
11 ms
20656 KiB
testcase08.txt
AC
11 ms
20024 KiB
testcase09.txt
AC
11 ms
20800 KiB
testcase10.txt
AC
12 ms
19700 KiB
testcase11.txt
AC
13 ms
20720 KiB
testcase12.txt
AC
14 ms
19664 KiB
testcase13.txt
AC
15 ms
20736 KiB
testcase14.txt
AC
17 ms
20260 KiB
testcase15.txt
AC
18 ms
21076 KiB
testcase16.txt
AC
19 ms
19792 KiB
testcase17.txt
AC
20 ms
21084 KiB
testcase18.txt
AC
21 ms
19884 KiB
testcase19.txt
AC
24 ms
21004 KiB
testcase20.txt
AC
27 ms
20332 KiB
testcase21.txt
AC
29 ms
20996 KiB
testcase22.txt
AC
33 ms
20540 KiB
testcase23.txt
AC
35 ms
20844 KiB
testcase24.txt
AC
40 ms
22380 KiB
testcase25.txt
AC
43 ms
22760 KiB
testcase26.txt
AC
53 ms
40016 KiB
testcase27.txt
AC
59 ms
43052 KiB
testcase28.txt
AC
57 ms
51696 KiB
testcase29.txt
AC
68 ms
58668 KiB
testcase30.txt
AC
98 ms
110488 KiB