結果

問題 No.768 Tapris and Noel play the game on Treeone
ユーザー stoqstoq
提出日時 2020-09-27 10:32:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 117 ms / 2,000 ms
コード長 4,804 bytes
コンパイル時間 2,811 ms
コンパイル使用メモリ 223,848 KB
実行使用メモリ 16,868 KB
最終ジャッジ日時 2024-06-30 11:46:10
合計ジャッジ時間 6,801 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,948 KB
testcase_07 AC 57 ms
11,136 KB
testcase_08 AC 27 ms
7,424 KB
testcase_09 AC 33 ms
8,192 KB
testcase_10 AC 23 ms
7,040 KB
testcase_11 AC 111 ms
15,584 KB
testcase_12 AC 111 ms
15,572 KB
testcase_13 AC 113 ms
15,264 KB
testcase_14 AC 103 ms
15,300 KB
testcase_15 AC 117 ms
15,820 KB
testcase_16 AC 114 ms
16,004 KB
testcase_17 AC 112 ms
16,248 KB
testcase_18 AC 95 ms
16,868 KB
testcase_19 AC 98 ms
16,632 KB
testcase_20 AC 97 ms
16,168 KB
testcase_21 AC 89 ms
15,488 KB
20evil_special_uni1.txt AC 107 ms
16,220 KB
20evil_special_uni2.txt AC 100 ms
15,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define MOD_TYPE 1

#pragma region Macros

#include <bits/stdc++.h>
using namespace std;

#if 0
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using Int = boost::multiprecision::cpp_int;
using lld = boost::multiprecision::cpp_dec_float_100;
#endif

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
template <typename Q_type>
using smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;

constexpr ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353);
//constexpr ll MOD = 1;
constexpr int INF = (int)1e9 + 10;
constexpr ll LINF = (ll)4e18;
constexpr double PI = acos(-1.0);
constexpr double EPS = 1e-11;
constexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0};
constexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};

#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define REPI(i, m, n) for (int i = m; i < (int)(n); ++i)
#define repi(i, n) REPI(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << "\n"
#define Yes(n) cout << ((n) ? "Yes" : "No") << "\n"
#define possible(n) cout << ((n) ? "possible" : "impossible") << "\n"
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << "\n"
#define Yay(n) cout << ((n) ? "Yay!" : ":(") << "\n"
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x) cerr << #x << ":" << x << "\n";

struct io_init
{
  io_init()
  {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(30) << setiosflags(ios::fixed);
  };
} io_init;
template <typename T>
inline bool chmin(T &a, T b)
{
  if (a > b)
  {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
inline bool chmax(T &a, T b)
{
  if (a < b)
  {
    a = b;
    return true;
  }
  return false;
}
inline ll CEIL(ll a, ll b)
{
  return (a + b - 1) / b;
}
template <typename A, size_t N, typename T>
inline void Fill(A (&array)[N], const T &val)
{
  fill((T *)array, (T *)(array + N), val);
}
template <typename T, typename U>
constexpr istream &operator>>(istream &is, pair<T, U> &p) noexcept
{
  is >> p.first >> p.second;
  return is;
}
template <typename T, typename U>
constexpr ostream &operator<<(ostream &os, pair<T, U> &p) noexcept
{
  os << p.first << " " << p.second;
  return os;
}
#pragma endregion

template <typename T>
struct ReRooting
{
  using F = function<T(T, int)>;
  using F2 = function<T(T, T)>;
  int V;
  vector<vector<int>> G;
  vector<vector<T>> dp;
  // dp_v = g(merge(f(dp_c1,c1), f(dp_c2,c2), ..., f(dp_ck,ck)), v)
  F f, g;
  F2 merge;
  T mi; // identity of merge

  ReRooting() {}
  ReRooting(int V, F f, F2 merge, T mi, F g)
      : V(V), f(f), merge(merge), mi(mi), g(g)
  {
    G.resize(V);
    dp.resize(V);
  }

  void read_graph(int index = 1)
  {
    int a, b;
    for (int i = 0; i < V - 1; ++i)
    {
      cin >> a >> b;
      a -= index, b -= index;
      G[a].emplace_back(b);
      G[b].emplace_back(a);
    }
  }

  void add_edge(int a, int b)
  {
    G[a].emplace_back(b);
    G[b].emplace_back(a);
  }

  T dfs1(int p, int v)
  {
    T res = mi;
    for (int i = 0; i < G[v].size(); ++i)
    {
      if (G[v][i] == p)
        continue;
      dp[v][i] = dfs1(v, G[v][i]);
      res = merge(res, f(dp[v][i], G[v][i]));
    }
    return g(res, v);
  }

  void dfs2(int p, int v, T from_par)
  {
    for (int i = 0; i < G[v].size(); ++i)
    {
      if (G[v][i] == p)
      {
        dp[v][i] = from_par;
        break;
      }
    }
    vector<T> pR(G[v].size() + 1);
    pR[G[v].size()] = mi;
    for (int i = G[v].size(); i > 0; --i)
      pR[i - 1] = merge(pR[i], f(dp[v][i - 1], G[v][i - 1]));
    T pL = mi;
    for (int i = 0; i < G[v].size(); ++i)
    {
      if (G[v][i] != p)
      {
        T val = merge(pL, pR[i + 1]);
        dfs2(v, G[v][i], g(val, v));
      }
      pL = merge(pL, f(dp[v][i], G[v][i]));
    }
  }

  void calc(int root = 0)
  {
    for (int i = 0; i < V; ++i)
      dp[i].resize(G[i].size());
    dfs1(-1, root);
    dfs2(-1, root, mi);
  }

  T solve(int v)
  {
    T ans = mi;
    for (int i = 0; i < G[v].size(); ++i)
      ans = merge(ans, f(dp[v][i], G[v][i]));
    return g(ans, v);
  }
};

void solve()
{
  int n;
  cin >> n;
  auto f = [](bool a, int v) { return a; };
  auto merge = [](bool a, bool b) { return a or b; };
  auto g = [](bool a, int v) { return !a; };
  ReRooting<bool> re(n, f, merge, false, g);
  re.read_graph();
  re.calc();
  vector<int> ans;
  rep(i, n)
  {
    if (re.solve(i))
      ans.push_back(i + 1);
  }
  cout << ans.size() << "\n";
  for (auto v : ans)
    cout << v << "\n";
}

int main()
{
  solve();
}
0