Submission #1060741

# Submission time Handle Problem Language Result Execution time Memory
1060741 2024-08-15T21:40:54 Z MilosMilutinovic The Ties That Guide Us (CEOI23_incursion) C++17
30 / 100
261 ms 16292 KB
#include "incursion.h"
#include <bits/stdc++.h>

using namespace std;

vector<int> mark(vector<pair<int, int>> e, int safe) {
  --safe;
  int n = (int) e.size() + 1;
  vector<vector<int>> g(n);
  for (int i = 0; i + 1 < n; i++) {
    --e[i].first; --e[i].second;
    g[e[i].first].push_back(e[i].second);
    g[e[i].second].push_back(e[i].first);
  }
  int root;
  {
    vector<int> sz(n);
    function<void(int, int)> Dfs = [&](int v, int pv) {
      sz[v] = 1;
      for (int u : g[v]) {
        if (u == pv) {
          continue;
        }
        Dfs(u, v);
        sz[v] += sz[u];
      }
    };
    Dfs(0, 0);
    vector<int> cen;
    function<int(int, int)> FindCentroid = [&](int v, int pv) {
      if (sz[v] * 2 == n) {
        cen.push_back(pv);
      }
      for (int u : g[v]) {
        if (u == pv || sz[u] * 2 < n) {
          continue;
        }
        return FindCentroid(u, v);
      }
      cen.push_back(v);
      return v;
    };
    root = FindCentroid(0, 0);
    if ((int) cen.size() == 2) {
      vector<int> que(1, safe);
      vector<bool> was(n);
      was[safe] = true;
      for (int b = 0; b < (int) que.size(); b++) {
        int i = que[b];
        if (i == cen[0] || i == cen[1]) {
          root = i;
          break;
        }
        for (int j : g[i]) {
          if (!was[j]) {
            que.push_back(j);
            was[j] = true;
          }
        }
      }
    }
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if ((int) g[i].size() == 2) {
      cnt += 1;
    }
  }
  if (cnt == 1) {
    for (int i = 0; i < n; i++) {
      if ((int) g[i].size() == 2) {
        root = i;
      }
    }
  }
  vector<int> dfn(n);
  vector<int> dfo(n);
  int T = -1;
  function<void(int, int)> Dfs = [&](int v, int pv) {
    dfn[v] = ++T;
    for (int u : g[v]) {
      if (u == pv) {
        continue;
      }
      Dfs(u, v);
    }
    dfo[v] = T;
  };
  Dfs(root, root);
  vector<int> seq(n);
  for (int i = 0; i < n; i++) {
    if (dfn[i] <= dfn[safe] && dfo[safe] <= dfo[i]) {
      seq[i] = 1;
    } else {
      seq[i] = 0;
    }
  }
  return seq;
}

