提出 #54180865


ソースコード 拡げる

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define mp make_pair
#define all(x) x.begin(), x.end()
#define x first
#define y second

vector<int> adj[202020];
int dp[4][202020];
// portal 사용 / 미사용
// 왕복   0   1
// 일방   2   3 

int ans;
void dfs(int v, int pv = -1) {

    for (int i = 0; i < 4; i++) dp[i][v] = 0;
    int mn = 0;
    int mn2 = 0;

    for (auto f : adj[v]) if (f != pv) {
        dfs(f, v);
        dp[1][v] += dp[1][f] + 2;
        dp[3][v] += dp[1][f] + 2;
        mn = min(mn, -dp[1][f] - 1 + dp[3][f]);

        int val = min(dp[3][f] + 1, dp[0][f] + 2);
        dp[0][v] += val;
        dp[2][v] += val;
        mn2 = min(mn2, -val + dp[2][f] + 1);
    }

    dp[3][v] += mn;
    dp[2][v] += mn2;

    /*cout << "R1: " << v << " | ";
    for (int i = 0; i < 4; i++) cout << dp[i][v] << ' ';
    cout << '\n';*/
}

void dfs2(int v, int pv = -1, vector<int> p = { 0, 0, 0, 0 }) {

    vector<pair<int, int>> mn, mn2;
    
    mn.push_back(mp(0, 0));
    mn2.push_back(mp(0, 0));
    vector<int> ndp(4);
    for (auto f : adj[v]) if (f != pv) {
        ndp[1] += dp[1][f] + 2;
        ndp[3] += dp[1][f] + 2;
        mn.push_back(mp(-dp[1][f] - 1 + dp[3][f], f));
        int val = min(dp[3][f] + 1, dp[0][f] + 2);
        ndp[0] += val;
        ndp[2] += val;
        mn2.push_back(mp(-val + dp[2][f] + 1, f));
    }

    if (pv != -1) {
        ndp[1] += p[1] + 2;
        ndp[3] += p[1] + 2;
        mn.push_back(mp(-p[1] - 1 + p[3], pv));
        int val = min(p[3] + 1, p[0] + 2);
        ndp[0] += val;
        ndp[2] += val;
        mn2.push_back(mp(-val + p[2] + 1, pv));
    }
    sort(all(mn));
    sort(all(mn2));

   // cout << "R2: " << v << " | ";
    for (int i = 0; i < 4; i++) {
        if (i == 3) {
            ans = min(ans, ndp[i] + mn[0].x);
            //cout << ndp[i] + mn[0].x << ' ';
        }
        else if (i == 2) {
            ans = min(ans, ndp[i] + mn2[0].x);
          //  cout << ndp[i] + mn2[0].x << ' ';
        }
        else {
            ans = min(ans, ndp[i]);
            //cout << ndp[i] << ' ';
        }
    }
   /* cout << '\n';
    cout << "UPP: ";
    for (auto f : p) cout << f << ' ';
    cout << '\n';*/

    for (auto f : adj[v]) if (f != pv) {

        vector<int> np(4);

        np[1] = ndp[1] - (dp[1][f] + 2);
        np[3] = ndp[3] - (dp[1][f] + 2);
        for (int i = 0; i < 2; i++) {
            if (mn[i].y == f) continue;
            np[3] += mn[i].x;
            break;
        }

        int val = min(dp[3][f] + 1, dp[0][f] + 2);
        np[0] = ndp[0] - val;
        np[2] = ndp[2] - val;
        for (int i = 0; i < 2; i++) {
            if (mn2[i].y == f) continue;
            np[2] += mn2[i].x;
            break;
        }
        dfs2(f, v, np);
    }

}

