Submission #1052758

# Submission time Handle Problem Language Result Execution time Memory
1052758 2024-08-10T20:24:18 Z MilosMilutinovic Simurgh (IOI17_simurgh) C++14
70 / 100
219 ms 5000 KB
#include "simurgh.h"
#include <bits/stdc++.h>

using namespace std;

class dsu {
 public:
  vector<int> p;
  int n;

  dsu(int _n) : n(_n) {
    p.resize(n);
    iota(p.begin(), p.end(), 0);
  }

  inline int get(int x) {
    return (x == p[x] ? x : (p[x] = get(p[x])));
  }

  inline bool unite(int x, int y) {
    x = get(x);
    y = get(y);
    if (x != y) {
      p[x] = y;
      return true;
    }
    return false;
  }
};

vector<int> find_roads(int n, vector<int> u, vector<int> v) {
  int m = (int) u.size();
  vector<vector<int>> g(n);
  for (int i = 0; i < m; i++) {
    g[u[i]].push_back(i);
    g[v[i]].push_back(i);
  }
  dsu d(n);
  vector<int> tree;
  for (int i = 0; i < m; i++) {
    if (d.unite(u[i], v[i])) {
      tree.push_back(i);
    }
  }
  vector<vector<int>> t(n);
  for (int i : tree) {
    t[u[i]].push_back(i);
    t[v[i]].push_back(i);
  }
  int cnt = count_common_roads(tree);
  const int L = 20;
  vector<vector<int>> pr(n, vector<int>(L));
  vector<int> dep(n);
  vector<int> id(n);
  function<void(int, int)> Dfs = [&](int x, int px) {
    dep[x] = dep[px] + 1;
    pr[x][0] = px;
    for (int i = 1; i < L; i++) {
      pr[x][i] = pr[pr[x][i - 1]][i - 1];
    }
    for (int e : t[x]) {
      int y = (x ^ u[e] ^ v[e]);
      if (y == px) {
        continue;
      }
      id[y] = e;
      Dfs(y, x);
    }
  };
  Dfs(0, 0);
  auto LCA = [&](int x, int y) {
    if (dep[x] > dep[y]) {
      swap(x, y);
    }
    for (int i = L - 1; i >= 0; i--) {
      if (dep[pr[y][i]] >= dep[x]) {
        y = pr[y][i];
      }
    }
    if (x == y) {
      return x;
    }
    for (int i = L - 1; i >= 0; i--) {
      if (pr[x][i] != pr[y][i]) {
        x = pr[x][i];
        y = pr[y][i];
      }
    }
    return pr[x][0];
  };
  vector<int> state(m, 2);
  for (int i : tree) {
    state[i] = -1;
  }
  for (int i = 0; i < m; i++) {
    if (state[i] != 2) {
      continue;
    }
    vector<int> edges;
    int z = LCA(u[i], v[i]);
    for (int x = u[i]; x != z; x = pr[x][0]) {
      edges.push_back(id[x]);
    }
    for (int x = v[i]; x != z; x = pr[x][0]) {
      edges.push_back(id[x]);
    }
    vector<pair<int, int>> a;
    for (int e : edges) {
      if (state[e] != -1) {
        continue;
      }
      vector<int> qv(1, i);
      for (int i : tree) {
        if (i != e) {
          qv.push_back(i);
        }
      }
      a.emplace_back(count_common_roads(qv), e);
    }
    if (a.empty()) {
      continue;
    }
    for (auto& p : a) {
      if (p.first == cnt - 1) {
        state[i] = 0;
      }
      if (p.first == cnt + 1) {
        state[i] = 1;
      }
    }
    if (state[i] == 2) {
      for (int e : edges) {
        if (state[e] == -1) {
          continue;
        }
        vector<int> qv(1, i);
        for (int i : tree) {
          if (i != e) {
            qv.push_back(i);
          }
        }
        state[i] = (state[e] ^ (cnt != count_common_roads(qv) ? 1 : 0));
        break;
      }
      if (state[i] == 2) {
        state[i] = 0;
      }
    }
    for (auto& p : a) {
      state[p.second] = (state[i] ^ (cnt != p.first ? 1 : 0));
    }
  }
  for (int e : tree) {
    if (state[e] == -1) {
      state[e] = 1;
    }
  }
  vector<int> deg(n);
  for (int i = 0; i < n; i++) {
    dsu d(n);
    vector<int> qv;
    for (int e : g[i]) {
      qv.push_back(e);
      d.unite(u[e], v[e]);
    }
    int ft = 0;
    for (int e : tree) {
      if (d.unite(u[e], v[e])) {
        qv.push_back(e);
        ft += state[e];
      }
    }
    deg[i] = count_common_roads(qv) - ft;
  }
  vector<int> res;
  for (int i = 0; i < n; i++) {
    int ptr = 0;
    for (int foo = 0; foo < deg[i]; foo++) {
      int low = ptr, high = (int) g[i].size() - 1, p = 0;
      while (low <= high) {
        int mid = (low + high) >> 1;
        vector<int> qv;
        dsu d(n);
        for (int j = ptr; j <= mid; j++) {
          qv.push_back(g[i][j]);
          d.unite(u[g[i][j]], v[g[i][j]]);
        }
        int ft = 0;
        for (int e : tree) {
          if (d.unite(u[e], v[e])) {
            qv.push_back(e);
            ft += state[e];
          }
        }
        if (count_common_roads(qv) - ft > 0) {
          p = mid;
          high = mid - 1;
        } else {
          low = mid + 1;
        }
      }
      res.push_back(g[i][p]);
      ptr = p + 1;
    }
  }
  sort(res.begin(), res.end());
  res.erase(unique(res.begin(), res.end()), res.end());
  return res;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 2 ms 348 KB correct
15 Correct 2 ms 348 KB correct
16 Correct 2 ms 348 KB correct
17 Correct 1 ms 348 KB correct
18 Correct 1 ms 344 KB correct
19 Correct 1 ms 348 KB correct
20 Correct 2 ms 348 KB correct
21 Correct 1 ms 344 KB correct
22 Correct 2 ms 348 KB correct
23 Correct 1 ms 348 KB correct
24 Correct 2 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 2 ms 348 KB correct
28 Correct 1 ms 348 KB correct
29 Correct 1 ms 348 KB correct
30 Correct 1 ms 348 KB correct
31 Correct 1 ms 348 KB correct
32 Correct 1 ms 440 KB correct
33 Correct 1 ms 348 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 2 ms 348 KB correct
15 Correct 2 ms 348 KB correct
16 Correct 2 ms 348 KB correct
17 Correct 1 ms 348 KB correct
18 Correct 1 ms 344 KB correct
19 Correct 1 ms 348 KB correct
20 Correct 2 ms 348 KB correct
21 Correct 1 ms 344 KB correct
22 Correct 2 ms 348 KB correct
23 Correct 1 ms 348 KB correct
24 Correct 2 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 2 ms 348 KB correct
28 Correct 1 ms 348 KB correct
29 Correct 1 ms 348 KB correct
30 Correct 1 ms 348 KB correct
31 Correct 1 ms 348 KB correct
32 Correct 1 ms 440 KB correct
33 Correct 1 ms 348 KB correct
34 Correct 43 ms 1372 KB correct
35 Correct 42 ms 1372 KB correct
36 Correct 37 ms 1260 KB correct
37 Correct 11 ms 344 KB correct
38 Correct 42 ms 1520 KB correct
39 Correct 40 ms 1368 KB correct
40 Correct 34 ms 1116 KB correct
41 Correct 38 ms 1372 KB correct
42 Correct 38 ms 1508 KB correct
43 Correct 25 ms 1116 KB correct
44 Correct 21 ms 860 KB correct
45 Correct 23 ms 860 KB correct
46 Correct 26 ms 856 KB correct
47 Correct 14 ms 600 KB correct
48 Correct 4 ms 508 KB correct
49 Correct 7 ms 548 KB correct
50 Correct 13 ms 604 KB correct
51 Correct 25 ms 1000 KB correct
52 Correct 21 ms 860 KB correct
53 Correct 20 ms 860 KB correct
54 Correct 25 ms 856 KB correct
55 Correct 25 ms 860 KB correct
56 Correct 24 ms 860 KB correct
57 Correct 25 ms 1012 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 133 ms 2964 KB correct
4 Correct 211 ms 4084 KB correct
5 Correct 215 ms 3928 KB correct
6 Correct 197 ms 3932 KB correct
7 Correct 188 ms 3928 KB correct
8 Correct 190 ms 4080 KB correct
9 Correct 208 ms 3928 KB correct
10 Correct 219 ms 4080 KB correct
11 Correct 210 ms 3928 KB correct
12 Correct 218 ms 3932 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 203 ms 4100 KB correct
15 Correct 219 ms 4076 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 2 ms 348 KB correct
15 Correct 2 ms 348 KB correct
16 Correct 2 ms 348 KB correct
17 Correct 1 ms 348 KB correct
18 Correct 1 ms 344 KB correct
19 Correct 1 ms 348 KB correct
20 Correct 2 ms 348 KB correct
21 Correct 1 ms 344 KB correct
22 Correct 2 ms 348 KB correct
23 Correct 1 ms 348 KB correct
24 Correct 2 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 2 ms 348 KB correct
28 Correct 1 ms 348 KB correct
29 Correct 1 ms 348 KB correct
30 Correct 1 ms 348 KB correct
31 Correct 1 ms 348 KB correct
32 Correct 1 ms 440 KB correct
33 Correct 1 ms 348 KB correct
34 Correct 43 ms 1372 KB correct
35 Correct 42 ms 1372 KB correct
36 Correct 37 ms 1260 KB correct
37 Correct 11 ms 344 KB correct
38 Correct 42 ms 1520 KB correct
39 Correct 40 ms 1368 KB correct
40 Correct 34 ms 1116 KB correct
41 Correct 38 ms 1372 KB correct
42 Correct 38 ms 1508 KB correct
43 Correct 25 ms 1116 KB correct
44 Correct 21 ms 860 KB correct
45 Correct 23 ms 860 KB correct
46 Correct 26 ms 856 KB correct
47 Correct 14 ms 600 KB correct
48 Correct 4 ms 508 KB correct
49 Correct 7 ms 548 KB correct
50 Correct 13 ms 604 KB correct
51 Correct 25 ms 1000 KB correct
52 Correct 21 ms 860 KB correct
53 Correct 20 ms 860 KB correct
54 Correct 25 ms 856 KB correct
55 Correct 25 ms 860 KB correct
56 Correct 24 ms 860 KB correct
57 Correct 25 ms 1012 KB correct
58 Correct 0 ms 348 KB correct
59 Correct 0 ms 348 KB correct
60 Correct 133 ms 2964 KB correct
61 Correct 211 ms 4084 KB correct
62 Correct 215 ms 3928 KB correct
63 Correct 197 ms 3932 KB correct
64 Correct 188 ms 3928 KB correct
65 Correct 190 ms 4080 KB correct
66 Correct 208 ms 3928 KB correct
67 Correct 219 ms 4080 KB correct
68 Correct 210 ms 3928 KB correct
69 Correct 218 ms 3932 KB correct
70 Correct 0 ms 348 KB correct
71 Correct 203 ms 4100 KB correct
72 Correct 219 ms 4076 KB correct
73 Correct 0 ms 344 KB correct
74 Incorrect 202 ms 5000 KB WA in grader: NO
75 Halted 0 ms 0 KB -