답안 #944085

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
944085 2024-03-12T08:14:47 Z wii 꿈 (IOI13_dreaming) C++17
0 / 100
1000 ms 25428 KB
#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;

#define sz(x) (int)(x).size()
const int MaxN = 1e5 + 5;

template<typename T> bool maximize(T &res, const T &val) { if (res < val) { res = val; return true; } return false; }
template<typename T> bool minimize(T &res, const T &val) { if (val < res) { res = val; return true; } return false; }

const int Inf = 0x3f3f3f3f;

int n, m, l;
vector<int> pref[MaxN], suff[MaxN];
vector<pair<int, int>> adj[MaxN];

int vis[MaxN];
int dep[MaxN], dp[MaxN];
void dfs(int u, int p) {
    vis[u] = true;

    for (auto [v, w]: adj[u]) if (v != p) {
        dep[v] = dep[u] + w;
        dfs(v, u);

        maximize(dp[u], dp[v] + w);
    }

    //cout << "? " << u << " " << dep[u] << endl;
}

int ans;
void reroot(int u, int p) {
   // cout << u << " " << dp[u] << endl;

    minimize(ans, dp[u]);

    for (int i = 0; i < sz(adj[u]); ++i) {
        auto [v, w] = adj[u][i];
        pref[u].emplace_back(dp[v] + w);
        suff[u].emplace_back(dp[v] + w);
    }

    for (int i = 0; i < sz(adj[u]); ++i) {
        auto [v, w] = adj[u][i];
        if (i) maximize(pref[u][i], pref[u][i - 1]);
    }

    for (int i = sz(adj[u]) - 1; i >= 0; --i) {
        auto [v, w] = adj[u][i];
        if (i + 1 < sz(adj[u])) maximize(suff[u][i], suff[u][i + 1]);
    }

    for (int i = 0; i < sz(adj[u]); ++i) {
        auto [v, w] = adj[u][i];
        if (v == p) continue;

        int old = dp[v];
        int nw = max((i ? pref[u][i - 1] : 0), (i + 1 < sz(adj[u]) ? suff[u][i + 1] : 0));

        dp[u] = nw;
        dp[v] = max(dp[u] + w, old);

        reroot(v, u);

        dp[u] = pref[u].back();
        dp[v] = old;
    }
}

int used[MaxN];
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
    n = N;
    m = M;
    l = L;

    for (int i = 0; i < m; ++i) {
        int u = A[i];
        int v = B[i];
        int w = T[i];

        adj[u].emplace_back(v, w);
        adj[v].emplace_back(u, w);
    }

    vector<int> best;

    if (1)
    for (int i = 0; i < n; ++i) if (!vis[i]) {
        dfs(i, i);

        ans = Inf;
        reroot(i, i);

        best.push_back(ans);

        for (int i = 0; i < n; ++i) if (vis[i] && !used[i]) {
            //for (auto [v, w]: adj[i]) if (vis[v] && !used[v])
            //    cout << i << " " << v << " " << w << endl;
        } //cout << endl;

        for (int i = 0; i < n; ++i) if (vis[i]) used[i] = true;
    }


    sort(best.begin(), best.end(), greater<int>());
    int maxD = 0;

    maximize(maxD, best[0]);
    if (best.size() >= 2) maximize(maxD, best[0] + best[1] + l);
    if (best.size() >= 3) maximize(maxD, best[1] + best[2] + 2*l);

    return maxD;
}

Compilation message

dreaming.cpp: In function 'void reroot(int, int)':
dreaming.cpp:46:14: warning: structured binding declaration set but not used [-Wunused-but-set-variable]
   46 |         auto [v, w] = adj[u][i];
      |              ^~~~~~
dreaming.cpp:51:14: warning: structured binding declaration set but not used [-Wunused-but-set-variable]
   51 |         auto [v, w] = adj[u][i];
      |              ^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 54 ms 25428 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 9816 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 54 ms 25428 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1054 ms 13312 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 9816 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 54 ms 25428 KB Output isn't correct
2 Halted 0 ms 0 KB -