답안 #94564

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
94564 2019-01-21T00:41:27 Z wxh010910 Simurgh (IOI17_simurgh) C++17
100 / 100
793 ms 9720 KB
#include <bits/stdc++.h>
#include "simurgh.h"

using namespace std;

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

  dsu(int n): n(n) {
    p.resize(n);
    for (int i = 0; i < n; ++i) {
      p[i] = i;
    }
  }

  inline int find(int x) {
    while (x != p[x]) {
      x = p[x] = p[p[x]];
    }
    return x;
  }

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

vector<int> find_roads(int n, vector<int> u, vector<int> v) {
  int m = u.size();
  vector<int> color(m, -1);
  vector<int> tree;
  vector<int> ans;
  set<int> cur;
  auto query = [&](vector<int> edges) {
    dsu d(n);
    for (auto i : edges) {
      d.unite(u[i], v[i]);
    }
    int more = 0;
    for (auto i : tree) {
      if (d.unite(u[i], v[i])) {
        edges.push_back(i);
        more += color[i];
      }
    }
    return count_common_roads(edges) - more;
  };
  vector<vector<pair<int, int>>> adj(n);
  dsu d(n);
  for (int i = 0; i < m; ++i) {
    if (d.unite(u[i], v[i])) {
      adj[u[i]].emplace_back(v[i], i);
      adj[v[i]].emplace_back(u[i], i);
      tree.push_back(i);
    } else {
      cur.insert(i);
    }
  }
  vector<int> pv(n, -1);
  vector<int> pe(n, -1);
  vector<int> depth(n);
  function<void(int)> dfs = [&](int x) {
    for (auto p : adj[x]) {
      if (p.first != pv[x]) {
        pv[p.first] = x;
        pe[p.first] = p.second;
        depth[p.first] = depth[x] + 1;
        dfs(p.first);
      }
    }
  };
  dfs(0);
  auto get_path = [&](int x, int y) {
    vector<int> res;
    while (x != y) {
      if (depth[x] >= depth[y]) {
        res.push_back(pe[x]);
        x = pv[x];
      } else {
        res.push_back(pe[y]);
        y = pv[y];
      }
    }
    return res;
  };
  for (int i = 0; i < m; ++i) {
    vector<int> path = get_path(u[i], v[i]);
    if (path.size() == 1) {
      continue;
    }
    bool all_colored = true;
    for (auto j : path) {
      if (color[j] == -1) {
        all_colored = false;
        break;
      }
    }
    if (all_colored) {
      continue;
    }
    path.push_back(i);
    int max_common_edges = -n;
    for (auto j : path) {
      if (color[j] != -1) {
        vector<int> edges;
        for (auto k : path) {
          if (k != j) {
            edges.push_back(k);
          }
        }
        max_common_edges = query(edges) + color[j];
        break;
      }
    }
    vector<pair<int, int>> all;
    for (auto j : path) {
      if (color[j] == -1) {
        vector<int> edges;
        for (auto k : path) {
          if (k != j) {
            edges.push_back(k);
          }
        }
        int res = query(edges);
        max_common_edges = max(max_common_edges, res);
        all.emplace_back(j, res);
      }
    }
    for (auto p : all) {
      color[p.first] = p.second < max_common_edges;
    }
  }
  for (auto i : tree) {
    if (color[i] == -1) {
      color[i] = 1;
    }
    if (color[i]) {
      ans.push_back(i);
    }
  }
  function<void(vector<int>, int)> solve = [&](vector<int> e, int common) {
    int n = e.size();
    if (!common) {
      return;
    } else if (common == n) {
      for (auto i : e) {
        ans.push_back(i);
      }
    } else {
      vector<int> left(e.begin(), e.begin() + (n >> 1));
      vector<int> right(e.begin() + (n >> 1), e.end());
      int common_left = query(left);
      int common_right = common - common_left;
      solve(left, common_left);
      solve(right, common_right);
    }
  };
  for (int i = 0; i < n; ++i) {
    vector<int> useful;
    for (auto j : cur) {
      if (u[j] == i || v[j] == i) {
        useful.push_back(j);
      }
    }
    if (!useful.empty()) {
      for (auto j : useful) {
        cur.erase(j);
      }
      solve(useful, query(useful));
    }
  }
  sort(ans.begin(), ans.end());
  return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 376 KB correct
4 Correct 2 ms 376 KB correct
5 Correct 2 ms 256 KB correct
6 Correct 2 ms 376 KB correct
7 Correct 2 ms 376 KB correct
8 Correct 2 ms 376 KB correct
9 Correct 2 ms 256 KB correct
10 Correct 2 ms 256 KB correct
11 Correct 2 ms 376 KB correct
12 Correct 2 ms 256 KB correct
13 Correct 2 ms 256 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 376 KB correct
4 Correct 2 ms 376 KB correct
5 Correct 2 ms 256 KB correct
6 Correct 2 ms 376 KB correct
7 Correct 2 ms 376 KB correct
8 Correct 2 ms 376 KB correct
9 Correct 2 ms 256 KB correct
10 Correct 2 ms 256 KB correct
11 Correct 2 ms 376 KB correct
12 Correct 2 ms 256 KB correct
13 Correct 2 ms 256 KB correct
14 Correct 4 ms 376 KB correct
15 Correct 3 ms 376 KB correct
16 Correct 4 ms 376 KB correct
17 Correct 4 ms 376 KB correct
18 Correct 2 ms 376 KB correct
19 Correct 3 ms 376 KB correct
20 Correct 4 ms 376 KB correct
21 Correct 3 ms 376 KB correct
22 Correct 3 ms 376 KB correct
23 Correct 2 ms 376 KB correct
24 Correct 2 ms 376 KB correct
25 Correct 2 ms 376 KB correct
26 Correct 2 ms 376 KB correct
27 Correct 3 ms 376 KB correct
28 Correct 2 ms 376 KB correct
29 Correct 2 ms 256 KB correct
30 Correct 3 ms 376 KB correct
31 Correct 2 ms 376 KB correct
32 Correct 3 ms 376 KB correct
33 Correct 3 ms 376 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 376 KB correct
4 Correct 2 ms 376 KB correct
5 Correct 2 ms 256 KB correct
6 Correct 2 ms 376 KB correct
7 Correct 2 ms 376 KB correct
8 Correct 2 ms 376 KB correct
9 Correct 2 ms 256 KB correct
10 Correct 2 ms 256 KB correct
11 Correct 2 ms 376 KB correct
12 Correct 2 ms 256 KB correct
13 Correct 2 ms 256 KB correct
14 Correct 4 ms 376 KB correct
15 Correct 3 ms 376 KB correct
16 Correct 4 ms 376 KB correct
17 Correct 4 ms 376 KB correct
18 Correct 2 ms 376 KB correct
19 Correct 3 ms 376 KB correct
20 Correct 4 ms 376 KB correct
21 Correct 3 ms 376 KB correct
22 Correct 3 ms 376 KB correct
23 Correct 2 ms 376 KB correct
24 Correct 2 ms 376 KB correct
25 Correct 2 ms 376 KB correct
26 Correct 2 ms 376 KB correct
27 Correct 3 ms 376 KB correct
28 Correct 2 ms 376 KB correct
29 Correct 2 ms 256 KB correct
30 Correct 3 ms 376 KB correct
31 Correct 2 ms 376 KB correct
32 Correct 3 ms 376 KB correct
33 Correct 3 ms 376 KB correct
34 Correct 99 ms 2496 KB correct
35 Correct 191 ms 2396 KB correct
36 Correct 71 ms 1784 KB correct
37 Correct 14 ms 376 KB correct
38 Correct 94 ms 2476 KB correct
39 Correct 82 ms 2168 KB correct
40 Correct 67 ms 1784 KB correct
41 Correct 93 ms 2424 KB correct
42 Correct 93 ms 2424 KB correct
43 Correct 40 ms 1400 KB correct
44 Correct 35 ms 1144 KB correct
45 Correct 40 ms 1400 KB correct
46 Correct 33 ms 1144 KB correct
47 Correct 16 ms 632 KB correct
48 Correct 5 ms 376 KB correct
49 Correct 8 ms 532 KB correct
50 Correct 15 ms 636 KB correct
51 Correct 39 ms 1528 KB correct
52 Correct 34 ms 1144 KB correct
53 Correct 31 ms 1144 KB correct
54 Correct 41 ms 1400 KB correct
55 Correct 42 ms 1400 KB correct
56 Correct 38 ms 1432 KB correct
57 Correct 39 ms 1404 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 341 ms 6340 KB correct
4 Correct 624 ms 9676 KB correct
5 Correct 678 ms 9720 KB correct
6 Correct 660 ms 9592 KB correct
7 Correct 591 ms 9688 KB correct
8 Correct 569 ms 9720 KB correct
9 Correct 630 ms 9712 KB correct
10 Correct 625 ms 9680 KB correct
11 Correct 618 ms 9676 KB correct
12 Correct 641 ms 9592 KB correct
13 Correct 2 ms 376 KB correct
14 Correct 582 ms 9592 KB correct
15 Correct 598 ms 9676 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 376 KB correct
4 Correct 2 ms 376 KB correct
5 Correct 2 ms 256 KB correct
6 Correct 2 ms 376 KB correct
7 Correct 2 ms 376 KB correct
8 Correct 2 ms 376 KB correct
9 Correct 2 ms 256 KB correct
10 Correct 2 ms 256 KB correct
11 Correct 2 ms 376 KB correct
12 Correct 2 ms 256 KB correct
13 Correct 2 ms 256 KB correct
14 Correct 4 ms 376 KB correct
15 Correct 3 ms 376 KB correct
16 Correct 4 ms 376 KB correct
17 Correct 4 ms 376 KB correct
18 Correct 2 ms 376 KB correct
19 Correct 3 ms 376 KB correct
20 Correct 4 ms 376 KB correct
21 Correct 3 ms 376 KB correct
22 Correct 3 ms 376 KB correct
23 Correct 2 ms 376 KB correct
24 Correct 2 ms 376 KB correct
25 Correct 2 ms 376 KB correct
26 Correct 2 ms 376 KB correct
27 Correct 3 ms 376 KB correct
28 Correct 2 ms 376 KB correct
29 Correct 2 ms 256 KB correct
30 Correct 3 ms 376 KB correct
31 Correct 2 ms 376 KB correct
32 Correct 3 ms 376 KB correct
33 Correct 3 ms 376 KB correct
34 Correct 99 ms 2496 KB correct
35 Correct 191 ms 2396 KB correct
36 Correct 71 ms 1784 KB correct
37 Correct 14 ms 376 KB correct
38 Correct 94 ms 2476 KB correct
39 Correct 82 ms 2168 KB correct
40 Correct 67 ms 1784 KB correct
41 Correct 93 ms 2424 KB correct
42 Correct 93 ms 2424 KB correct
43 Correct 40 ms 1400 KB correct
44 Correct 35 ms 1144 KB correct
45 Correct 40 ms 1400 KB correct
46 Correct 33 ms 1144 KB correct
47 Correct 16 ms 632 KB correct
48 Correct 5 ms 376 KB correct
49 Correct 8 ms 532 KB correct
50 Correct 15 ms 636 KB correct
51 Correct 39 ms 1528 KB correct
52 Correct 34 ms 1144 KB correct
53 Correct 31 ms 1144 KB correct
54 Correct 41 ms 1400 KB correct
55 Correct 42 ms 1400 KB correct
56 Correct 38 ms 1432 KB correct
57 Correct 39 ms 1404 KB correct
58 Correct 2 ms 256 KB correct
59 Correct 2 ms 376 KB correct
60 Correct 341 ms 6340 KB correct
61 Correct 624 ms 9676 KB correct
62 Correct 678 ms 9720 KB correct
63 Correct 660 ms 9592 KB correct
64 Correct 591 ms 9688 KB correct
65 Correct 569 ms 9720 KB correct
66 Correct 630 ms 9712 KB correct
67 Correct 625 ms 9680 KB correct
68 Correct 618 ms 9676 KB correct
69 Correct 641 ms 9592 KB correct
70 Correct 2 ms 376 KB correct
71 Correct 582 ms 9592 KB correct
72 Correct 598 ms 9676 KB correct
73 Correct 2 ms 256 KB correct
74 Correct 661 ms 9668 KB correct
75 Correct 793 ms 9336 KB correct
76 Correct 232 ms 3576 KB correct
77 Correct 737 ms 9668 KB correct
78 Correct 597 ms 9596 KB correct
79 Correct 667 ms 9708 KB correct
80 Correct 605 ms 9436 KB correct
81 Correct 462 ms 7928 KB correct
82 Correct 687 ms 9380 KB correct
83 Correct 344 ms 5116 KB correct
84 Correct 317 ms 5880 KB correct
85 Correct 295 ms 5112 KB correct
86 Correct 184 ms 3676 KB correct
87 Correct 145 ms 2692 KB correct
88 Correct 111 ms 2168 KB correct
89 Correct 121 ms 2040 KB correct
90 Correct 97 ms 1912 KB correct
91 Correct 22 ms 504 KB correct
92 Correct 14 ms 376 KB correct
93 Correct 296 ms 5240 KB correct
94 Correct 189 ms 3712 KB correct
95 Correct 192 ms 3456 KB correct
96 Correct 103 ms 1940 KB correct
97 Correct 117 ms 2228 KB correct
98 Correct 145 ms 2812 KB correct
99 Correct 128 ms 2296 KB correct
100 Correct 42 ms 760 KB correct
101 Correct 16 ms 504 KB correct
102 Correct 271 ms 4984 KB correct
103 Correct 273 ms 4988 KB correct
104 Correct 254 ms 5024 KB correct
105 Correct 277 ms 4984 KB correct