void solve() {
    int n;
    cin >> n;
    for (int i = 0; i < n - 1; i++) {
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    ans = n * 10;

    dfs(1);
    dfs2(1);
    cout << ans;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int t = 1;
    // cin >> t;
    while (t--) solve();
}

提出情報

提出日時
問題 D - Portable Gate
ユーザ JYJin
言語 C++ 20 (gcc 12.2)
得点 700
コード長 3304 Byte
結果 AC
実行時間 185 ms
メモリ 92388 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 700 / 700
結果
AC × 2
AC × 67
セット名 テストケース
Sample 00-sample-001.txt, 00-sample-002.txt
All 00-sample-001.txt, 00-sample-002.txt, 01-handmade-001.txt, 01-handmade-002.txt, 01-handmade-003.txt, 01-handmade-004.txt, 01-handmade-005.txt, 01-handmade-006.txt, 01-handmade-007.txt, 01-handmade-008.txt, 01-handmade-009.txt, 01-handmade-010.txt, 01-handmade-011.txt, 01-handmade-012.txt, 01-handmade-013.txt, 01-handmade-014.txt, 01-handmade-015.txt, 01-handmade-016.txt, 01-handmade-017.txt, 01-handmade-018.txt, 01-handmade-019.txt, 01-handmade-020.txt, 01-handmade-021.txt, 01-handmade-022.txt, 01-handmade-023.txt, 01-handmade-024.txt, 01-handmade-025.txt, 01-handmade-026.txt, 01-handmade-027.txt, 01-handmade-028.txt, 01-handmade-029.txt, 01-handmade-030.txt, 01-handmade-031.txt, 01-handmade-032.txt, 01-handmade-033.txt, 01-handmade-034.txt, 01-handmade-035.txt, 02-random-001.txt, 02-random-002.txt, 02-random-003.txt, 02-random-004.txt, 02-random-005.txt, 02-random-006.txt, 02-random-007.txt, 02-random-008.txt, 02-random-009.txt, 02-random-010.txt, 02-random-011.txt, 02-random-012.txt, 02-random-013.txt, 02-random-014.txt, 02-random-015.txt, 02-random-016.txt, 02-random-017.txt, 02-random-018.txt, 02-random-019.txt, 02-random-020.txt, 02-random-021.txt, 02-random-022.txt, 02-random-023.txt, 02-random-024.txt, 02-random-025.txt, 02-random-026.txt, 02-random-027.txt, 02-random-028.txt, 02-random-029.txt, 02-random-030.txt
ケース名 結果 実行時間 メモリ
00-sample-001.txt AC 2 ms 3476 KiB
00-sample-002.txt AC 2 ms 3528 KiB
01-handmade-001.txt AC 2 ms 3540 KiB
01-handmade-002.txt AC 2 ms 3460 KiB
01-handmade-003.txt AC 185 ms 92388 KiB
01-handmade-004.txt AC 121 ms 17584 KiB
01-handmade-005.txt AC 99 ms 17516 KiB
01-handmade-006.txt AC 106 ms 18360 KiB
01-handmade-007.txt AC 98 ms 18792 KiB
01-handmade-008.txt AC 109 ms 18688 KiB
01-handmade-009.txt AC 117 ms 21324 KiB
01-handmade-010.txt AC 115 ms 18148 KiB
01-handmade-011.txt AC 137 ms 43068 KiB
01-handmade-012.txt AC 134 ms 44604 KiB
01-handmade-013.txt AC 105 ms 17620 KiB
01-handmade-014.txt AC 108 ms 17740 KiB
01-handmade-015.txt AC 139 ms 46580 KiB
01-handmade-016.txt AC 109 ms 17848 KiB
01-handmade-017.txt AC 160 ms 65072 KiB
01-handmade-018.txt AC 113 ms 17648 KiB
01-handmade-019.txt AC 116 ms 19876 KiB
01-handmade-020.txt AC 151 ms 54076 KiB
01-handmade-021.txt AC 104 ms 17768 KiB
01-handmade-022.txt AC 115 ms 19812 KiB
01-handmade-023.txt AC 133 ms 36744 KiB
01-handmade-024.txt AC 141 ms 49916 KiB
01-handmade-025.txt AC 113 ms 17620 KiB
01-handmade-026.txt AC 104 ms 17568 KiB
01-handmade-027.txt AC 153 ms 67416 KiB
01-handmade-028.txt AC 115 ms 19808 KiB
01-handmade-029.txt AC 148 ms 44348 KiB
01-handmade-030.txt AC 123 ms 18096 KiB
01-handmade-031.txt AC 113 ms 20168 KiB
01-handmade-032.txt AC 167 ms 72784 KiB
01-handmade-033.txt AC 126 ms 30024 KiB
01-handmade-034.txt AC 134 ms 39036 KiB
01-handmade-035.txt AC 135 ms 41936 KiB
02-random-001.txt AC 2 ms 3444 KiB
02-random-002.txt AC 2 ms 3616 KiB
02-random-003.txt AC 2 ms 3616 KiB
02-random-004.txt AC 2 ms 3492 KiB
02-random-005.txt AC 2 ms 3492 KiB
02-random-006.txt AC 2 ms 3556 KiB
02-random-007.txt AC 2 ms 3440 KiB
02-random-008.txt AC 2 ms 3476 KiB
02-random-009.txt AC 2 ms 3488 KiB
02-random-010.txt AC 2 ms 3500 KiB
02-random-011.txt AC 4 ms 4088 KiB
02-random-012.txt AC 3 ms 3820 KiB
02-random-013.txt AC 4 ms 4100 KiB
02-random-014.txt AC 3 ms 3756 KiB
02-random-015.txt AC 2 ms 3580 KiB
02-random-016.txt AC 70 ms 12808 KiB
02-random-017.txt AC 17 ms 6240 KiB
02-random-018.txt AC 39 ms 9264 KiB
02-random-019.txt AC 63 ms 12196 KiB
02-random-020.txt AC 106 ms 16040 KiB
02-random-021.txt AC 19 ms 6584 KiB
02-random-022.txt AC 74 ms 13868 KiB
02-random-023.txt AC 52 ms 11300 KiB
02-random-024.txt AC 65 ms 12644 KiB
02-random-025.txt AC 98 ms 16904 KiB
02-random-026.txt AC 18 ms 6244 KiB
02-random-027.txt AC 93 ms 15832 KiB
02-random-028.txt AC 10 ms 5160 KiB
02-random-029.txt AC 47 ms 10772 KiB
02-random-030.txt AC 23 ms 6984 KiB