結果

問題 No.1153 ねこちゃんゲーム
ユーザー keijakkeijak
提出日時 2021-06-22 14:20:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 7,148 bytes
コンパイル時間 3,425 ms
コンパイル使用メモリ 234,136 KB
実行使用メモリ 294,856 KB
最終ジャッジ日時 2023-09-05 15:42:15
合計ジャッジ時間 47,555 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
11,476 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 5 ms
4,376 KB
testcase_03 AC 5 ms
4,380 KB
testcase_04 AC 5 ms
4,376 KB
testcase_05 AC 5 ms
4,376 KB
testcase_06 AC 5 ms
4,376 KB
testcase_07 AC 1,030 ms
177,996 KB
testcase_08 AC 1,039 ms
178,300 KB
testcase_09 AC 1,031 ms
178,048 KB
testcase_10 AC 1,022 ms
178,160 KB
testcase_11 AC 1,035 ms
178,068 KB
testcase_12 AC 1,038 ms
178,152 KB
testcase_13 AC 1,045 ms
177,964 KB
testcase_14 AC 1,037 ms
178,140 KB
testcase_15 AC 1,028 ms
178,124 KB
testcase_16 AC 1,032 ms
178,152 KB
testcase_17 AC 1,058 ms
181,720 KB
testcase_18 AC 1,067 ms
185,064 KB
testcase_19 AC 1,050 ms
185,172 KB
testcase_20 AC 1,061 ms
192,104 KB
testcase_21 AC 1,064 ms
192,312 KB
testcase_22 AC 1,048 ms
180,532 KB
testcase_23 AC 1,047 ms
183,608 KB
testcase_24 AC 1,053 ms
183,360 KB
testcase_25 AC 1,057 ms
188,720 KB
testcase_26 AC 1,062 ms
188,556 KB
testcase_27 AC 1,247 ms
274,952 KB
testcase_28 AC 1,335 ms
294,856 KB
testcase_29 AC 1,311 ms
280,240 KB
testcase_30 AC 1,312 ms
280,900 KB
testcase_31 AC 1,217 ms
209,124 KB
testcase_32 TLE -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP_(i, a_, b_, a, b, ...) \
  for (int i = (a), END_##i = (b); i < END_##i; ++i)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define ALL(x) std::begin(x), std::end(x)
using i64 = long long;
using u64 = unsigned long long;

template <typename T, typename U>
inline bool chmax(T &a, U b) {
  return a < b and ((a = std::move(b)), true);
}
template <typename T, typename U>
inline bool chmin(T &a, U b) {
  return a > b and ((a = std::move(b)), true);
}
template <typename T>
inline int ssize(const T &a) {
  return (int)std::size(a);
}

template <typename T>
std::istream &operator>>(std::istream &is, std::vector<T> &a) {
  for (auto &x : a) is >> x;
  return is;
}
template <typename T, typename U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &a) {
  return os << "(" << a.first << ", " << a.second << ")";
}
template <typename Container>
std::ostream &print_seq(const Container &a, std::string_view sep = " ",
                        std::string_view ends = "\n",
                        std::ostream &os = std::cout) {
  auto b = std::begin(a), e = std::end(a);
  for (auto it = std::begin(a); it != e; ++it) {
    if (it != b) os << sep;
    os << *it;
  }
  return os << ends;
}
template <typename T, typename = void>
struct is_iterable : std::false_type {};
template <typename T>
struct is_iterable<T, std::void_t<decltype(std::begin(std::declval<T>())),
                                  decltype(std::end(std::declval<T>()))>>
    : std::true_type {};

template <typename T, typename = std::enable_if_t<
                          is_iterable<T>::value &&
                          !std::is_same<T, std::string_view>::value &&
                          !std::is_same<T, std::string>::value>>
std::ostream &operator<<(std::ostream &os, const T &a) {
  return print_seq(a, ", ", "", (os << "{")) << "}";
}

void print() { std::cout << "\n"; }
template <class T>
void print(const T &x) {
  std::cout << x << "\n";
}
template <typename Head, typename... Tail>
void print(const Head &head, Tail... tail) {
  std::cout << head << " ";
  print(tail...);
}

void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T &value, Ts &...args) {
  std::cin >> value;
  read_from_cin(args...);
}
#define VAR(type, ...) \
  type __VA_ARGS__;    \
  read_from_cin(__VA_ARGS__);

#ifdef ENABLE_DEBUG
#include "debug_dump.hpp"
#else
#define DUMP(...)
#endif

using namespace std;

// Most Significant Set Bit (Highest One Bit) = std::bit_floor(x)
int mssb_pos(unsigned x) {
  static const int CLZ_WIDTH = __builtin_clz(1);
  assert(x != 0);
  return CLZ_WIDTH - __builtin_clz(x);
}
int mssb_pos(u64 x) {
  static const int CLZLL_WIDTH = __builtin_clzll(1LL);
  assert(x != 0);
  return CLZLL_WIDTH - __builtin_clzll(x);
}
template <typename U>
inline U bit_floor(U x) {
  if (x == 0) return 0;
  return U(1) << mssb_pos(x);
}

