Submission #520374

# Submission time Handle Problem Language Result Execution time Memory
520374 2022-01-29T18:05:52 Z Alex_tz307 Kutije (COCI21_kutije) C++17
70 / 70
188 ms 31984 KB
#include <bits/stdc++.h>

using namespace std;

const int kN = 1e3;
const int kQ = 5e5;
vector<int> g[1 + kN], gt[1 + kN], ctc[1 + kN], t[1 + kN];
pair<int, int> edges[kN * kN];
vector<pair<int, int>> queries[1 + kN];
int ctcCnt, ctcIndex[1 + kN];
bitset<1 + kN> vis, inStack;
stack<int> st;
bitset<1 + kQ> sol;

void dfs1(int u) {
  vis[u] = true;
  for (int v : g[u]) {
    if (!vis[v]) {
      dfs1(v);
    }
  }
  st.emplace(u);
}

void dfs2(int u) {
  ctcIndex[u] = ctcCnt;
  ctc[ctcCnt].emplace_back(u);
  for (int v : gt[u]) {
    if (ctcIndex[v] == 0) {
      dfs2(v);
    }
  }
}

void dfs3(int u) {
  vis[u] = true;
  for (int v : ctc[u]) {
    inStack[v] = true;
  }
  for (int v : ctc[u]) {
    for (auto it : queries[v]) {
      sol[it.second] = inStack[it.first];
    }
  }
  for (int v : t[u]) {
    if (!vis[v]) {
      dfs3(v);
    }
  }
  for (int v : ctc[u]) {
    inStack[v] = false;
  }
}

void TestCase() {
  int n, m, q;
  cin >> n >> m >> q;
  int cntE = 0;
  for (int i = 1; i <= m; ++i) {
    for (int u = 1; u <= n; ++u) {
      int v;
      cin >> v;
      g[u].emplace_back(v);
      gt[v].emplace_back(u);
      edges[cntE++] = {u, v};
    }
  }
  for (int i = 1; i <= q; ++i) {
    int u, v;
    cin >> u >> v;
    queries[v].emplace_back(u, i);
  }
  for (int v = 1; v <= n; ++v) {
    if (!vis[v]) {
      dfs1(v);
    }
  }
  while (!st.empty()) {
    int u = st.top();
    st.pop();
    if (ctcIndex[u] == 0) {
      ++ctcCnt;
      dfs2(u);
    }
  }
  for (int i = 0; i < n * m; ++i) {
    int u, v;
    tie(u, v) = edges[i];
    int p = ctcIndex[u], r = ctcIndex[v];
    if (p != r) {
      t[p].emplace_back(r);
    }
  }
  vis.reset();
  for (int i = 1; i <= ctcCnt; ++i) {
    if (!vis[i]) {
      dfs3(i);
    }
  }
  for (int i = 1; i <= q; ++i) {
    if (sol[i]) {
      cout << "DA\n";
    } else {
      cout << "NE\n";
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int tests = 1;
  for (int tc = 1; tc <= tests; ++tc) {
    TestCase();
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 80 ms 11684 KB Output is correct
2 Correct 78 ms 9420 KB Output is correct
3 Correct 66 ms 8856 KB Output is correct
4 Correct 78 ms 11828 KB Output is correct
5 Correct 81 ms 11712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 588 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 2 ms 560 KB Output is correct
5 Correct 1 ms 588 KB Output is correct
6 Correct 2 ms 588 KB Output is correct
7 Correct 1 ms 588 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 2 ms 580 KB Output is correct
10 Correct 1 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 588 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 2 ms 560 KB Output is correct
5 Correct 1 ms 588 KB Output is correct
6 Correct 2 ms 588 KB Output is correct
7 Correct 1 ms 588 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 2 ms 580 KB Output is correct
10 Correct 1 ms 588 KB Output is correct
11 Correct 1 ms 580 KB Output is correct
12 Correct 1 ms 588 KB Output is correct
13 Correct 1 ms 588 KB Output is correct
14 Correct 1 ms 588 KB Output is correct
15 Correct 2 ms 592 KB Output is correct
16 Correct 1 ms 588 KB Output is correct
17 Correct 1 ms 588 KB Output is correct
18 Correct 1 ms 588 KB Output is correct
19 Correct 1 ms 584 KB Output is correct
20 Correct 1 ms 592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 11684 KB Output is correct
2 Correct 78 ms 9420 KB Output is correct
3 Correct 66 ms 8856 KB Output is correct
4 Correct 78 ms 11828 KB Output is correct
5 Correct 81 ms 11712 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 588 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 2 ms 560 KB Output is correct
10 Correct 1 ms 588 KB Output is correct
11 Correct 2 ms 588 KB Output is correct
12 Correct 1 ms 588 KB Output is correct
13 Correct 1 ms 588 KB Output is correct
14 Correct 2 ms 580 KB Output is correct
15 Correct 1 ms 588 KB Output is correct
16 Correct 1 ms 580 KB Output is correct
17 Correct 1 ms 588 KB Output is correct
18 Correct 1 ms 588 KB Output is correct
19 Correct 1 ms 588 KB Output is correct
20 Correct 2 ms 592 KB Output is correct
21 Correct 1 ms 588 KB Output is correct
22 Correct 1 ms 588 KB Output is correct
23 Correct 1 ms 588 KB Output is correct
24 Correct 1 ms 584 KB Output is correct
25 Correct 1 ms 592 KB Output is correct
26 Correct 169 ms 31984 KB Output is correct
27 Correct 166 ms 31820 KB Output is correct
28 Correct 142 ms 26308 KB Output is correct
29 Correct 173 ms 30472 KB Output is correct
30 Correct 136 ms 26660 KB Output is correct
31 Correct 138 ms 26948 KB Output is correct
32 Correct 163 ms 31496 KB Output is correct
33 Correct 188 ms 31288 KB Output is correct
34 Correct 166 ms 31428 KB Output is correct
35 Correct 162 ms 31684 KB Output is correct