Submission #59025

# Submission time Handle Problem Language Result Execution time Memory
59025 2018-07-20T08:40:49 Z choikiwon Sandwich (JOI16_sandwich) C++17
35 / 100
8000 ms 65576 KB
#include<bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;

const int MN = 402;

int N, M;
char G[MN][MN];
vector<int> adj[MN * MN * 2], radj[MN * MN * 2], sadj[MN * MN * 2];
int tin[MN * MN * 2], bck[MN * MN * 2], id[MN * MN * 2], sz[MN * MN * 2], timer, scnt;
stack<int> stk;

int f(int r, int c, int t) {
    return r * (2 * M) + c * 2 + t;
}

void dfs(int u) {
    tin[u] = timer++;
    bck[u] = tin[u];
    stk.push(u);

    for(int i = 0; i < radj[u].size(); i++) {
        int v = radj[u][i];
        if(tin[v] == -1) {
            dfs(v);
            bck[u] = min(bck[u], bck[v]);
        }
        else if(id[v] == -1) {
            bck[u] = min(bck[u], tin[v]);
        }
    }
    if(bck[u] == tin[u]) {
        while(1) {
            int t = stk.top(); stk.pop();
            id[t] = scnt;
            if(t == u) break;
        }
        scnt++;
    }
}

int del[MN * MN * 2], chk[MN * MN * 2];
queue<int> q;

void scc() {
    memset(tin, -1, sizeof(tin));
    memset(id, -1, sizeof(id));
    for(int i = 0; i < N * M * 2; i++) if(tin[i] == -1) {
        dfs(i);
    }
    for(int i = 0; i < N * M * 2; i++) {
        sz[ id[i] ]++;

        for(int j = 0; j < radj[i].size(); j++) {
            int k = radj[i][j];
            if(id[i] == id[k]) continue;
            sadj[ id[i] ].push_back(id[k]);
        }
    }
    for(int i = 0; i < scnt; i++) {
        if(sz[i] > 1) {
            q.push(i);
            del[i] = 1;
        }
    }
    while(!q.empty()) {
        int u = q.front(); q.pop();

        for(int i = 0; i < sadj[u].size(); i++) {
            int v = sadj[u][i];
            if(!del[v]) {
                q.push(v);
                del[v] = 1;
            }
        }
    }
    for(int i = 0; i < N * M * 2; i++) {
        chk[i] = del[ id[i] ];
    }
}

int ans[MN * MN * 2], vis[MN * MN * 2];

int main() {
    scanf("%d %d", &N, &M);

    for(int i = 0; i < N; i++) {
        scanf("\n");
        for(int j = 0; j < M; j++) {
            scanf("%c", &G[i][j]);
        }
    }

    for(int i = 0; i < N; i++) {
        for(int j = 0; j < M; j++) {
            for(int k = 0; k < 2; k++) {
                if(k) {
                    if(i) {
                        adj[ f(i, j, k) ].push_back(f(i - 1, j, k));
                        radj[ f(i - 1, j, k) ].push_back(f(i, j, k));
                    }
                }
                else if(i < N - 1) {
                    adj[ f(i, j, k) ].push_back(f(i + 1, j, k));
                    radj[ f(i + 1, j, k) ].push_back(f(i, j, k));
                }
                if(k ^ (G[i][j] == 'N')) {
                    if(j) {
                        adj[ f(i, j, k) ].push_back(f(i, j - 1, G[i][j - 1] == 'Z'));
                        radj[ f(i, j - 1, G[i][j - 1] == 'Z') ].push_back(f(i, j, k));
                    }
                }
                else if(j < M - 1) {
                    adj[ f(i, j, k) ].push_back(f(i, j + 1, G[i][j + 1] == 'N'));
                    radj[ f(i, j + 1, G[i][j + 1] == 'N') ].push_back(f(i, j, k));
                }
            }
        }
    }

    scc();

    for(int i = 0; i < N; i++) {
        memset(vis, 0, sizeof(vis));

        for(int j = 0; j < M; j++) {
            int k = G[i][j] == 'N';
            if(chk[ f(i, j, k) ]) continue;

            q.push(f(i, j, k));
            ans[ f(i, j, k) ] += M - j;
            vis[ f(i, j, k) ] = 1;
            while(!q.empty()) {
                int u = q.front(); q.pop();

                for(int x = 0; x < radj[u].size(); x++) {
                    int v = radj[u][x];
                    if(!chk[v] && !vis[v]) {
                        q.push(v);
                        ans[v] += M - j;
                        vis[v] = 1;
                    }
                }
            }
        }
    }
    for(int i = 0; i < N; i++) {
        memset(vis, 0, sizeof(vis));

        for(int j = M - 1; j >= 0; j--) {
            int k = G[i][j] == 'Z';
            if(chk[ f(i, j, k) ]) continue;

            q.push(f(i, j, k));
            ans[ f(i, j, k) ] += j + 1;
            vis[ f(i, j, k) ] = 1;
            while(!q.empty()) {
                int u = q.front(); q.pop();

                for(int x = 0; x < radj[u].size(); x++) {
                    int v = radj[u][x];
                    if(!chk[v] && !vis[v]) {
                        q.push(v);
                        ans[v] += j + 1;
                        vis[v] = 1;
                    }
                }
            }
        }
    }

    for(int i = 0; i < N; i++) {
        for(int j = 0; j < M; j++) {
            if(chk[ f(i, j, 0) ] && chk[ f(i, j, 1) ]) printf("-1 ");
            else if(chk[ f(i, j, 0) ]) printf("%d ", 2 * ans[ f(i, j, 1) ]);
            else if(chk[ f(i, j, 1) ]) printf("%d ", 2 * ans[ f(i, j, 0) ]);
            else printf("%d ", 2 * min(ans[ f(i, j, 0) ], ans[ f(i, j, 1) ]));
        }
        printf("\n");
    }
}