struct Edge {
  int to;
};

template <typename Task>
class InplaceRerooting {
 public:
  using NV = typename Task::NodeValue;
  using EV = typename Task::EdgeValue;

  Task task;
  int n;                             // number of nodes
  std::vector<std::vector<Edge>> g;  // graph (tree)
  std::vector<NV> sub;               // values for each subtree rooted at i
  std::vector<NV> full;              // values for each entire tree rooted at i
  int base_root;                     // base root node where we start DFS

  explicit InplaceRerooting(Task task, std::vector<std::vector<Edge>> g,
                            int r = 0)
      : task(move(task)),
        n((int)g.size()),
        g(move(g)),
        sub(n),
        full(n),
        base_root(r) {}

  const std::vector<NV> &run() {
    pull_up(base_root, -1);
    NV leaf_val = task.add_node(task.id(), base_root);
    push_down(base_root, -1, leaf_val);
    return full;
  }

 private:
  NV pull_up(int v, int par) {
    EV res = task.id();
    for (auto &e : g[v]) {
      int u = e.to;
      if (u == par) continue;
      auto sub = task.add_edge(pull_up(u, v), e);
      task.merge_inplace(res, std::move(sub));
    }
    return (sub[v] = task.add_node(res, v));
  }

  void push_down(int v, int par, NV upper_sub) {
    int m = g[v].size();
    EV agg = task.id();

    for (int i = 0; i < m; ++i) {
      auto &e = g[v][i];
      int u = e.to;
      if (u == par) {
        task.merge_inplace(agg, task.add_edge(upper_sub, e));
      } else {
        task.merge_inplace(agg, task.add_edge(sub[u], e));
      }
    }
    full[v] = task.add_node(agg, v);

    for (int i = 0; i < m; ++i) {
      auto &e = g[v][i];
      int u = e.to;
      if (u == par) continue;
      EV edge_value = task.add_edge(sub[u], e);
      task.subtract_inplace(agg, edge_value);
      NV next_upper_sub = task.add_node(agg, v);
      push_down(u, v, std::move(next_upper_sub));
      task.merge_inplace(agg, std::move(edge_value));
    }
  }
};

struct InplaceTask {
  struct NodeValue {
    unsigned nimber;
    map<unsigned, set<int>> nexts;
  };
  struct EdgeValue {
    map<unsigned, set<int>> nimbers;
  };

  EdgeValue id() const { return {}; }

  NodeValue add_node(const EdgeValue &e, int node) const {
    NodeValue res;
    unsigned mex = 0;
    bool mex_determined = false;
    for (auto &[g, tos] : e.nimbers) {
      if (tos.empty()) continue;
      if (g == mex) {
        if (not mex_determined) mex++;
      } else if (g > mex) {
        mex_determined = true;
      }
      res.nexts[g].insert(ALL(tos));
    }
    res.nimber = mex;
    return res;
  }

  EdgeValue add_edge(NodeValue val, const Edge &edge) const {
    EdgeValue res;
    res.nimbers[val.nimber].insert(edge.to);
    return res;
  }

  void merge_inplace(EdgeValue &agg, const EdgeValue &x) const {
    for (auto &[g, tos] : x.nimbers) {
      agg.nimbers[g].insert(ALL(tos));
    }
  }

  void subtract_inplace(EdgeValue &agg, const EdgeValue &x) const {
    for (auto &[g, tos] : x.nimbers) {
      for (auto to : tos) {
        agg.nimbers[g].erase(to);
        if (agg.nimbers[g].empty()) {
          agg.nimbers.erase(g);
        }
      }
    }
  }
};

pair<int, int> solve() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  REP(i, m) {
    cin >> a[i];
    --a[i];
  }
  vector<vector<Edge>> g(n);
  REP(i, n - 1) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    g[u].push_back({v});
    g[v].push_back({u});
  }

  InplaceTask task;
  InplaceRerooting<InplaceTask> solver(task, g);
  auto res = solver.run();

  unsigned ag = 0;
  REP(i, m) {
    const auto &node = res[a[i]];
    ag ^= node.nimber;
  }
  if (ag == 0) {
    return {-1, -1};
  }

  unsigned ag_mssb = bit_floor(ag);
  int mi = -1;
  REP(i, m) {
    const auto &node = res[a[i]];
    if (node.nimber & ag_mssb) {
      mi = i;
      break;
    }
  }
  DUMP(mi);
  assert(mi >= 0);
  const auto &nv = res[a.at(mi)];
  unsigned tg = (nv.nimber ^ ag);
  auto it = nv.nexts.find(tg);
  assert(it != nv.nexts.end());
  assert(not it->second.empty());
  int to = *it->second.begin();
  return {mi + 1, to + 1};
}

int main() {
  ios_base::sync_with_stdio(false), cin.tie(nullptr);
  auto [i, v] = solve();
  print(i, v);
}
0