Submission #31224

#TimeUsernameProblemLanguageResultExecution timeMemory
31224gs14004Young Zebra (KRIII5_YZ)C++14
7 / 7
123 ms63284 KiB
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;
typedef long long lint;
const int MAXN = 1005;
const int mod = 1e9 + 7;

struct edg{int pos, d1, d2;};

int n, m;
char buf[MAXN][MAXN];
int ans[MAXN][MAXN];
vector<edg> gph[MAXN * MAXN];

int get(int x, int y){ return x * m + y; }

vector<int> v;
int vis[MAXN * MAXN], bad;
pi tr[MAXN * MAXN];

void dfs(int x, int p, int q){
	if(vis[x]){
		if(tr[x] != pi(p, q)){
			bad = 1;
		}
		return;
	}
	vis[x] = 1;
	tr[x] = pi(p, q);
	v.push_back(x);
	for(auto &i : gph[x]){
		dfs(i.pos, p + i.d1, q + i.d2);
	}
}

int main(){
	scanf("%d %d",&n,&m);
	for(int i=0; i<n; i++) scanf("%s", buf[i]);
	for(int i=0; i<n; i++){
		for(int j=0; j<m; j++){
			if(buf[i][j] == buf[(i+1)%n][j]){
				int cur = get(i, j);
				int nxt = get((i+1)%n, j);
				gph[cur].push_back({nxt, (i + 1 == n ? 1 : 0), 0});
				gph[nxt].push_back({cur, (i + 1 == n ? -1 : 0), 0});
			}
			if(buf[i][j] == buf[i][(j+1)%m]){
				int cur = get(i, j);
				int nxt = get(i, (j+1)%m);
				gph[cur].push_back({nxt, (j + 1 == m ? 1 : 0), 0});
				gph[nxt].push_back({cur, (j + 1 == m ? -1: 0), 0});
			}
		}
	}
	for(int i=0; i<n*m; i++){
		if(!vis[i]){
			bad = 0;
			v.clear();
			dfs(i, 0, 0);
			if(bad){
				for(auto &j : v) ans[j/m][j%m] = -1;
			}
			else{
				for(auto &j : v) ans[j/m][j%m] = v.size();
			}
		}
	}
	for(int i=0; i<n; i++){
		for(int j=0; j<m; j++){
			printf("%d ", ans[i][j]);
		}
		puts("");
	}
}

Compilation message (stderr)

YZ.cpp: In function 'int main()':
YZ.cpp:37:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&n,&m);
                      ^
YZ.cpp:38:44: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=0; i<n; i++) scanf("%s", buf[i]);
                                            ^
#Verdict Execution timeMemoryGrader output
Fetching results...