Compilation message

sandwich.cpp: In function 'void dfs(int)':
sandwich.cpp:23:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < radj[u].size(); i++) {
                    ~~^~~~~~~~~~~~~~~~
sandwich.cpp: In function 'void scc()':
sandwich.cpp:55:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j = 0; j < radj[i].size(); j++) {
                        ~~^~~~~~~~~~~~~~~~
sandwich.cpp:70:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0; i < sadj[u].size(); i++) {
                        ~~^~~~~~~~~~~~~~~~
sandwich.cpp: In function 'int main()':
sandwich.cpp:137:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 for(int x = 0; x < radj[u].size(); x++) {
                                ~~^~~~~~~~~~~~~~~~
sandwich.cpp:161:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 for(int x = 0; x < radj[u].size(); x++) {
                                ~~^~~~~~~~~~~~~~~~
sandwich.cpp:86:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &N, &M);
     ~~~~~^~~~~~~~~~~~~~~~~
sandwich.cpp:89:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("\n");
         ~~~~~^~~~~~
sandwich.cpp:91:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%c", &G[i][j]);
             ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 25 ms 26872 KB Output is correct
2 Correct 25 ms 26996 KB Output is correct
3 Correct 33 ms 27020 KB Output is correct
4 Correct 25 ms 27052 KB Output is correct
5 Correct 36 ms 27172 KB Output is correct
6 Correct 37 ms 27748 KB Output is correct
7 Correct 46 ms 27748 KB Output is correct
8 Correct 45 ms 27816 KB Output is correct
9 Correct 35 ms 27816 KB Output is correct
10 Correct 43 ms 27816 KB Output is correct
11 Correct 55 ms 27816 KB Output is correct
12 Correct 41 ms 27816 KB Output is correct
13 Correct 46 ms 27816 KB Output is correct
14 Correct 49 ms 27816 KB Output is correct
15 Correct 50 ms 27816 KB Output is correct
16 Correct 48 ms 27892 KB Output is correct
17 Correct 51 ms 27892 KB Output is correct
18 Correct 46 ms 27928 KB Output is correct
19 Correct 40 ms 27928 KB Output is correct
20 Correct 41 ms 27928 KB Output is correct
21 Correct 38 ms 27928 KB Output is correct
22 Correct 41 ms 27928 KB Output is correct
23 Correct 39 ms 27928 KB Output is correct
24 Correct 50 ms 27928 KB Output is correct
25 Correct 45 ms 27928 KB Output is correct
26 Correct 47 ms 27928 KB Output is correct
27 Correct 43 ms 27932 KB Output is correct
28 Correct 44 ms 27948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 26872 KB Output is correct
2 Correct 25 ms 26996 KB Output is correct
3 Correct 33 ms 27020 KB Output is correct
4 Correct 25 ms 27052 KB Output is correct
5 Correct 36 ms 27172 KB Output is correct
6 Correct 37 ms 27748 KB Output is correct
7 Correct 46 ms 27748 KB Output is correct
8 Correct 45 ms 27816 KB Output is correct
9 Correct 35 ms 27816 KB Output is correct
10 Correct 43 ms 27816 KB Output is correct
11 Correct 55 ms 27816 KB Output is correct
12 Correct 41 ms 27816 KB Output is correct
13 Correct 46 ms 27816 KB Output is correct
14 Correct 49 ms 27816 KB Output is correct
15 Correct 50 ms 27816 KB Output is correct
16 Correct 48 ms 27892 KB Output is correct
17 Correct 51 ms 27892 KB Output is correct
18 Correct 46 ms 27928 KB Output is correct
19 Correct 40 ms 27928 KB Output is correct
20 Correct 41 ms 27928 KB Output is correct
21 Correct 38 ms 27928 KB Output is correct
22 Correct 41 ms 27928 KB Output is correct
23 Correct 39 ms 27928 KB Output is correct
24 Correct 50 ms 27928 KB Output is correct
25 Correct 45 ms 27928 KB Output is correct
26 Correct 47 ms 27928 KB Output is correct
27 Correct 43 ms 27932 KB Output is correct
28 Correct 44 ms 27948 KB Output is correct
29 Correct 27 ms 27948 KB Output is correct
30 Correct 85 ms 27948 KB Output is correct
31 Correct 310 ms 65524 KB Output is correct
32 Correct 311 ms 65576 KB Output is correct
33 Correct 110 ms 65576 KB Output is correct
34 Execution timed out 8055 ms 65576 KB Time limit exceeded
35 Halted 0 ms 0 KB -