Submission #400438

#TimeUsernameProblemLanguageResultExecution timeMemory
400438timmyfengVirus Experiment (JOI19_virus)C++17
14 / 100
334 ms89040 KiB
#include <bits/stdc++.h> using namespace std; const int I[] = {1, 0, -1, 0}, J[] = {0, 1, 0, -1}; const string D = "SENW"; const int N = 800; int resist[N][N], wind[1 << 4], r, c; vector<int> scc[N * N], adj[N * N], path; int visited[N * N], color[N * N]; bool check(int i, int j, int k) { int mask = 0; for (int d = 0; d < 4; ++d) { int i_new = i + I[d], j_new = j + J[d]; if (0 <= i_new && i_new < r && 0 <= j_new && j_new < c) { if (color[i_new * c + j_new] == k) { mask |= 1 << d; } } } return resist[i][j] > 0 && resist[i][j] <= wind[mask]; } void process(vector<int> &nodes, vector<int> &out) { for (auto u : nodes) { int i = u / c, j = u % c; for (int d = 0; d < 4; ++d) { int i_new = i + I[d], j_new = j + J[d]; if (0 <= i_new && i_new < r && 0 <= j_new && j_new < c) { if (check(i_new, j_new, color[u])) { out.push_back(i_new * c + j_new); } } } } } void dfs(int u) { path.push_back(color[u]); visited[u] = 1; for (int i = 0; i < (int) adj[u].size(); ++i) { int c = adj[u][i]; if (visited[c] == 0) { dfs(c); if (color[u] != path.back()) { path.pop_back(); } } else if (visited[c] == 1) { int x = path.back(); path.pop_back(); while (color[c] != x) { int y = path.back(); path.pop_back(); if (scc[x].size() < scc[y].size()) { swap(x, y); } for (auto v : scc[y]) { color[v] = x; } scc[x].insert(scc[x].end(), scc[y].begin(), scc[y].end()); process(scc[y], adj[u]); scc[y].clear(); } path.push_back(x); } } visited[u] = 2; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int m; string d; cin >> m >> r >> c >> d; for (int i = 0; i < r; ++i) { for (int j = 0; j < c; ++j) { cin >> resist[i][j]; } } for (int i = 0; i < (1 << 4); ++i) { int len = 0; for (int j = 0; j < 2 * m; ++j) { int dir = find(D.begin(), D.end(), d[j % m]) - D.begin(); if ((i & (1 << dir)) > 0) { wind[i] = max(wind[i], ++len); } else { len = 0; } } if (len == 2 * m) { wind[i] = INT_MAX; } } iota(color, color + r * c, 0); for (int i = 0; i < r * c; ++i) { scc[i].push_back(i); process(scc[i], adj[i]); } for (int i = 0; i < r * c; ++i) { if (visited[i] == 0) { dfs(i); } } int mini = INT_MAX, ways = 0; for (int i = 0; i < r * c; ++i) { bool leaf = !scc[i].empty(); for (auto j : scc[i]) { for (auto k : adj[j]) { leaf &= color[k] == i; } } if (leaf && resist[i / c][i % c] > 0) { if ((int) scc[i].size() < mini) { mini = scc[i].size(); ways = 0; } if ((int) scc[i].size() == mini) { ways += mini; } } } cout << mini << "\n"; cout << ways << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...