Submission #205982

# Submission time Handle Problem Language Result Execution time Memory
205982 2020-03-01T15:57:13 Z dennisstar Virus Experiment (JOI19_virus) C++17
100 / 100
152 ms 17692 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define hsh(x, y) (((x)-1)*C+(y))
using namespace std;
typedef pair<int, int> pii;

const int xx[]={-1, 0, 1, 0}, yy[]={0, 1, 0, -1};

int M, R, C, U[805][805], S[16], v; string D;
int chk[805][805], A[805][805];

int pr[640005], Do[640005];
int get(int x) { return pr[x]?(pr[x]=get(pr[x])):x; }
void Un(int x, int y) {
	if (Do[y]) { Do[x]=1; return ; }
	x=get(x), y=get(y);
	if (x!=y) pr[y]=x;
}

pii stk[640005]; int tp;
bool bfs(int x, int y) {
	tp=0; chk[x][y]=v, stk[tp++]=pii(x, y);
	for (int i=0; i<tp; i++) {
		int a, b; tie(a, b)=stk[i];
		for (int j=0; j<4; j++) {
			int aa=a+xx[j], bb=b+yy[j], w=0;
			if (!U[aa][bb]||chk[aa][bb]==v) continue;
			for (int k=0; k<4; k++) if (chk[aa+xx[k]][bb+yy[k]]==v) w|=(1<<k);
			if (S[w]<U[aa][bb]) continue;
			if (hsh(x, y)!=get(hsh(aa, bb))) { Un(get(hsh(aa, bb)), hsh(x, y)); return 1; }
			chk[aa][bb]=v, stk[tp++]=pii(aa, bb);
		}
	}
	for (int i=0; i<tp; i++) A[stk[i].fi][stk[i].se]=tp;
	Do[hsh(x, y)]=1;
	return 0;
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin>>M>>R>>C>>D;
	for (int i=0; i<M; i++) D[i]=(D[i]=='N'?0:D[i]=='S'?2:D[i]=='E'?1:3);
	for (int i=1; i<=R; i++) for (int j=1; j<=C; j++) cin>>U[i][j];
	D+=D, M*=2; 
	for (int i=1; i<16; i++) for (int j=0, k; j<M; j++) {
		if ((i&(1<<D[j]))!=(1<<D[j])) continue;
		for (k=j; k<M; k++) if ((i&(1<<D[k]))!=(1<<D[k])) break;
		S[i]=max(S[i], k-j); j=k-1;
	}
	for (int i=1; i<16; i++) if (S[i]==M) S[i]=1e9;
	
	memset(A, 0x3f, sizeof(A));
	while (1) {
		bool fl=0;
		for (int i=1; i<=R; i++) for (int j=1; j<=C; j++)
			if (U[i][j]&&!Do[hsh(i, j)]&&get(hsh(i, j))==hsh(i, j)) v++, fl|=bfs(i, j);
		if (!fl) break;
	}

	int A1=1e9, A2=0;
	for (int i=1; i<=R; i++) for (int j=1; j<=C; j++) {
		if (!U[i][j]) continue;
		if (A1>A[i][j]) A1=A[i][j], A2=0;
		if (A1==A[i][j]) A2++;
	}
	cout<<A1<<'\n'<<A2<<'\n';
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 3192 KB Output is correct
2 Correct 82 ms 14200 KB Output is correct
3 Correct 101 ms 16888 KB Output is correct
4 Correct 80 ms 14200 KB Output is correct
5 Correct 108 ms 14496 KB Output is correct
6 Correct 8 ms 6776 KB Output is correct
7 Correct 116 ms 14596 KB Output is correct
8 Correct 59 ms 8988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2936 KB Output is correct
2 Correct 10 ms 3228 KB Output is correct
3 Correct 22 ms 3356 KB Output is correct
4 Correct 10 ms 3228 KB Output is correct
5 Correct 23 ms 3228 KB Output is correct
6 Correct 22 ms 3612 KB Output is correct
7 Correct 6 ms 2936 KB Output is correct
8 Correct 22 ms 3484 KB Output is correct
9 Correct 7 ms 3192 KB Output is correct
10 Correct 10 ms 3356 KB Output is correct
11 Correct 6 ms 3192 KB Output is correct
12 Correct 6 ms 3192 KB Output is correct
13 Correct 21 ms 3484 KB Output is correct
14 Correct 21 ms 3484 KB Output is correct
15 Correct 23 ms 3612 KB Output is correct
16 Correct 21 ms 3612 KB Output is correct
17 Correct 15 ms 3448 KB Output is correct
18 Correct 7 ms 3192 KB Output is correct
19 Correct 22 ms 3484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 3192 KB Output is correct
2 Correct 82 ms 14200 KB Output is correct
3 Correct 101 ms 16888 KB Output is correct
4 Correct 80 ms 14200 KB Output is correct
5 Correct 108 ms 14496 KB Output is correct
6 Correct 8 ms 6776 KB Output is correct
7 Correct 116 ms 14596 KB Output is correct
8 Correct 59 ms 8988 KB Output is correct
9 Correct 6 ms 2936 KB Output is correct
10 Correct 10 ms 3228 KB Output is correct
11 Correct 22 ms 3356 KB Output is correct
12 Correct 10 ms 3228 KB Output is correct
13 Correct 23 ms 3228 KB Output is correct
14 Correct 22 ms 3612 KB Output is correct
15 Correct 6 ms 2936 KB Output is correct
16 Correct 22 ms 3484 KB Output is correct
17 Correct 7 ms 3192 KB Output is correct
18 Correct 10 ms 3356 KB Output is correct
19 Correct 6 ms 3192 KB Output is correct
20 Correct 6 ms 3192 KB Output is correct
21 Correct 21 ms 3484 KB Output is correct
22 Correct 21 ms 3484 KB Output is correct
23 Correct 23 ms 3612 KB Output is correct
24 Correct 21 ms 3612 KB Output is correct
25 Correct 15 ms 3448 KB Output is correct
26 Correct 7 ms 3192 KB Output is correct
27 Correct 22 ms 3484 KB Output is correct
28 Correct 118 ms 17180 KB Output is correct
29 Correct 77 ms 12088 KB Output is correct
30 Correct 85 ms 12188 KB Output is correct
31 Correct 116 ms 12844 KB Output is correct
32 Correct 103 ms 12412 KB Output is correct
33 Correct 85 ms 14200 KB Output is correct
34 Correct 152 ms 16760 KB Output is correct
35 Correct 85 ms 13560 KB Output is correct
36 Correct 132 ms 13560 KB Output is correct
37 Correct 111 ms 13688 KB Output is correct
38 Correct 98 ms 16736 KB Output is correct
39 Correct 125 ms 15644 KB Output is correct
40 Correct 131 ms 15640 KB Output is correct
41 Correct 76 ms 14584 KB Output is correct
42 Correct 120 ms 17692 KB Output is correct
43 Correct 84 ms 14364 KB Output is correct
44 Correct 61 ms 8988 KB Output is correct