結果

問題 No.1976 Cut then Connect
ユーザー magstamagsta
提出日時 2022-05-27 23:12:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,977 bytes
コンパイル時間 2,161 ms
コンパイル使用メモリ 180,460 KB
実行使用メモリ 27,376 KB
最終ジャッジ日時 2023-10-20 23:22:30
合計ジャッジ時間 5,079 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 24 ms
8,348 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 133 ms
23,660 KB
testcase_10 AC 30 ms
9,140 KB
testcase_11 AC 152 ms
26,828 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 104 ms
19,964 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 41 ms
11,516 KB
testcase_18 WA -
testcase_19 AC 110 ms
21,020 KB
testcase_20 AC 156 ms
27,092 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 2 ms
4,348 KB
testcase_24 WA -
testcase_25 AC 2 ms
4,348 KB
testcase_26 WA -
testcase_27 AC 2 ms
4,348 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct Edge {
    int u;
    int v;
    Edge(int a, int b) : u(a), v(b) {

    }
};
using Graph = vector<vector<Edge>>;

vector<vector<int>> dp, dp2, dp_;

void dfs(const Graph& G, int v, int p) {
    for (auto nv : G[v]) {
        if (nv.v == p) continue;
        dfs(G, nv.v, v);
    }
    int count_ = -1;
    for (auto c : G[v]) {
        count_++;
        if (c.v == p) continue;
        int count = -1;
        for (auto d : G[c.v]) {
            count++;
            if (d.v == v) continue;
          	//cout << v << " " << c.v << " " << d.v << " " << count << " " << count_ << endl;
            if (dp[v][count_] < dp[c.v][count] + 1) {
                dp2[v][count_] = dp[v][count_];
                dp[v][count_] = dp[c.v][count] + 1;
            }
            else if (dp2[v][count_] < dp[c.v][count] + 1) {
                dp2[v][count_] = dp[c.v][count] + 1;
            }
        }
        count = -1;
        dp_[v][count_] = dp[v][count_] + dp2[v][count_];
        for (auto d : G[c.v]) {
            count++;
            if (d.v == v) continue;
            dp_[v][count_] = max(dp_[v][count_], dp_[c.v][count]);
        }
    }
}

void dfs2(const Graph& G, int v, int p) {
    int count_ = -1;
    for (auto c : G[v]) {
        count_++;
        if (c.v == p) {
            int count = -1;
            for (auto d : G[c.v]) {
                count++;
                if (d.v == v) continue;
                if (dp[v][count_] < dp[c.v][count] + 1) {
                    dp2[v][count_] = dp[v][count_];
                    dp[v][count_] = dp[c.v][count] + 1;
                }
                else if (dp2[v][count_] < dp[c.v][count] + 1) {
                    dp2[v][count_] = dp[c.v][count] + 1;
                }
            }
            count = -1;
            dp_[v][count_] = dp[v][count_] + dp2[v][count_];
            for (auto d : G[c.v]) {
                count++;
                if (d.v == v) continue;
                dp_[v][count_] = max(dp_[v][count_], dp_[c.v][count]);
            }
        }
    }
    for (auto nv : G[v]) {
        if (nv.v == p) continue;
        dfs2(G, nv.v, v);
    }
}

int main() {
    int N; cin >> N;

    Graph G(N);
    vector<int> a(N - 1), b(N - 1), c(N - 1), d(N - 1);
    for (int i = 0; i < N - 1; i++) {
        cin >> a[i] >> b[i];
        c[i] = G[a[i] - 1].size();
        d[i] = G[b[i] - 1].size();
        G[a[i] - 1].emplace_back(a[i] - 1, b[i] - 1);
        G[b[i] - 1].emplace_back(b[i] - 1, a[i] - 1);
    }

    dp.assign(N, vector<int>(0));
    dp2.assign(N, vector<int>(0));
    dp_.assign(N, vector<int>(0));
    for (int i = 0; i < N; i++) {
        dp[i].assign(G[i].size(), 0);
        dp2[i].assign(G[i].size(), 0);
        dp_[i].assign(G[i].size(), 0);
    }

    dfs(G, 0, -1);
    dfs2(G, 0, -1);

    int ans_ = 1000000;
    for (int i = 0; i < N - 1; i++) {
        ans_ = min(ans_, dp_[b[i] - 1][d[i]]);
    }

    cout << ans_ << endl;
}
0