Submission #206317

# Submission time Handle Problem Language Result Execution time Memory
206317 2020-03-03T01:08:18 Z wiwiho Zoo (COCI19_zoo) C++14
110 / 110
158 ms 22008 KB
//#define NDEBUG

#include <bits/stdc++.h>
#include <bits/extc++.h>

#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define pob pop_back()
#define pof pop_front()
#define F first
#define S second
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
    if(pvaspace) b << " "; pvaspace=true;\
    b << pva;\
}\
b << "\n";}
#define pii pair<int, int>
#define pll pair<ll, ll>
#define tiii tuple<int, int, int>
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define iceil(a, b) ((a) / (b) + !!((a) % (b)))

//#define TEST

typedef long long ll;
typedef unsigned long long ull;

using namespace std;
using namespace __gnu_pbds;

const ll MOD = 1000000007;
const ll MAX = 2147483647;

template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
    return o << '(' << p.F << ',' << p.S << ')';
}

vector<string> a;
vector<vector<int>> id;
vector<pii> d = {mp(0, 1), mp(1, 0), mp(0, -1), mp(-1, 0)};

void dfs(int x, int y, int cnt){
    id[x][y] = cnt;
    for(pii i : d){
        int nx = x + i.F;
        int ny = y + i.S;
        if(a[nx][ny] == a[x][y] && id[nx][ny] == -1) dfs(nx, ny, cnt);
    }
}

int main(){
    StarBurstStream

    int r, c;
    cin >> r >> c;

    a.resize(r + 2);
    a[0] = a[r + 1] = string(c + 2, '*');
    for(int i = 1; i <= r; i++){
        cin >> a[i];
        a[i] = '*' + a[i] + '*';
    }

    id.resize(r + 2, vector<int>(c + 2, -1));
    int cnt = 1;
    for(int i = 1; i <= r; i++){
        for(int j = 1; j <= c; j++){
            if(a[i][j] != '*' && id[i][j] == -1) dfs(i, j, cnt++);
        }
//        printv(id[i], cerr);
    }

    vector<vector<int>> g(cnt);
    for(int i = 1; i <= r; i++){
        for(int j = 1; j <= c; j++){
            for(pii k : d){
                //cerr << i << " " << j << " " << i + k.F << " " << j + k.S << " " << id[i][j] << " " << id[i + k.F][i + k.S] << "\n";
                if(id[i][j] != -1 && id[i + k.F][j + k.S] != -1 && id[i + k.F][j + k.S] != id[i][j]) g[id[i][j]].eb(id[i + k.F][j + k.S]);
            }
        }
    }

    vector<int> dis(cnt, -1);
    queue<int> q;
    dis[id[r][c]] = 0;
    q.push(id[r][c]);
    while(!q.empty()){
        int now = q.front();
        q.pop();
        for(int i : g[now]){
            if(dis[i] != -1) continue;
            dis[i] = dis[now] + 1;
            q.push(i);
        }
    }

    cout << *max_element(iter(dis)) + 1 << "\n";

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 504 KB Output is correct
6 Correct 5 ms 504 KB Output is correct
7 Correct 5 ms 504 KB Output is correct
8 Correct 6 ms 504 KB Output is correct
9 Correct 6 ms 504 KB Output is correct
10 Correct 6 ms 632 KB Output is correct
11 Correct 6 ms 504 KB Output is correct
12 Correct 6 ms 504 KB Output is correct
13 Correct 6 ms 504 KB Output is correct
14 Correct 6 ms 508 KB Output is correct
15 Correct 6 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 504 KB Output is correct
6 Correct 5 ms 504 KB Output is correct
7 Correct 5 ms 504 KB Output is correct
8 Correct 6 ms 504 KB Output is correct
9 Correct 6 ms 504 KB Output is correct
10 Correct 6 ms 632 KB Output is correct
11 Correct 6 ms 504 KB Output is correct
12 Correct 6 ms 504 KB Output is correct
13 Correct 6 ms 504 KB Output is correct
14 Correct 6 ms 508 KB Output is correct
15 Correct 6 ms 504 KB Output is correct
16 Correct 24 ms 8056 KB Output is correct
17 Correct 30 ms 7672 KB Output is correct
18 Correct 25 ms 7928 KB Output is correct
19 Correct 29 ms 8184 KB Output is correct
20 Correct 26 ms 7800 KB Output is correct
21 Correct 136 ms 20984 KB Output is correct
22 Correct 139 ms 20856 KB Output is correct
23 Correct 143 ms 21112 KB Output is correct
24 Correct 147 ms 22008 KB Output is correct
25 Correct 144 ms 21528 KB Output is correct
26 Correct 143 ms 21368 KB Output is correct
27 Correct 139 ms 20856 KB Output is correct
28 Correct 142 ms 20728 KB Output is correct
29 Correct 158 ms 22008 KB Output is correct
30 Correct 148 ms 21624 KB Output is correct