結果

問題 No.768 Tapris and Noel play the game on Treeone
ユーザー homesentinelhomesentinel
提出日時 2022-07-28 16:41:20
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 235 ms / 2,000 ms
コード長 6,616 bytes
コンパイル時間 1,447 ms
コンパイル使用メモリ 128,504 KB
実行使用メモリ 23,132 KB
最終ジャッジ日時 2023-09-25 09:07:21
合計ジャッジ時間 6,563 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 96 ms
14,276 KB
testcase_08 AC 43 ms
9,012 KB
testcase_09 AC 53 ms
10,108 KB
testcase_10 AC 39 ms
8,440 KB
testcase_11 AC 165 ms
20,244 KB
testcase_12 AC 177 ms
20,384 KB
testcase_13 AC 162 ms
19,948 KB
testcase_14 AC 156 ms
19,912 KB
testcase_15 AC 176 ms
20,876 KB
testcase_16 AC 179 ms
21,012 KB
testcase_17 AC 182 ms
21,436 KB
testcase_18 AC 235 ms
23,132 KB
testcase_19 AC 230 ms
23,032 KB
testcase_20 AC 222 ms
21,816 KB
testcase_21 AC 209 ms
20,948 KB
20evil_special_uni1.txt AC 183 ms
21,440 KB
20evil_special_uni2.txt AC 173 ms
20,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>
#include <cassert>


using namespace std;
//using namespace atcoder;

#define REP(i, n) for (int i=0; i<(n); ++i)
#define RREP(i, n) for (int i=(int)(n)-1; i>=0; --i)
#define FOR(i, a, n) for (int i=(a); i<(n); ++i)
#define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i)

#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()

#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl;

template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    os << "[";
    REP(i, SZ(v)) {
        if (i) os << ", ";
        os << v[i];
    }
    return os << "]";
}

template<class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    return os << "(" << p.first << " " << p.second << ")";
}

template<class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;

const ll MOD = 1e9 + 7;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
const ld eps = 1e-9;


// edit
template<typename E, typename O=E>
struct Rerooting {
    struct Edge {
        int to;
        int rev;
        O w;
    };

    vector<vector<Edge>> graph;
    vector<vector<E>> dp;
    vector<vector<int>> vis;

    const int N;

    Rerooting(int N) : graph(N), dp(N), vis(N), N(N) {
    }

    // 重みとかがない場合でもwに適当な値を入れておくこと
    void add_edge(int u, int v, O w) {
        int su = graph[u].size();
        int sv = graph[v].size();

        graph[u].push_back({v, sv, w});
        graph[v].push_back({u, su, w});
    }

    // 根をrとしたときrのc番目の子の値を求める
    E dfs_sub(int r, int c) {
        if (vis[r][c]) return dp[r][c];
        vis[r][c] = true;

        E &ret = dp[r][c];

        int u = graph[r][c].to;
        int su = graph[u].size();

        // TODO 葉に対する例外処理 
        if (graph[u].size() == 1) {
            ret = true;
            return ret;
        }

        // TODO 探索前の処理
        bool win_exist = false;

        for (int i = 0; i < su; ++i) {
            int v = graph[u][i].to;
            if (v == r) continue;

            E sub_res = dfs_sub(u, i);

            // TODO sub_resに辺(u,v)を適用
            // E applied_res = f1(sub_res, graph[u][i].w)

            // TODO
            // ret = f2(ret, applied_res);
            win_exist = win_exist || sub_res;
        }

        // TODO 探索後の処理
        // 例: TDPC-V

        ret = !win_exist;
        return ret;
    }

    void dfs_all(int u, int p = -1) {
        int su = graph[u].size();

        // TODO 初期値を定めておく
        vector<E> es(su + 2, 0);
        vector<E> lce(su + 2, 0);
        vector<E> rce(su + 2, 0);

        for (int i = 0; i < su; ++i) {
            E sub_res = dfs_sub(u, i);
            // TODO sub_resに辺(u, v)を適用し、es lce rceに代入
            // es[i + 1] = lce[i + 1] = rce[i + 1] = f1(sub_res, graph[u][i].w);

            es[i + 1] = lce[i + 1] = rce[i + 1] = sub_res;
        }

        for (int i = 0; i <= su; ++i) {
//            lce[i + 1] = f2(lce[i], lce[i + 1]);
            lce[i + 1] = lce[i] || lce[i + 1];
        }

        for (int i = su + 1; i > 0; --i) {
//            rce[i - 1] = f2(rce[i], rce[i - 1]);
            rce[i - 1] = rce[i] || rce[i - 1];
        }

        for (int i = 0; i < su; ++i) {
            // vが根のときのuに関する値を求める
            int v = graph[u][i].to;
            int rev = graph[u][i].rev;
            vis[v][rev] = true;

            // TODO uが葉の場合の例外処理
            if (graph[u].size() == 1) {
                dp[v][rev] = 1;
                continue;
            }

            // TODO 累積和を使って値を求める
            // dp[v][rev] = f2(lce[i], rce[i + 2]);
            dp[v][rev] = !(lce[i] || rce[i + 2]);
        }

        for (int i = 0; i < su; ++i) {
            int v = graph[u][i].to;
            if (v == p) continue;
            dfs_all(v, u);
        }
    }

    vector<E> build(int root = 0) {
        // dp, visの初期化
        for (int i = 0; i < N; ++i) {
            dp[i].resize(graph[i].size());
            vis[i].resize(graph[i].size(), false);
        }

        dfs_all(root, -1);

        vector<E> ret(N);

        // uの子の結果を利用してuの値を求める
        for (int u = 0; u < N; ++u) {
            int su = graph[u].size();

            // TODO 探索前の処理
            bool win_exist = false;


            for (int i = 0; i < su; ++i) {
                // 部分問題を求める
                E sub_res = dfs_sub(u, i);

                // TODO sub_resに辺(u,v)を適用
                // E applied_res = f1(sub_res, graph[u][i].w);

                // TODO
                // ret[u] = f2(ret[u], applied_res);
                win_exist = win_exist || sub_res;
            }

            // TODO 探索後の処理
            ret[u] = !win_exist;
        }

        return ret;
    }
};


void solve() {
    int N;
    cin >> N;

    Rerooting<int> rr(N+1);

    REP(i, N - 1) {
        int a, b;
        cin >> a >> b;
        rr.add_edge(a, b, 0);
    }

    auto res = rr.build(1);
    vector<int> anss;

    FOR(i, 1, N + 1) {
        if (res[i]) anss.push_back(i);
    }

    cout << anss.size() << endl;
    for (auto ans: anss) {
        cout << ans << endl;
    }

}


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
//    std::ifstream in("input.txt");
//    std::cin.rdbuf(in.rdbuf());

    solve();


    return 0;
}
0