void locate(vector<pair<int, int>> e, int curr, int t) {
  --curr;
  int n = (int) e.size() + 1;
  vector<vector<int>> g(n);
  for (int i = 0; i + 1 < n; i++) {
    --e[i].first; --e[i].second;
    g[e[i].first].push_back(e[i].second);
    g[e[i].second].push_back(e[i].first);
  }
  vector<int> cen;
  {
    vector<int> sz(n);
    function<void(int, int)> Dfs = [&](int v, int pv) {
      sz[v] = 1;
      for (int u : g[v]) {
        if (u == pv) {
          continue;
        }
        Dfs(u, v);
        sz[v] += sz[u];
      }
    };
    Dfs(0, 0);
    function<void(int, int)> Find = [&](int v, int pv) {
      if (sz[v] * 2 == n) {
        cen.push_back(pv);
      }
      for (int u : g[v]) {
        if (u == pv || sz[u] * 2 < n) {
          continue;
        }
        Find(u, v);
        return;
      }
      cen.push_back(v);
    };
    Find(0, 0);
  }
  int root = cen[0];
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if ((int) g[i].size() == 2) {
      cnt += 1;
    }
  }
  if (cnt == 1) {
    for (int i = 0; i < n; i++) {
      if ((int) g[i].size() == 2) {
        root = i;
      }
    }
  }
  vector<int> x(n, -1);
  vector<int> pr(n);
  vector<int> sz(n);
  function<void(int, int)> Dfs = [&](int v, int pv) {
    pr[v] = pv;
    sz[v] = 1;
    for (int u : g[v]) {
      if (u == pv) {
        continue;
      }
      Dfs(u, v);
      sz[v] += sz[u];
      if (x[v] == -1 || sz[x[v]] < sz[u]) {
        x[v] = u;
      }
    }
  };
  Dfs(root, root);
  int Q = 0;
  while (true) {
    if (curr == root && t == 0) {
      root = cen[1];
      x = vector<int>(n, -1);
      Dfs(root, root);
      t = visit(root + 1);
      assert(t == 1);
      curr = root;
      continue;
    }
    if (t == 0) {
      t = visit(pr[curr] + 1);
      curr = pr[curr];
      continue;
    }
    if (x[curr] == -1) {
      return;
    }
    int k = visit(x[curr] + 1);
    if (k == 1) {
      curr = x[curr];
      t = k;
      continue;
    }
    Q += 2;
    assert(Q <= 28);
    visit(curr + 1);
    bool found = false;
    for (int u : g[curr]) {
      if (u == pr[curr] || u == x[curr]) {
        continue;
      }
      int k = visit(u + 1);
      if (k == 1) {
        t = k;
        curr = u;
        found = true;
        break;
      }
      Q += 2;
      assert(Q <= 28);
      visit(curr + 1);
    }
    if (!found) {
      return;
    }
  }
}

Compilation message

interface.cpp: In function 'int main()':
interface.cpp:44:55: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   44 |     if(fread(T.data(), sizeof(int), 2 * N - 2, stdin) != 2 * N - 2) exit(0);
      |        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
interface.cpp:50:33: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   50 |         int l = (numbers.size() == N ? N : 0);
      |                  ~~~~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 764 KB Correct
