Submission #1052763

# Submission time Handle Problem Language Result Execution time Memory
1052763 2024-08-10T20:29:44 Z MilosMilutinovic Simurgh (IOI17_simurgh) C++14
100 / 100
149 ms 5028 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> que;
  for (int i = 0; i < n; i++) {
    if (deg[i] == 1) {
      que.push_back(i);
    }
  }
  vector<bool> was(n);
  vector<int> res;
  for (int b = 0; b < (int) que.size(); b++) {
    int i = que[b];
    if (i == 0) {
      continue;
    }
    was[i] = true;
    vector<int> f;
    for (int e : g[i]) {
      if (!was[u[e] ^ v[e] ^ i]) {
        f.push_back(e);
      }
    }
    int low = 0, high = (int) f.size() - 1, who = -1;
    while (low <= high) {
      int mid = (low + high) >> 1;
      vector<int> qv;
      dsu d(n);
      for (int j = 0; j <= mid; j++) {
        qv.push_back(f[j]);
        d.unite(u[f[j]], v[f[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) {
        who = mid;
        high = mid - 1;
      } else {
        low = mid + 1;
      }
    }
    res.push_back(f[who]);
    int j = (u[f[who]] ^ v[f[who]] ^ i);
    deg[j] -= 1;
    if (deg[j] == 1) {
      que.push_back(j);
    }
  }
  return res;
}
# Verdict Execution time Memory Grader output
1 Correct 0 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 344 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 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 344 KB correct
14 Correct 1 ms 348 KB correct
15 Correct 1 ms 348 KB correct
16 Correct 1 ms 348 KB correct
17 Correct 1 ms 348 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 1 ms 348 KB correct
20 Correct 1 ms 348 KB correct
21 Correct 1 ms 344 KB correct
22 Correct 1 ms 348 KB correct
23 Correct 1 ms 348 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 1 ms 348 KB correct
28 Correct 0 ms 348 KB correct
29 Correct 0 ms 348 KB correct
30 Correct 1 ms 348 KB correct
31 Correct 1 ms 348 KB correct
32 Correct 1 ms 348 KB correct
33 Correct 1 ms 348 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 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 344 KB correct
14 Correct 1 ms 348 KB correct
15 Correct 1 ms 348 KB correct
16 Correct 1 ms 348 KB correct
17 Correct 1 ms 348 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 1 ms 348 KB correct
20 Correct 1 ms 348 KB correct
21 Correct 1 ms 344 KB correct
22 Correct 1 ms 348 KB correct
23 Correct 1 ms 348 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 1 ms 348 KB correct
28 Correct 0 ms 348 KB correct
29 Correct 0 ms 348 KB correct
30 Correct 1 ms 348 KB correct
31 Correct 1 ms 348 KB correct
32 Correct 1 ms 348 KB correct
33 Correct 1 ms 348 KB correct
34 Correct 27 ms 1116 KB correct
35 Correct 26 ms 1264 KB correct
36 Correct 22 ms 1116 KB correct
37 Correct 6 ms 344 KB correct
38 Correct 26 ms 1320 KB correct
39 Correct 24 ms 1116 KB correct
40 Correct 23 ms 1116 KB correct
41 Correct 26 ms 1312 KB correct
42 Correct 26 ms 1316 KB correct
43 Correct 14 ms 860 KB correct
44 Correct 12 ms 852 KB correct
45 Correct 14 ms 896 KB correct
46 Correct 11 ms 604 KB correct
47 Correct 7 ms 604 KB correct
48 Correct 3 ms 348 KB correct
49 Correct 4 ms 348 KB correct
50 Correct 8 ms 604 KB correct
51 Correct 14 ms 900 KB correct
52 Correct 13 ms 604 KB correct
53 Correct 12 ms 820 KB correct
54 Correct 15 ms 860 KB correct
55 Correct 18 ms 908 KB correct
56 Correct 18 ms 860 KB correct
57 Correct 15 ms 856 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 82 ms 2968 KB correct
4 Correct 149 ms 4076 KB correct
5 Correct 133 ms 3984 KB correct
6 Correct 127 ms 3928 KB correct
7 Correct 128 ms 4092 KB correct
8 Correct 129 ms 3932 KB correct
9 Correct 126 ms 3932 KB correct
10 Correct 128 ms 3932 KB correct
11 Correct 129 ms 3932 KB correct
12 Correct 135 ms 3932 KB correct
13 Correct 0 ms 344 KB correct
14 Correct 129 ms 3984 KB correct
15 Correct 124 ms 4084 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 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 344 KB correct
14 Correct 1 ms 348 KB correct
15 Correct 1 ms 348 KB correct
16 Correct 1 ms 348 KB correct
17 Correct 1 ms 348 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 1 ms 348 KB correct
20 Correct 1 ms 348 KB correct
21 Correct 1 ms 344 KB correct
22 Correct 1 ms 348 KB correct
23 Correct 1 ms 348 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 1 ms 348 KB correct
28 Correct 0 ms 348 KB correct
29 Correct 0 ms 348 KB correct
30 Correct 1 ms 348 KB correct
31 Correct 1 ms 348 KB correct
32 Correct 1 ms 348 KB correct
33 Correct 1 ms 348 KB correct
34 Correct 27 ms 1116 KB correct
35 Correct 26 ms 1264 KB correct
36 Correct 22 ms 1116 KB correct
37 Correct 6 ms 344 KB correct
38 Correct 26 ms 1320 KB correct
39 Correct 24 ms 1116 KB correct
40 Correct 23 ms 1116 KB correct
41 Correct 26 ms 1312 KB correct
42 Correct 26 ms 1316 KB correct
43 Correct 14 ms 860 KB correct
44 Correct 12 ms 852 KB correct
45 Correct 14 ms 896 KB correct
46 Correct 11 ms 604 KB correct
47 Correct 7 ms 604 KB correct
48 Correct 3 ms 348 KB correct
49 Correct 4 ms 348 KB correct
50 Correct 8 ms 604 KB correct
51 Correct 14 ms 900 KB correct
52 Correct 13 ms 604 KB correct
53 Correct 12 ms 820 KB correct
54 Correct 15 ms 860 KB correct
55 Correct 18 ms 908 KB correct
56 Correct 18 ms 860 KB correct
57 Correct 15 ms 856 KB correct
58 Correct 0 ms 348 KB correct
59 Correct 0 ms 348 KB correct
60 Correct 82 ms 2968 KB correct
61 Correct 149 ms 4076 KB correct
62 Correct 133 ms 3984 KB correct
63 Correct 127 ms 3928 KB correct
64 Correct 128 ms 4092 KB correct
65 Correct 129 ms 3932 KB correct
66 Correct 126 ms 3932 KB correct
67 Correct 128 ms 3932 KB correct
68 Correct 129 ms 3932 KB correct
69 Correct 135 ms 3932 KB correct
70 Correct 0 ms 344 KB correct
71 Correct 129 ms 3984 KB correct
72 Correct 124 ms 4084 KB correct
73 Correct 0 ms 344 KB correct
74 Correct 128 ms 3980 KB correct
75 Correct 126 ms 4692 KB correct
76 Correct 60 ms 2136 KB correct
77 Correct 135 ms 4860 KB correct
78 Correct 131 ms 4956 KB correct
79 Correct 126 ms 5028 KB correct
80 Correct 125 ms 4700 KB correct
81 Correct 118 ms 4184 KB correct
82 Correct 139 ms 4696 KB correct
83 Correct 97 ms 2908 KB correct
84 Correct 76 ms 3324 KB correct
85 Correct 72 ms 3160 KB correct
86 Correct 63 ms 2236 KB correct
87 Correct 44 ms 1676 KB correct
88 Correct 38 ms 1372 KB correct
89 Correct 37 ms 1236 KB correct
90 Correct 34 ms 1116 KB correct
91 Correct 13 ms 604 KB correct
92 Correct 11 ms 344 KB correct
93 Correct 80 ms 3164 KB correct
94 Correct 54 ms 2240 KB correct
95 Correct 53 ms 2136 KB correct
96 Correct 36 ms 1372 KB correct
97 Correct 41 ms 1372 KB correct
98 Correct 44 ms 1864 KB correct
99 Correct 39 ms 1372 KB correct
100 Correct 19 ms 604 KB correct
101 Correct 10 ms 604 KB correct
102 Correct 73 ms 2688 KB correct
103 Correct 74 ms 2648 KB correct
104 Correct 76 ms 2648 KB correct
105 Correct 76 ms 2652 KB correct