Submission #258412

# Submission time Handle Problem Language Result Execution time Memory
258412 2020-08-05T22:51:23 Z ipaljak Zoo (COCI19_zoo) C++14
110 / 110
355 ms 44976 KB
#include <bits/stdc++.h>

using namespace std;

#define TRACE(x) cerr << #x << " " << x << endl
#define FOR(i, a, b) for (int i = (a); i < int(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define _ << " " <<

typedef long long llint;

const int MAXN = 1010;

const int dr[] = {1, 0, -1, 0};
const int ds[] = {0, 1, 0, -1};

char grid[MAXN][MAXN];

int R, S, comp_cnt;
int comp[MAXN][MAXN];

set<pair<int, int>> E;
vector<vector<int>> v;

inline bool inside(int r, int s) {
  return r >= 0 && r < R && s >= 0 && s < S;
}

inline bool check(int r, int s) {
  return r >= 0 && r < R && s >= 0 && s < S && comp[r][s] == -1;
}

void bfs(int r, int s, int curr_comp) {
  queue<pair<int, int>> Q;
  comp[r][s] = curr_comp;
  Q.push({r, s});
  while (!Q.empty()) {
    auto curr = Q.front(); Q.pop();
    for (int i = 0; i < 4; ++i) {
      int rr = curr.first + dr[i], ss = curr.second + ds[i];
      if (check(rr, ss) && grid[rr][ss] == grid[r][s]) {
        comp[rr][ss] = comp[r][s];
        Q.push({rr, ss});
      }
    }
  }
}

int main(void) {
  scanf("%d%d", &R, &S);
  for (int i = 0; i < R; ++i)
    scanf("%s", grid[i]);

  memset(comp, -1, sizeof comp);

  for (int i = 0; i < R; ++i)
    for (int j = 0; j < S; ++j)
      if (comp[i][j] == -1 && grid[i][j] != '*')
        bfs(i, j, comp_cnt++);

  v.resize(comp_cnt);
  for (int i = 0; i < R; ++i) {
    for (int j = 0; j < S; ++j) {
      if (comp[i][j] == -1) continue;
      for (int k = 0; k < 4; ++k) {
        int ii = i + dr[k], jj = j + ds[k];
        if (!inside(ii, jj) || comp[ii][jj] == -1 || comp[ii][jj] == comp[i][j])
          continue;
        if (E.find({comp[i][j], comp[ii][jj]}) == E.end()) {
          E.insert({comp[i][j], comp[ii][jj]});
          v[comp[i][j]].push_back(comp[ii][jj]);
        }
      }
    }
  }

  vector<bool> bio(comp_cnt, false);
  queue<pair<int, int>> Q;
  bio[0] = true;
  Q.push({0, 1});
  int sol = 0;
  while (!Q.empty()) {
    auto curr = Q.front(); Q.pop();
    sol = max(sol, curr.second);
    for (int nxt : v[curr.first]) {
      if (bio[nxt]) continue;
      bio[nxt] = true;
      Q.push({nxt, curr.second + 1});
    }
  }

  printf("%d\n", sol);
  return 0;
}

Compilation message

zoo.cpp: In function 'int main()':
zoo.cpp:50:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &R, &S);
   ~~~~~^~~~~~~~~~~~~~~~
zoo.cpp:52:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%s", grid[i]);
     ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4352 KB Output is correct
2 Correct 2 ms 4352 KB Output is correct
3 Correct 3 ms 4352 KB Output is correct
4 Correct 3 ms 4352 KB Output is correct
5 Correct 3 ms 4480 KB Output is correct
6 Correct 3 ms 4480 KB Output is correct
7 Correct 3 ms 4480 KB Output is correct
8 Correct 5 ms 4736 KB Output is correct
9 Correct 5 ms 4736 KB Output is correct
10 Correct 5 ms 4736 KB Output is correct
11 Correct 4 ms 4736 KB Output is correct
12 Correct 6 ms 4864 KB Output is correct
13 Correct 5 ms 4736 KB Output is correct
14 Correct 4 ms 4736 KB Output is correct
15 Correct 5 ms 4736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4352 KB Output is correct
2 Correct 2 ms 4352 KB Output is correct
3 Correct 3 ms 4352 KB Output is correct
4 Correct 3 ms 4352 KB Output is correct
5 Correct 3 ms 4480 KB Output is correct
6 Correct 3 ms 4480 KB Output is correct
7 Correct 3 ms 4480 KB Output is correct
8 Correct 5 ms 4736 KB Output is correct
9 Correct 5 ms 4736 KB Output is correct
10 Correct 5 ms 4736 KB Output is correct
11 Correct 4 ms 4736 KB Output is correct
12 Correct 6 ms 4864 KB Output is correct
13 Correct 5 ms 4736 KB Output is correct
14 Correct 4 ms 4736 KB Output is correct
15 Correct 5 ms 4736 KB Output is correct
16 Correct 19 ms 6528 KB Output is correct
17 Correct 19 ms 6528 KB Output is correct
18 Correct 18 ms 6528 KB Output is correct
19 Correct 26 ms 6656 KB Output is correct
20 Correct 27 ms 6520 KB Output is correct
21 Correct 342 ms 43256 KB Output is correct
22 Correct 330 ms 42872 KB Output is correct
23 Correct 337 ms 43384 KB Output is correct
24 Correct 342 ms 44976 KB Output is correct
25 Correct 353 ms 44152 KB Output is correct
26 Correct 331 ms 43784 KB Output is correct
27 Correct 343 ms 42872 KB Output is correct
28 Correct 336 ms 42744 KB Output is correct
29 Correct 351 ms 44792 KB Output is correct
30 Correct 355 ms 44320 KB Output is correct