結果

問題 No.697 池の数はいくつか
ユーザー 👑 horiesinitihoriesiniti
提出日時 2018-06-11 03:16:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 853 ms / 6,000 ms
コード長 1,149 bytes
コンパイル時間 716 ms
コンパイル使用メモリ 82,724 KB
実行使用メモリ 18,564 KB
最終ジャッジ日時 2024-04-25 20:02:53
合計ジャッジ時間 7,739 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 1 ms
6,944 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 1 ms
6,944 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 96 ms
6,944 KB
testcase_25 AC 95 ms
6,944 KB
testcase_26 AC 96 ms
6,940 KB
testcase_27 AC 91 ms
6,940 KB
testcase_28 AC 93 ms
6,940 KB
testcase_29 AC 694 ms
6,940 KB
testcase_30 AC 853 ms
17,220 KB
testcase_31 AC 713 ms
6,944 KB
testcase_32 AC 838 ms
18,564 KB
testcase_33 AC 813 ms
16,672 KB
testcase_34 AC 832 ms
16,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <stdio.h>
#include <vector>
#include <string.h>
#include <set>

using namespace std;
vector<int> vec,vec2;
set<int> ss;

int f(int p){
	if(p==vec[p]){
		return p;
	}else{
		int p2=f(vec[p]);
		vec[p]=p2;
		return p2;
	}
}


int main() {
	// your code goes here
	int h,w;
	int ms[2][3001];
	int cs[2][3001];
	int c=0;
	memset(cs,0,sizeof(cs));
	memset(ms,0,sizeof(ms));
	scanf("%d %d",&h,&w);
	for(int y=0;y<h;y++){
		int now=y%2;
		int old=(y+1)%2;
		for(int x=0;x<w;x++){
			scanf("%d",&ms[now][x]);
		}
		for(int x=0;x<w;x++){
			if(ms[now][x]==0)continue;
			
			int ml,mu;
			if(x-1<0){
				ml=0;
			}else{
				ml=ms[now][x-1];
			}
			if(y-1<0){
				mu=0;
			}else{
				mu=ms[old][x];
			}
			if(ml==0&&mu==0){
				vec.push_back(c);
				vec2.push_back(0);
				cs[now][x]=c;
				c++;
			}else if(ml==0){
				cs[now][x]=cs[old][x];
			}else if(mu==0){
				cs[now][x]=cs[now][x-1];
			}else{
				cs[now][x]=cs[old][x];
				vec[f(cs[now][x-1])]=f(cs[old][x]);
			}
		}
	}
	for(int i=0;i<vec.size();i++){
		vec2[f(i)]=1;
	}
	int ans=0;
	for(int i=0;i<vec.size();i++){
		ans+=vec2[i];
	}
	printf("%d\n",ans);
	return 0;
}
0