Submission #317493

# Submission time Handle Problem Language Result Execution time Memory
317493 2020-10-29T19:42:00 Z kdh9949 Simurgh (IOI17_simurgh) C++17
100 / 100
125 ms 8312 KB
#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;

using vint = vector<int>;
using vll = vector<ll>;
using vld = vector<ld>;
using vpii = vector<pii>;
using vpil = vector<pil>;
using vpli = vector<pli>;
using vpll = vector<pll>;

#define x first
#define y second
#define all(v) v.begin(),v.end()

vint find_roads(int n, vint u, vint v) {
  int m = u.size();

  vector<vpii> e(n);
  for(int i = 0; i < m; i++) {
    e[u[i]].emplace_back(v[i], i);
    e[v[i]].emplace_back(u[i], i);
  }

  vint ord(n), low(n), tv;
  vpii par(n);
  vector<vpii> te(n), be(n);
  function<void(int, int)> f = [&](int x, int y) {
    static int cnt = 0;
    ord[x] = low[x] = ++cnt;
    for(pii &p : e[x]) {
      if(y == p.x) par[x] = p;
      else if(!ord[p.x]) {
        f(p.x, x);
        te[x].push_back(p);
        tv.push_back(p.y);
        low[x] = min(low[x], low[p.x]);
      }
      else if(ord[x] > ord[p.x]) {
        be[x].push_back(p);
        low[x] = min(low[x], ord[p.x]);
      }
    }
  };
  f(0, 0);

  auto rep1 = [&](int x, int y) {
    vint v = tv;
    *find(all(v), x) = y;
    return v;
  };

  int T = count_common_roads(tv);
  vint ans(m, -1);
  function<void(int)> g = [&](int x) {
    for(pii &p : te[x]) g(p.x);
    if(x && low[x] == ord[x]) {
      ans[par[x].y] = 1;
      return;
    }
    if(be[x].empty()) return;
    sort(all(be[x]), [&](pii &p, pii &q) {
      return ord[p.x] > ord[q.x];
    });
    pii B = be[x].back();
    if(ans[par[x].y] >= 0) {
      ans[B.y] = count_common_roads(rep1(par[x].y, B.y)) - T + ans[par[x].y];
      for(int t = par[x].x; t != B.x; t = par[t].x) {
        if(ans[par[t].y] < 0)
          ans[par[t].y] = T - count_common_roads(rep1(par[t].y, B.y)) + ans[B.y];
      }
    }
    else {
      int mn = 0, mx = 0;
      for(int t = x; t != B.x; t = par[t].x) {
        ans[par[t].y] = count_common_roads(rep1(par[t].y, B.y)) - T;
        mn = min(mn, ans[par[t].y]);
        mx = max(mx, ans[par[t].y]);
      }
      ans[B.y] = mx;
      if(mn < mx) {
        for(int t = x; t != B.x; t = par[t].x) ans[par[t].y] = mx - ans[par[t].y];
      }
    }
    be[x].pop_back();
  };
  g(0);

  vint chk(m), val(m);
  int ts = 0;
  function<void(vint&, vint&, int, int, int)> h =
  [&](vint &v, vint &w, int s, int e, int c) {
    if(c == 0 || c == e - s + 1) {
      for(int i = s; i <= e; i++) ans[v[i]] = !!c;
      return;
    }
    ts++;
    int m = (s + e) / 2;
    for(int i = s; i <= m; i++) {
      chk[w[i]] = ts;
      val[w[i]] = v[i];
    }
    vint cv = tv;
    int ofs = 0;
    for(int &x : cv) if(chk[x] == ts) {
      ofs += ans[x];
      x = val[x];
    }
    int lc = count_common_roads(cv) - T + ofs;
    h(v, w, s, m, lc);
    h(v, w, m + 1, e, c - lc);
  };

  for(int x = 1; x < n; x++) {
    if(be[x].empty()) continue;
    vint v, w;
    int sz = be[x].size();
    for(int i = 0, t = x; i < sz; i++) {
      v.push_back(be[x][i].y);
      w.push_back(par[t].y);
      t = be[x][i].x;
    }

    ts++;
    for(int i = 0; i < sz; i++) {
      chk[w[i]] = ts;
      val[w[i]] = v[i];
    }
    vint cv = tv;
    int ofs = 0;
    for(int &y : cv) if(chk[y] == ts) {
      ofs += ans[y];
      y = val[y];
    }
    h(v, w, 0, sz - 1, count_common_roads(cv) - T + ofs);
  }

  vint ret;
  for(int i = 0; i < m; i++) if(ans[i]) ret.push_back(i);
  return ret;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB correct
2 Correct 1 ms 384 KB correct
3 Correct 1 ms 256 KB correct
4 Correct 1 ms 384 KB correct
5 Correct 1 ms 256 KB correct
6 Correct 1 ms 256 KB correct
7 Correct 1 ms 256 KB correct
8 Correct 1 ms 256 KB correct
9 Correct 1 ms 256 KB correct
10 Correct 1 ms 256 KB correct
11 Correct 1 ms 256 KB correct
12 Correct 1 ms 256 KB correct
13 Correct 1 ms 384 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB correct
2 Correct 1 ms 384 KB correct
3 Correct 1 ms 256 KB correct
4 Correct 1 ms 384 KB correct
5 Correct 1 ms 256 KB correct
6 Correct 1 ms 256 KB correct
7 Correct 1 ms 256 KB correct
8 Correct 1 ms 256 KB correct
9 Correct 1 ms 256 KB correct
10 Correct 1 ms 256 KB correct
11 Correct 1 ms 256 KB correct
12 Correct 1 ms 256 KB correct
13 Correct 1 ms 384 KB correct
14 Correct 2 ms 384 KB correct
15 Correct 2 ms 384 KB correct
16 Correct 2 ms 384 KB correct
17 Correct 1 ms 384 KB correct
18 Correct 1 ms 384 KB correct
19 Correct 2 ms 384 KB correct
20 Correct 1 ms 384 KB correct
21 Correct 1 ms 512 KB correct
22 Correct 1 ms 384 KB correct
23 Correct 1 ms 384 KB correct
24 Correct 1 ms 512 KB correct
25 Correct 1 ms 384 KB correct
26 Correct 1 ms 384 KB correct
27 Correct 1 ms 384 KB correct
28 Correct 1 ms 384 KB correct
29 Correct 1 ms 384 KB correct
30 Correct 1 ms 384 KB correct
31 Correct 1 ms 416 KB correct
32 Correct 1 ms 384 KB correct
33 Correct 1 ms 384 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB correct
2 Correct 1 ms 384 KB correct
3 Correct 1 ms 256 KB correct
4 Correct 1 ms 384 KB correct
5 Correct 1 ms 256 KB correct
6 Correct 1 ms 256 KB correct
7 Correct 1 ms 256 KB correct
8 Correct 1 ms 256 KB correct
9 Correct 1 ms 256 KB correct
10 Correct 1 ms 256 KB correct
11 Correct 1 ms 256 KB correct
12 Correct 1 ms 256 KB correct
13 Correct 1 ms 384 KB correct
14 Correct 2 ms 384 KB correct
15 Correct 2 ms 384 KB correct
16 Correct 2 ms 384 KB correct
17 Correct 1 ms 384 KB correct
18 Correct 1 ms 384 KB correct
19 Correct 2 ms 384 KB correct
20 Correct 1 ms 384 KB correct
21 Correct 1 ms 512 KB correct
22 Correct 1 ms 384 KB correct
23 Correct 1 ms 384 KB correct
24 Correct 1 ms 512 KB correct
25 Correct 1 ms 384 KB correct
26 Correct 1 ms 384 KB correct
27 Correct 1 ms 384 KB correct
28 Correct 1 ms 384 KB correct
29 Correct 1 ms 384 KB correct
30 Correct 1 ms 384 KB correct
31 Correct 1 ms 416 KB correct
32 Correct 1 ms 384 KB correct
33 Correct 1 ms 384 KB correct
34 Correct 25 ms 2304 KB correct
35 Correct 25 ms 2176 KB correct
36 Correct 22 ms 1792 KB correct
37 Correct 29 ms 512 KB correct
38 Correct 26 ms 2176 KB correct
39 Correct 23 ms 2048 KB correct
40 Correct 21 ms 1792 KB correct
41 Correct 16 ms 2176 KB correct
42 Correct 22 ms 2176 KB correct
43 Correct 14 ms 1536 KB correct
44 Correct 13 ms 1152 KB correct
45 Correct 14 ms 1280 KB correct
46 Correct 12 ms 1280 KB correct
47 Correct 8 ms 768 KB correct
48 Correct 3 ms 384 KB correct
49 Correct 6 ms 512 KB correct
50 Correct 9 ms 768 KB correct
51 Correct 14 ms 1312 KB correct
52 Correct 13 ms 1152 KB correct
53 Correct 12 ms 1152 KB correct
54 Correct 14 ms 1480 KB correct
55 Correct 15 ms 1408 KB correct
56 Correct 13 ms 1280 KB correct
57 Correct 13 ms 1280 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB correct
2 Correct 1 ms 256 KB correct
3 Correct 72 ms 5752 KB correct
4 Correct 114 ms 8312 KB correct
5 Correct 116 ms 8184 KB correct
6 Correct 116 ms 8312 KB correct
7 Correct 99 ms 8312 KB correct
8 Correct 90 ms 8188 KB correct
9 Correct 115 ms 8184 KB correct
10 Correct 118 ms 8184 KB correct
11 Correct 112 ms 8184 KB correct
12 Correct 120 ms 8172 KB correct
13 Correct 1 ms 256 KB correct
14 Correct 107 ms 8184 KB correct
15 Correct 116 ms 8184 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB correct
2 Correct 1 ms 384 KB correct
3 Correct 1 ms 256 KB correct
4 Correct 1 ms 384 KB correct
5 Correct 1 ms 256 KB correct
6 Correct 1 ms 256 KB correct
7 Correct 1 ms 256 KB correct
8 Correct 1 ms 256 KB correct
9 Correct 1 ms 256 KB correct
10 Correct 1 ms 256 KB correct
11 Correct 1 ms 256 KB correct
12 Correct 1 ms 256 KB correct
13 Correct 1 ms 384 KB correct
14 Correct 2 ms 384 KB correct
15 Correct 2 ms 384 KB correct
16 Correct 2 ms 384 KB correct
17 Correct 1 ms 384 KB correct
18 Correct 1 ms 384 KB correct
19 Correct 2 ms 384 KB correct
20 Correct 1 ms 384 KB correct
21 Correct 1 ms 512 KB correct
22 Correct 1 ms 384 KB correct
23 Correct 1 ms 384 KB correct
24 Correct 1 ms 512 KB correct
25 Correct 1 ms 384 KB correct
26 Correct 1 ms 384 KB correct
27 Correct 1 ms 384 KB correct
28 Correct 1 ms 384 KB correct
29 Correct 1 ms 384 KB correct
30 Correct 1 ms 384 KB correct
31 Correct 1 ms 416 KB correct
32 Correct 1 ms 384 KB correct
33 Correct 1 ms 384 KB correct
34 Correct 25 ms 2304 KB correct
35 Correct 25 ms 2176 KB correct
36 Correct 22 ms 1792 KB correct
37 Correct 29 ms 512 KB correct
38 Correct 26 ms 2176 KB correct
39 Correct 23 ms 2048 KB correct
40 Correct 21 ms 1792 KB correct
41 Correct 16 ms 2176 KB correct
42 Correct 22 ms 2176 KB correct
43 Correct 14 ms 1536 KB correct
44 Correct 13 ms 1152 KB correct
45 Correct 14 ms 1280 KB correct
46 Correct 12 ms 1280 KB correct
47 Correct 8 ms 768 KB correct
48 Correct 3 ms 384 KB correct
49 Correct 6 ms 512 KB correct
50 Correct 9 ms 768 KB correct
51 Correct 14 ms 1312 KB correct
52 Correct 13 ms 1152 KB correct
53 Correct 12 ms 1152 KB correct
54 Correct 14 ms 1480 KB correct
55 Correct 15 ms 1408 KB correct
56 Correct 13 ms 1280 KB correct
57 Correct 13 ms 1280 KB correct
58 Correct 1 ms 256 KB correct
59 Correct 1 ms 256 KB correct
60 Correct 72 ms 5752 KB correct
61 Correct 114 ms 8312 KB correct
62 Correct 116 ms 8184 KB correct
63 Correct 116 ms 8312 KB correct
64 Correct 99 ms 8312 KB correct
65 Correct 90 ms 8188 KB correct
66 Correct 115 ms 8184 KB correct
67 Correct 118 ms 8184 KB correct
68 Correct 112 ms 8184 KB correct
69 Correct 120 ms 8172 KB correct
70 Correct 1 ms 256 KB correct
71 Correct 107 ms 8184 KB correct
72 Correct 116 ms 8184 KB correct
73 Correct 1 ms 256 KB correct
74 Correct 117 ms 8184 KB correct
75 Correct 116 ms 8060 KB correct
76 Correct 64 ms 3320 KB correct
77 Correct 119 ms 8184 KB correct
78 Correct 119 ms 8312 KB correct
79 Correct 125 ms 8184 KB correct
80 Correct 121 ms 8056 KB correct
81 Correct 113 ms 7416 KB correct
82 Correct 113 ms 8056 KB correct
83 Correct 91 ms 4600 KB correct
84 Correct 66 ms 5624 KB correct
85 Correct 63 ms 5112 KB correct
86 Correct 52 ms 3448 KB correct
87 Correct 47 ms 2808 KB correct
88 Correct 41 ms 2048 KB correct
89 Correct 42 ms 2168 KB correct
90 Correct 38 ms 1792 KB correct
91 Correct 18 ms 512 KB correct
92 Correct 8 ms 512 KB correct
93 Correct 67 ms 5116 KB correct
94 Correct 55 ms 3704 KB correct
95 Correct 55 ms 3448 KB correct
96 Correct 41 ms 1976 KB correct
97 Correct 42 ms 2040 KB correct
98 Correct 48 ms 2808 KB correct
99 Correct 42 ms 2176 KB correct
100 Correct 27 ms 896 KB correct
101 Correct 10 ms 512 KB correct
102 Correct 56 ms 4344 KB correct
103 Correct 58 ms 4472 KB correct
104 Correct 58 ms 4344 KB correct
105 Correct 55 ms 4344 KB correct