結果

問題 No.1976 Cut then Connect
ユーザー magstamagsta
提出日時 2022-06-08 19:58:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 174 ms / 2,000 ms
コード長 3,371 bytes
コンパイル時間 8,121 ms
コンパイル使用メモリ 305,432 KB
実行使用メモリ 33,360 KB
最終ジャッジ日時 2024-05-03 12:18:47
合計ジャッジ時間 11,583 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 164 ms
31,412 KB
testcase_03 AC 81 ms
20,728 KB
testcase_04 AC 25 ms
9,188 KB
testcase_05 AC 68 ms
18,560 KB
testcase_06 AC 120 ms
24,452 KB
testcase_07 AC 71 ms
18,384 KB
testcase_08 AC 171 ms
32,312 KB
testcase_09 AC 141 ms
27,612 KB
testcase_10 AC 29 ms
10,376 KB
testcase_11 AC 173 ms
32,312 KB
testcase_12 AC 5 ms
6,944 KB
testcase_13 AC 119 ms
24,620 KB
testcase_14 AC 109 ms
23,112 KB
testcase_15 AC 108 ms
22,448 KB
testcase_16 AC 143 ms
29,140 KB
testcase_17 AC 44 ms
12,948 KB
testcase_18 AC 10 ms
6,940 KB
testcase_19 AC 120 ms
24,560 KB
testcase_20 AC 174 ms
33,360 KB
testcase_21 AC 88 ms
21,056 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,944 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 2 ms
6,944 KB
testcase_30 AC 2 ms
6,944 KB
testcase_31 AC 2 ms
6,944 KB
testcase_32 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include "testlib.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() {
    registerValidation();
    int N = inf.readInt(2, 100000, "N");
    inf.readEoln();

    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++) {
        a[i] = inf.readInt(1, N, "a");
        inf.readSpace();
        b[i] = inf.readInt(1, N, "a");
        inf.readEoln();
        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);
    }
    inf.readEof();

    for (int i = 0; i < N; i++) {
        if (G[i].size() == 0) cout << -1 << endl;
    }

    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_, max(max(dp_[a[i] - 1][c[i]], dp_[b[i] - 1][d[i]]), (dp_[a[i] - 1][c[i]] + 1) / 2 + (dp_[b[i] - 1][d[i]] + 1) / 2 + 1));
    }

    cout << ans_ << endl;
}
0