Submission #20163715


Source Code Expand

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<bitset>
#include<deque>
#include<cstdlib>
#include<set>
#include<ctime>
#define ll long long
#define mp make_pair
using namespace std;
ll read()
{
	ll x=0,f=1;
	char c=getchar();
	while(c>'9'||c<'0')
	{
		if(c=='-') f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9')
	{
		x=x*10+c-'0';
		c=getchar();
	}
	return f*x;
}
int n,m,fa[1002],ans,st[1002],top,ans2;
char s[1002][1002],c[1002][1002];
bool b[1002];
int getfa(int x)
{
	return x==fa[x]?x:fa[x]=getfa(fa[x]);
}
int main()
{
	n=read(),m=read();
	ans=m-2;
	for(int i=1;i<=m;++i) fa[i]=i;
	for(int i=1;i<=n;++i)
	{
		scanf("%s",s[i]+1); 
	 } 
	b[1]=b[m]=1;
	fa[1]=fa[m]=0;
	for(int i=2;i<m;++i)
	{
		if(s[1][i]=='#'||s[n][i]=='#') b[i]=1,ans--,fa[i]=0;
	}
 
	for(int i=2;i<n;++i)
	{
		top=-1;
		for(int j=1;j<=m;++j) if(s[i][j]=='#')
		{
			if(top==-1) top=getfa(j);
			else
			{
				int tmp=getfa(j);
				if(tmp!=top) fa[tmp]=top,ans--;
			}
		}
	}
	
	
	for(int i=1;i<=n;++i) 
	{
		for(int j=1;j<=m;++j) 
		{
			c[j][i]=s[i][j];
		}
	}
	swap(n,m);
	ans2=m-2;
	for(int i=0;i<=m;++i) fa[i]=i,b[i]=0;
	b[1]=b[m]=1;
	fa[1]=fa[m]=0;
	for(int i=2;i<m;++i)
	{
		if(c[1][i]=='#'||c[n][i]=='#') b[i]=1,ans2--,fa[i]=0;
	}
 
	for(int i=2;i<n;++i)
	{
		top=-1;
		for(int j=1;j<=m;++j) if(c[i][j]=='#')
		{
			if(top==-1) top=getfa(j);
			else
			{
				int tmp=getfa(j);
				if(tmp!=top) fa[tmp]=top,ans2--;
			}
		}
	}
	
	cout<<min(ans,ans2)<<'\n';
	return 0;
 }

Submission Info

Submission Time
Task D - Skate
User MTS_zx
Language C++ (Clang 10.0.0)
Score 600
Code Size 1649 Byte
Status AC
Exec Time 22 ms
Memory 5172 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 2
AC × 37
Set Name Test Cases
Sample sample.txt, sample_2.txt
All complete_1000_1000.txt, complete_1000_1000_1.txt, complete_1000_1000_10.txt, complete_1000_1000_100.txt, complete_1000_1000_1000.txt, complete_1000_1000_3.txt, complete_1000_1000_333.txt, complete_1000_1000_5.txt, complete_1000_1000_7.txt, horizontal_820_2_1.txt, horizontal_86_2_2.txt, horizontal_892_2_0.txt, random_1000_1000.txt, random_1000_1000_2.txt, random_1000_1000_3.txt, random_1000_1000_4.txt, random_1000_1000_5.txt, random_391_910.txt, random_423_172.txt, random_45_809.txt, random_461_111.txt, random_740_398.txt, sample.txt, sample_2.txt, sparse_261_351.txt, sparse_292_662.txt, sparse_372_354.txt, sparse_381_830.txt, sparse_432_398.txt, sparse_626_931.txt, sparse_73_682.txt, sparse_801_149.txt, sparse_820_852.txt, sparse_85_950.txt, vertical_2_113_1.txt, vertical_2_304_2.txt, vertical_2_710_0.txt
Case Name Status Exec Time Memory
complete_1000_1000.txt AC 21 ms 5064 KiB
complete_1000_1000_1.txt AC 15 ms 5056 KiB
complete_1000_1000_10.txt AC 14 ms 5028 KiB
complete_1000_1000_100.txt AC 13 ms 5168 KiB
complete_1000_1000_1000.txt AC 12 ms 5064 KiB
complete_1000_1000_3.txt AC 22 ms 5116 KiB
complete_1000_1000_333.txt AC 13 ms 5108 KiB
complete_1000_1000_5.txt AC 14 ms 5144 KiB
complete_1000_1000_7.txt AC 21 ms 5048 KiB
horizontal_820_2_1.txt AC 2 ms 4084 KiB
horizontal_86_2_2.txt AC 3 ms 3196 KiB
horizontal_892_2_0.txt AC 3 ms 3932 KiB
random_1000_1000.txt AC 15 ms 5160 KiB
random_1000_1000_2.txt AC 14 ms 5172 KiB
random_1000_1000_3.txt AC 12 ms 5024 KiB
random_1000_1000_4.txt AC 17 ms 5148 KiB
random_1000_1000_5.txt AC 15 ms 4992 KiB
random_391_910.txt AC 8 ms 4340 KiB
random_423_172.txt AC 2 ms 3800 KiB
random_45_809.txt AC 8 ms 3924 KiB
random_461_111.txt AC 5 ms 3632 KiB
random_740_398.txt AC 7 ms 4176 KiB
sample.txt AC 2 ms 3124 KiB
sample_2.txt AC 2 ms 3056 KiB
sparse_261_351.txt AC 3 ms 3792 KiB
sparse_292_662.txt AC 3 ms 4144 KiB
sparse_372_354.txt AC 5 ms 3820 KiB
sparse_381_830.txt AC 6 ms 4408 KiB
sparse_432_398.txt AC 4 ms 4068 KiB
sparse_626_931.txt AC 8 ms 4680 KiB
sparse_73_682.txt AC 3 ms 3904 KiB
sparse_801_149.txt AC 4 ms 4084 KiB
sparse_820_852.txt AC 13 ms 4892 KiB
sparse_85_950.txt AC 4 ms 4052 KiB
vertical_2_113_1.txt AC 2 ms 3208 KiB
vertical_2_304_2.txt AC 2 ms 3336 KiB
vertical_2_710_0.txt AC 2 ms 3916 KiB