답안 #224461

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
224461 2020-04-18T00:19:34 Z rama_pang Simurgh (IOI17_simurgh) C++14
51 / 100
213 ms 2040 KB
#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;

class DisjointSet {
 private:
  vector<int> p;
  vector<int> sz;

 public:
  DisjointSet() {}
  DisjointSet(int n) {
    p.assign(n, 0);
    iota(begin(p), end(p), 0);
    sz.assign(n, 1);
  }

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

  bool Unite(int x, int y) {
    x = Find(x), y = Find(y);
    if (x == y) return false;
    if (sz[x] > sz[y]) swap(x, y);
    p[x] = y;
    sz[y] += sz[x];
    return true;
  }

  bool SameSet(int x, int y) {
    return Find(x) == Find(y);
  }
};

vector<int> find_roads(int n, vector<int> u, vector<int> v) {
  int m = u.size();
  vector<int> is_royal(m, -1); // undetermined: -1, not royal: 0, royal: 1

  DisjointSet dsu(n);
  vector<int> T; // Spanning Tree of G
  vector<vector<int>> Tadj(n);
  vector<int> Tpar(n, -1);
  vector<int> Tdepth(n, 0);

  vector<bool> inT(m, false);
  for (int i = 0; i < m; i++) {
    if (dsu.Unite(u[i], v[i])) {
      inT[i] = true;
      T.emplace_back(i);
      Tadj[u[i]].emplace_back(i);
      Tadj[v[i]].emplace_back(i);
    }
  }

  int Troyal = count_common_roads(T);

  function<void(int, int)> dfs = [&](int cur, int p) {
    for (auto e : Tadj[cur]) {
      int nxt = (u[e] == cur ? v[e] : u[e]);
      if (nxt != p) {
        Tpar[nxt] = e;
        Tdepth[nxt] = Tdepth[cur] + 1;
        dfs(nxt, cur);
      }
    }
  };

  function<void(int)> determine = [&](int edge) {
    function<int(vector<int> T_, int, int)> Query = 
        [&](vector<int> T_, int remove, int add) {
      replace(begin(T_), end(T_), remove, add);
      return count_common_roads(T_);
    };

    vector<int> C; // Cycle
    int a = u[edge], b = v[edge];

    // Get edges of T in the cycle 
    if (Tdepth[a] > Tdepth[b]) swap(a, b);
    while (Tdepth[b] > Tdepth[a]) {
      C.emplace_back(Tpar[b]);
      b = (u[Tpar[b]] == b ? v[Tpar[b]] : u[Tpar[b]]);
    }
    while (a != b) {
      C.emplace_back(Tpar[a]);
      C.emplace_back(Tpar[b]);
      a = (u[Tpar[a]] == a ? v[Tpar[a]] : u[Tpar[a]]);
      b = (u[Tpar[b]] == b ? v[Tpar[b]] : u[Tpar[b]]);
    }

    // check if there is an already determined edge
    for (auto e : C) {
      if (is_royal[edge] == -1 && is_royal[e] != -1) { // there is an already determined edge
        int Nroyal = Query(T, e, edge);
        if (Nroyal == Troyal) {
          is_royal[edge] = is_royal[e];
        } else {
          is_royal[edge] = is_royal[e] ? 0 : 1;
        }
      }
    }

    // all edges are undetermined, check each of them
    if (is_royal[edge] == -1) {
      vector<int> same;
      for (auto e : C) {
        int Nroyal = Query(T, e, edge);
        if (Nroyal == Troyal) {
          same.emplace_back(e); 
        } else if (Nroyal < Troyal) { // e is a royal road while edge isn't
          is_royal[e] = 1;
          is_royal[edge] = 0;
          break;
        } else if (Nroyal > Troyal) { // e isn't a royal road while edge is
          is_royal[e] = 0;
          is_royal[edge] = 1;
          break;
        }
      } 

      if (is_royal[edge] == -1) { // royal roads will never form a cycle
        is_royal[edge] = 0;
      }

      // update edges which have the same royality as edge
      for (auto e : same) {
        is_royal[e] = is_royal[edge];
      }
    }

    // we have determined edge, now to determine other edges in the cycle
    for (auto e : C) {
      if (is_royal[e] == -1) {
        int Nroyal = Query(T, e, edge);
        if (Nroyal == Troyal) {
          is_royal[e] = is_royal[edge];
        } else {
          is_royal[e] = is_royal[edge] ? 0 : 1; 
        }
      }
    }

    // for (auto e : C) {
    //   dsu.Unite(u[e], v[e]);
    // }
  };

  dfs(0, -1);
  dsu = DisjointSet(n); // determine whether edges from u[i] and v[i] are all determined

  for (int i = 0; i < m; i++) {
    if (inT[i]) continue; // edge is in T
    if (dsu.SameSet(u[i], v[i])) continue; // all edges from u to v is already determined
    determine(i); // determine royalities of cycle from u[i] to v[i]
  }

  for (auto e : T) {
    if (is_royal[e] == -1) { // if it is not yet determined, that means e is a bridge
      is_royal[e] = 1;
    }
  }


  vector<vector<int>> adj(n);
  for (int i = 0; i < m; i++) {
    if (inT[i]) continue;
    adj[u[i]].emplace_back(i);
    adj[v[i]].emplace_back(i);
  }

  function<void(int)> find_royal = [&](int vertex) {
    function<int(vector<int>, vector<int>)> Query = 
        [&](vector<int> T_, vector<int> E) { // complete the golden set E with edges from T_
      DisjointSet d(n);
      for (auto e : E) {
        d.Unite(u[e], v[e]);
      }

      int royals_from_T = 0;
      for (auto e : T_) {
        if (d.Unite(u[e], v[e])) {
          royals_from_T += is_royal[e];
          E.emplace_back(e);
        }
      }

      return count_common_roads(E) - royals_from_T;
    };

    for (int e = 0; e < m; e++) {
      if (is_royal[e] == -1) {
        is_royal[e] = Query(T, {e});
      }
    }
    return;

    vector<int> adjacent;
    for (auto e : adj[vertex]) {
      if (u[e] == vertex) {
        adjacent.emplace_back(e);
      }
    }

    int count_royals = Query(T, adjacent);
    for (int L = 0; count_royals > 0; count_royals--) {
      int lo = L;
      int hi = (int) adjacent.size() - 1;

      while (lo < hi) {
        int mid = (lo + hi) / 2;
        vector<int> E(begin(adjacent) + L, begin(adjacent) + mid + 1);
        if (Query(T, E) > 0) {
          hi = mid;
        } else {
          lo = mid + 1;
        }
      }

      is_royal[adjacent[lo]] = 1;
      L = lo + 1;
    }

    for (auto e : adjacent) {
      if (is_royal[e] == -1) {
        is_royal[e] = 0;
      }
    }
  };

  // we have determined all roads in T, determine the remaining royal roads with binary search
  for (int i = 0; i < n; i++) {
    find_royal(i);
  }

  vector<int> royal_roads;
  for (int i = 0; i < m; i++) {
    if (is_royal[i] == 1) {
      royal_roads.emplace_back(i);
    }
  }
  
  return royal_roads;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 256 KB correct
2 Correct 5 ms 384 KB correct
3 Correct 4 ms 256 KB correct
4 Correct 5 ms 256 KB correct
5 Correct 4 ms 256 KB correct
6 Correct 5 ms 256 KB correct
7 Correct 5 ms 256 KB correct
8 Correct 4 ms 256 KB correct
9 Correct 5 ms 384 KB correct
10 Correct 5 ms 256 KB correct
11 Correct 4 ms 384 KB correct
12 Correct 5 ms 256 KB correct
13 Correct 5 ms 256 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 256 KB correct
2 Correct 5 ms 384 KB correct
3 Correct 4 ms 256 KB correct
4 Correct 5 ms 256 KB correct
5 Correct 4 ms 256 KB correct
6 Correct 5 ms 256 KB correct
7 Correct 5 ms 256 KB correct
8 Correct 4 ms 256 KB correct
9 Correct 5 ms 384 KB correct
10 Correct 5 ms 256 KB correct
11 Correct 4 ms 384 KB correct
12 Correct 5 ms 256 KB correct
13 Correct 5 ms 256 KB correct
14 Correct 7 ms 384 KB correct
15 Correct 7 ms 384 KB correct
16 Correct 7 ms 384 KB correct
17 Correct 7 ms 256 KB correct
18 Correct 5 ms 384 KB correct
19 Correct 7 ms 384 KB correct
20 Correct 6 ms 384 KB correct
21 Correct 6 ms 384 KB correct
22 Correct 6 ms 384 KB correct
23 Correct 6 ms 384 KB correct
24 Correct 6 ms 384 KB correct
25 Correct 5 ms 256 KB correct
26 Correct 6 ms 384 KB correct
27 Correct 6 ms 384 KB correct
28 Correct 5 ms 384 KB correct
29 Correct 5 ms 384 KB correct
30 Correct 6 ms 384 KB correct
31 Correct 6 ms 384 KB correct
32 Correct 6 ms 384 KB correct
33 Correct 6 ms 384 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 256 KB correct
2 Correct 5 ms 384 KB correct
3 Correct 4 ms 256 KB correct
4 Correct 5 ms 256 KB correct
5 Correct 4 ms 256 KB correct
6 Correct 5 ms 256 KB correct
7 Correct 5 ms 256 KB correct
8 Correct 4 ms 256 KB correct
9 Correct 5 ms 384 KB correct
10 Correct 5 ms 256 KB correct
11 Correct 4 ms 384 KB correct
12 Correct 5 ms 256 KB correct
13 Correct 5 ms 256 KB correct
14 Correct 7 ms 384 KB correct
15 Correct 7 ms 384 KB correct
16 Correct 7 ms 384 KB correct
17 Correct 7 ms 256 KB correct
18 Correct 5 ms 384 KB correct
19 Correct 7 ms 384 KB correct
20 Correct 6 ms 384 KB correct
21 Correct 6 ms 384 KB correct
22 Correct 6 ms 384 KB correct
23 Correct 6 ms 384 KB correct
24 Correct 6 ms 384 KB correct
25 Correct 5 ms 256 KB correct
26 Correct 6 ms 384 KB correct
27 Correct 6 ms 384 KB correct
28 Correct 5 ms 384 KB correct
29 Correct 5 ms 384 KB correct
30 Correct 6 ms 384 KB correct
31 Correct 6 ms 384 KB correct
32 Correct 6 ms 384 KB correct
33 Correct 6 ms 384 KB correct
34 Correct 206 ms 1376 KB correct
35 Correct 212 ms 1272 KB correct
36 Correct 145 ms 1176 KB correct
37 Correct 15 ms 384 KB correct
38 Correct 203 ms 1400 KB correct
39 Correct 189 ms 1272 KB correct
40 Correct 139 ms 1272 KB correct
41 Correct 212 ms 1400 KB correct
42 Correct 213 ms 1528 KB correct
43 Correct 104 ms 1016 KB correct
44 Correct 86 ms 888 KB correct
45 Correct 105 ms 888 KB correct
46 Correct 77 ms 760 KB correct
47 Correct 34 ms 512 KB correct
48 Correct 7 ms 384 KB correct
49 Correct 13 ms 384 KB correct
50 Correct 34 ms 512 KB correct
51 Correct 103 ms 888 KB correct
52 Correct 88 ms 760 KB correct
53 Correct 80 ms 760 KB correct
54 Correct 107 ms 1016 KB correct
55 Correct 97 ms 1016 KB correct
56 Correct 93 ms 888 KB correct
57 Correct 92 ms 888 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 256 KB correct
2 Correct 5 ms 256 KB correct
3 Incorrect 164 ms 2040 KB WA in grader: NO
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 256 KB correct
2 Correct 5 ms 384 KB correct
3 Correct 4 ms 256 KB correct
4 Correct 5 ms 256 KB correct
5 Correct 4 ms 256 KB correct
6 Correct 5 ms 256 KB correct
7 Correct 5 ms 256 KB correct
8 Correct 4 ms 256 KB correct
9 Correct 5 ms 384 KB correct
10 Correct 5 ms 256 KB correct
11 Correct 4 ms 384 KB correct
12 Correct 5 ms 256 KB correct
13 Correct 5 ms 256 KB correct
14 Correct 7 ms 384 KB correct
15 Correct 7 ms 384 KB correct
16 Correct 7 ms 384 KB correct
17 Correct 7 ms 256 KB correct
18 Correct 5 ms 384 KB correct
19 Correct 7 ms 384 KB correct
20 Correct 6 ms 384 KB correct
21 Correct 6 ms 384 KB correct
22 Correct 6 ms 384 KB correct
23 Correct 6 ms 384 KB correct
24 Correct 6 ms 384 KB correct
25 Correct 5 ms 256 KB correct
26 Correct 6 ms 384 KB correct
27 Correct 6 ms 384 KB correct
28 Correct 5 ms 384 KB correct
29 Correct 5 ms 384 KB correct
30 Correct 6 ms 384 KB correct
31 Correct 6 ms 384 KB correct
32 Correct 6 ms 384 KB correct
33 Correct 6 ms 384 KB correct
34 Correct 206 ms 1376 KB correct
35 Correct 212 ms 1272 KB correct
36 Correct 145 ms 1176 KB correct
37 Correct 15 ms 384 KB correct
38 Correct 203 ms 1400 KB correct
39 Correct 189 ms 1272 KB correct
40 Correct 139 ms 1272 KB correct
41 Correct 212 ms 1400 KB correct
42 Correct 213 ms 1528 KB correct
43 Correct 104 ms 1016 KB correct
44 Correct 86 ms 888 KB correct
45 Correct 105 ms 888 KB correct
46 Correct 77 ms 760 KB correct
47 Correct 34 ms 512 KB correct
48 Correct 7 ms 384 KB correct
49 Correct 13 ms 384 KB correct
50 Correct 34 ms 512 KB correct
51 Correct 103 ms 888 KB correct
52 Correct 88 ms 760 KB correct
53 Correct 80 ms 760 KB correct
54 Correct 107 ms 1016 KB correct
55 Correct 97 ms 1016 KB correct
56 Correct 93 ms 888 KB correct
57 Correct 92 ms 888 KB correct
58 Correct 4 ms 256 KB correct
59 Correct 5 ms 256 KB correct
60 Incorrect 164 ms 2040 KB WA in grader: NO
61 Halted 0 ms 0 KB -