답안 #350476

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
350476 2021-01-19T06:08:19 Z arnold518 Raspad (COI17_raspad) C++14
0 / 100
2 ms 620 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 1000;
const int MAXM = 50;

int N, M;
int A[MAXN+10][MAXM+10];
vector<pii> R[MAXN+10];

struct UF
{
	int par[MAXM*2+10];
	void init()
	{
		for(int i=1; i<=M+M; i++) par[i]=i;
	}
	int Find(int x)
	{
		if(x==par[x]) return x;
		return par[x]=Find(par[x]);
	}
	void Union(int x, int y)
	{
		x=Find(x); y=Find(y);
		if(x==y) return;
		if(x>y) swap(x, y);
		par[x]=y;
	}
}uf;

ll ans=0;

int main()
{
	scanf("%d%d", &N, &M);
	for(int i=1; i<=N; i++) for(int j=1; j<=M; j++) scanf("%1d", &A[i][j]);

	for(int i=1; i<=N; i++)
	{
		R[i].push_back({0, 0});

		int l, r;
		int cnt=1;
		for(int j=1; j<=M; j++)
		{
			if(!A[i][j-1] && A[i][j]) l=j;
			if(A[i][j] && !A[i][j+1])
			{
				r=j;
				R[i].push_back({l, r});
				for(int k=l; k<=r; k++) A[i][k]=cnt;
				cnt++;
			}
		}
	}

	ll val=0;
	vector<pair<int, pii>> V;


	for(int i=1; i<=N; i++)
	{
		vector<pair<int, pii>> V2;

		//printf("=================== %d\n", i);
		

		uf.init();
		for(int k=1; k<R[i].size(); k++)
		{
			int l=R[i][k].first, r=R[i][k].second;
			vector<int> t;
			for(int j=l; j<=r; j++)
			{
				if(A[i-1][j]) t.push_back(A[i-1][j]);
			}
			t.erase(unique(t.begin(), t.end()), t.end());
			for(int j=1; j<t.size(); j++)
			{
				if(uf.Find(t[0])!=uf.Find(t[j]))
				{
					uf.Union(t[0], t[j]);
					val-=i-1;
				}
			}
		}

		for(auto it : V)
		{
			if(uf.Find(it.second.first)==uf.Find(it.second.second))
			{
				val+=it.first;
			}
			else
			{
				uf.Union(it.second.first, it.second.second);
			}
		}
		//printf("!%lld\n", val);

		val+=R[i].size()-1;
		for(int k=1; k<R[i].size(); k++)
		{
			int l=R[i][k].first, r=R[i][k].second;
			bool flag=true;
			for(int j=l; j<=r; j++)
			{
				if(A[i-1][j]) flag=false;
			}
			if(flag) val+=i-1;
		}
		if(i!=1)
		{

			uf.init();
			for(int k=1; k<R[i-1].size(); k++)
			{
				int l=R[i-1][k].first, r=R[i-1][k].second;
				vector<int> t;
				for(int j=l; j<=r; j++)
				{
					if(A[i][j]) t.push_back(A[i][j]);
				}
				t.erase(unique(t.begin(), t.end()), t.end());
				for(int j=0; j<t.size(); j++)
				{
					uf.Union(k, t[j]+M);
				}
				for(int j=1; j<t.size(); j++)
				{
					V2.push_back({i-1, {t[0], t[j]}});
				}
			}

			for(auto it : V)
			{
				if(uf.Find(it.second.first)!=uf.Find(it.second.second))
				{
					if(uf.Find(it.second.first)>M && uf.Find(it.second.second)>M)
					{
						V2.push_back({it.first, {it.second.first-M, it.second.second-M}});
						uf.Union(it.second.first, it.second.second);
					}
				}
			}
		}
		V=V2;
		ans+=val;
		//printf("%lld\n", val);
	}
	printf("%lld\n", ans);
}

Compilation message

raspad.cpp: In function 'int main()':
raspad.cpp:74:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |   for(int k=1; k<R[i].size(); k++)
      |                ~^~~~~~~~~~~~
raspad.cpp:83:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |    for(int j=1; j<t.size(); j++)
      |                 ~^~~~~~~~~
raspad.cpp:107:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |   for(int k=1; k<R[i].size(); k++)
      |                ~^~~~~~~~~~~~
raspad.cpp:121:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |    for(int k=1; k<R[i-1].size(); k++)
      |                 ~^~~~~~~~~~~~~~
raspad.cpp:130:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  130 |     for(int j=0; j<t.size(); j++)
      |                  ~^~~~~~~~~
raspad.cpp:134:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  134 |     for(int j=1; j<t.size(); j++)
      |                  ~^~~~~~~~~
raspad.cpp:40:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   40 |  scanf("%d%d", &N, &M);
      |  ~~~~~^~~~~~~~~~~~~~~~
raspad.cpp:41:55: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   41 |  for(int i=1; i<=N; i++) for(int j=1; j<=M; j++) scanf("%1d", &A[i][j]);
      |                                                  ~~~~~^~~~~~~~~~~~~~~~~
raspad.cpp:47:7: warning: 'l' may be used uninitialized in this function [-Wmaybe-uninitialized]
   47 |   int l, r;
      |       ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Runtime error 2 ms 620 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Runtime error 2 ms 620 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 620 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Runtime error 2 ms 620 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -