Submission #31224

# Submission time Handle Problem Language Result Execution time Memory
31224 2017-08-14T13:39:42 Z gs14004 Young Zebra (KRIII5_YZ) C++14
7 / 7
123 ms 63284 KB
#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

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 time Memory Grader output
1 Correct 99 ms 59408 KB Output is correct
2 Correct 73 ms 54200 KB Output is correct
3 Correct 89 ms 59080 KB Output is correct
4 Correct 76 ms 54032 KB Output is correct
5 Correct 96 ms 54016 KB Output is correct
6 Correct 79 ms 53256 KB Output is correct
7 Correct 106 ms 56828 KB Output is correct
8 Correct 69 ms 56376 KB Output is correct
9 Correct 103 ms 55452 KB Output is correct
10 Correct 63 ms 56204 KB Output is correct
11 Correct 116 ms 60148 KB Output is correct
12 Correct 96 ms 63284 KB Output is correct
13 Correct 93 ms 54344 KB Output is correct
14 Correct 99 ms 54656 KB Output is correct
15 Correct 123 ms 54732 KB Output is correct
16 Correct 86 ms 53112 KB Output is correct
17 Correct 53 ms 52984 KB Output is correct
18 Correct 43 ms 52964 KB Output is correct
19 Correct 63 ms 48800 KB Output is correct
20 Correct 79 ms 52452 KB Output is correct
21 Correct 39 ms 52808 KB Output is correct
22 Correct 63 ms 54192 KB Output is correct
23 Correct 83 ms 52828 KB Output is correct
24 Correct 83 ms 53180 KB Output is correct
25 Correct 76 ms 53116 KB Output is correct
26 Correct 9 ms 42464 KB Output is correct
27 Correct 9 ms 42464 KB Output is correct
28 Correct 9 ms 42780 KB Output is correct
29 Correct 9 ms 42780 KB Output is correct
30 Correct 3 ms 42464 KB Output is correct
31 Correct 9 ms 42464 KB Output is correct
32 Correct 9 ms 42464 KB Output is correct
33 Correct 9 ms 42464 KB Output is correct
34 Correct 6 ms 42464 KB Output is correct
35 Correct 6 ms 42464 KB Output is correct
36 Correct 19 ms 43728 KB Output is correct
37 Correct 9 ms 43408 KB Output is correct
38 Correct 13 ms 42860 KB Output is correct
39 Correct 6 ms 43404 KB Output is correct
40 Correct 6 ms 43760 KB Output is correct