結果

問題 No.1153 ねこちゃんゲーム
ユーザー MisterMister
提出日時 2020-08-07 22:32:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,912 bytes
コンパイル時間 1,381 ms
コンパイル使用メモリ 85,768 KB
実行使用メモリ 23,640 KB
最終ジャッジ日時 2023-10-25 02:19:07
合計ジャッジ時間 12,201 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 WA -
testcase_03 AC 2 ms
4,348 KB
testcase_04 WA -
testcase_05 AC 2 ms
4,348 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 85 ms
19,640 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 97 ms
19,640 KB
testcase_14 WA -
testcase_15 AC 88 ms
19,640 KB
testcase_16 AC 89 ms
19,640 KB
testcase_17 WA -
testcase_18 AC 89 ms
19,640 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 103 ms
19,640 KB
testcase_29 WA -
testcase_30 AC 88 ms
19,640 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 83 ms
19,416 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 76 ms
23,640 KB
testcase_39 AC 80 ms
23,640 KB
testcase_40 WA -
testcase_41 AC 72 ms
23,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

template <class Cost = int>
struct Edge {
    int src, dst;
    Cost cost;
    Edge(int src = -1, int dst = -1, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }
};

template <class Cost = int>
struct Graph {
    std::vector<std::vector<Edge<Cost>>> graph;

    Graph(int n = 0) : graph(n) {}

    void span(bool direct, int src, int dst, Cost cost = 1) {
        graph[src].emplace_back(src, dst, cost);
        if (!direct) graph[dst].emplace_back(dst, src, cost);
    }

    int size() const { return graph.size(); }
    void clear() { graph.clear(); }
    void resize(int n) { graph.resize(n); }

    std::vector<Edge<Cost>>& operator[](int v) { return graph[v]; }
    std::vector<Edge<Cost>> operator[](int v) const { return graph[v]; }
};

Graph<> graph;

std::vector<std::vector<int>> chmexs;
std::vector<int> mexs, rmexs;

int dfs(int v, int p) {
    auto& chmex = chmexs[v];
    chmex.clear();
    chmex.shrink_to_fit();

    for (auto e : graph[v]) {
        int u = e.dst;
        if (u == p) continue;

        int m = dfs(u, v);
        while ((int)chmex.size() <= m) chmex.push_back(0);
        ++chmex[m];
    }

    auto& mex = mexs[v];
    mex = 0;
    while (mex < (int)chmex.size() && chmex[mex] > 0) ++mex;
    return mex;
}

void edfs(int v, int p, int pmex) {
    auto& chmex = chmexs[v];
    if (pmex != -1) {
        while ((int)chmex.size() <= pmex) chmex.push_back(0);
        ++chmex[pmex];
    }

    auto& mex = rmexs[v];
    mex = 0;
    while (mex < (int)chmex.size() && chmex[mex] > 0) ++mex;

    for (auto e : graph[v]) {
        int u = e.dst;
        if (u == p) continue;
        int m = mexs[u];

        int nmex = mex;
        if (m < nmex && chmex[m] == 1) nmex = m;
        edfs(u, v, nmex);
    }
}

void solve() {
    int n, m;
    std::cin >> n >> m;

    std::vector<int> xs(m);
    for (auto& x : xs) {
        std::cin >> x;
        --x;
    }

    graph.resize(n);
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        std::cin >> u >> v;
        graph.span(true, --u, --v);
    }

    chmexs.resize(n);
    mexs.resize(n);
    rmexs.resize(n);

    dfs(0, -1);
    edfs(0, -1, -1);

    int grundy = 0;
    for (auto x : xs) grundy ^= rmexs[x];

    if (grundy == 0) {
        std::cout << "-1 -1\n";
        return;
    }

    int ri = 0;
    while ((rmexs[xs[ri]] ^ grundy) >= rmexs[xs[ri]]) ++ri;

    int r = xs[ri];
    dfs(r, -1);

    for (auto e : graph[r]) {
        int u = e.dst;
        if (mexs[u] == (rmexs[xs[ri]] ^ grundy)) {
            std::cout << ri + 1 << " " << u + 1 << "\n";
            return;
        }
    }
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0