# Verdict Execution time Memory Grader output
1 Correct 192 ms 13348 KB Correct
2 Correct 207 ms 13812 KB Correct
3 Correct 120 ms 14912 KB Correct
4 Correct 121 ms 15580 KB Correct
5 Correct 188 ms 15076 KB Correct
6 Correct 92 ms 15352 KB Correct
7 Correct 90 ms 13812 KB Correct
8 Correct 194 ms 14140 KB Correct
9 Correct 221 ms 14716 KB Correct
10 Correct 152 ms 14076 KB Correct
11 Correct 103 ms 15512 KB Correct
12 Correct 261 ms 14004 KB Correct
13 Correct 78 ms 13052 KB Correct
14 Correct 83 ms 13848 KB Correct
15 Correct 196 ms 14792 KB Correct
16 Correct 230 ms 16292 KB Correct
17 Correct 134 ms 12856 KB Correct
18 Correct 105 ms 13544 KB Correct
19 Correct 173 ms 12884 KB Correct
20 Correct 83 ms 15364 KB Correct
21 Correct 79 ms 14068 KB Correct
22 Correct 217 ms 13800 KB Correct
23 Correct 200 ms 14180 KB Correct
24 Correct 96 ms 13632 KB Correct
25 Correct 93 ms 14752 KB Correct
26 Correct 89 ms 13604 KB Correct
27 Correct 91 ms 13888 KB Correct
28 Correct 88 ms 14336 KB Correct
29 Correct 227 ms 14324 KB Correct
30 Correct 201 ms 13076 KB Correct
31 Correct 93 ms 15832 KB Correct
32 Correct 226 ms 14376 KB Correct
33 Correct 256 ms 14808 KB Correct
34 Correct 79 ms 13372 KB Correct
35 Correct 87 ms 12800 KB Correct
36 Correct 203 ms 14304 KB Correct
37 Correct 203 ms 14312 KB Correct
38 Correct 249 ms 15144 KB Correct
39 Correct 148 ms 15500 KB Correct
40 Correct 203 ms 15196 KB Correct
41 Correct 89 ms 14476 KB Correct
42 Correct 92 ms 14580 KB Correct
43 Correct 238 ms 14352 KB Correct
44 Correct 237 ms 12788 KB Correct
45 Correct 98 ms 13240 KB Correct
46 Correct 97 ms 13632 KB Correct
47 Correct 107 ms 14572 KB Correct
48 Correct 89 ms 12864 KB Correct
49 Correct 95 ms 15076 KB Correct
# Verdict Execution time Memory Grader output
1 Correct 92 ms 7920 KB Correct
2 Correct 88 ms 8448 KB Correct
3 Correct 92 ms 7912 KB Correct
4 Correct 97 ms 12484 KB Correct
5 Correct 165 ms 12284 KB Correct
6 Correct 170 ms 12276 KB Correct
7 Correct 72 ms 8012 KB Correct
8 Correct 80 ms 7936 KB Correct
9 Correct 86 ms 7860 KB Correct
10 Runtime error 63 ms 11776 KB Execution killed with signal 6
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 764 KB Correct
2 Correct 192 ms 13348 KB Correct
3 Correct 207 ms 13812 KB Correct
4 Correct 120 ms 14912 KB Correct
5 Correct 121 ms 15580 KB Correct
6 Correct 188 ms 15076 KB Correct
7 Correct 92 ms 15352 KB Correct
8 Correct 90 ms 13812 KB Correct
9 Correct 194 ms 14140 KB Correct
10 Correct 221 ms 14716 KB Correct
11 Correct 152 ms 14076 KB Correct
12 Correct 103 ms 15512 KB Correct
13 Correct 261 ms 14004 KB Correct
14 Correct 78 ms 13052 KB Correct
15 Correct 83 ms 13848 KB Correct
16 Correct 196 ms 14792 KB Correct
17 Correct 230 ms 16292 KB Correct
18 Correct 134 ms 12856 KB Correct
19 Correct 105 ms 13544 KB Correct
20 Correct 173 ms 12884 KB Correct
21 Correct 83 ms 15364 KB Correct
22 Correct 79 ms 14068 KB Correct
23 Correct 217 ms 13800 KB Correct
24 Correct 200 ms 14180 KB Correct
25 Correct 96 ms 13632 KB Correct
26 Correct 93 ms 14752 KB Correct
27 Correct 89 ms 13604 KB Correct
28 Correct 91 ms 13888 KB Correct
29 Correct 88 ms 14336 KB Correct
30 Correct 227 ms 14324 KB Correct
31 Correct 201 ms 13076 KB Correct
32 Correct 93 ms 15832 KB Correct
33 Correct 226 ms 14376 KB Correct
34 Correct 256 ms 14808 KB Correct
35 Correct 79 ms 13372 KB Correct
36 Correct 87 ms 12800 KB Correct
37 Correct 203 ms 14304 KB Correct
38 Correct 203 ms 14312 KB Correct
39 Correct 249 ms 15144 KB Correct
40 Correct 148 ms 15500 KB Correct
41 Correct 203 ms 15196 KB Correct
42 Correct 89 ms 14476 KB Correct
43 Correct 92 ms 14580 KB Correct
44 Correct 238 ms 14352 KB Correct
45 Correct 237 ms 12788 KB Correct
46 Correct 98 ms 13240 KB Correct
47 Correct 97 ms 13632 KB Correct
48 Correct 107 ms 14572 KB Correct
49 Correct 89 ms 12864 KB Correct
50 Correct 95 ms 15076 KB Correct
51 Correct 92 ms 7920 KB Correct
52 Correct 88 ms 8448 KB Correct
53 Correct 92 ms 7912 KB Correct
54 Correct 97 ms 12484 KB Correct
55 Correct 165 ms 12284 KB Correct
56 Correct 170 ms 12276 KB Correct
57 Correct 72 ms 8012 KB Correct
58 Correct 80 ms 7936 KB Correct
59 Correct 86 ms 7860 KB Correct
60 Runtime error 63 ms 11776 KB Execution killed with signal 6
61 Halted 0 ms 0 KB -