Submission #1055840

# Submission time Handle Problem Language Result Execution time Memory
1055840 2024-08-13T05:58:08 Z 정민찬(#11068) Summer Driving (CCO24_day1problem3) C++17
1 / 25
6000 ms 64976 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;

int N, R, A, B;
vector<int> adj[300010];
int down[300010], par[300010];
int dep[300010];
int in[300010], out[300010];
int pv;

void dfs(int x, int p) {
    down[x] = 0;
    in[x] = ++ pv;
    for (auto &y : adj[x]) {
        if (y == p) continue;
        par[y] = x;
        dep[y] = dep[x] + 1;
        dfs(y, x);
        down[x] = max(down[x], down[y] + 1);
    }
    out[x] = pv;
}

int findmin(int x, int p, int dist) {
    if (dist > B-1) return N + 1;
    int ret = x;
    for (auto &y : adj[x]) {
        if (y == p) continue;
        ret = min(ret, findmin(y, x, dist+1));
    }
    return ret;
}

int dp[300010];
int dp2[300010];

int simulate(int x, int p, int dist) {
    if (dist > B) return N + 1;
    int ret = N + 1;
    for (auto &y : adj[x]) {
        if (y == p || y == par[x]) continue;
        ret = min(ret, dp[y]);
    }
    if (ret == N+1) ret = x;
    for (auto &y : adj[x]) {
        if (y == p) continue;
        ret = min(ret, simulate(y, x, dist + 1));
    }
    return ret;
}
vector<int> vtx[300010];

int main() {
    ios_base :: sync_with_stdio(false); cin.tie(NULL);
    cin >> N >> R >> A >> B;
    if (A <= B) {
        cout << "1\n";
        return 0;
    }
    for (int i=0; i<N-1; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(R, 0);
   
    int mxdep = 0;
    for (int i=1; i<=N; i++) {
        mxdep = max(mxdep, dep[i]);
        vtx[dep[i]].push_back(i);
    }
    for (int i=mxdep; i>=1; i--) {
        if (i+A-1 <= mxdep) {
            for (auto &x : vtx[i+A-1]) {
                dp2[x] = simulate(x, -1, 0);
            }
        }
        for (auto &x : vtx[i]) {
            if (down[x] < A-1) {
                dp[x] = min(par[x], findmin(x, par[x], 0));
            }
            else {
                for (auto &y : vtx[i+A-1]) {
                    if (in[x] <= in[y] && in[y] <= out[x])
                        dp[x] = max(dp[x], dp2[y]);
                }
            }
        }
    }
    int ans = 0;
    for (auto &x : vtx[1]) {
        ans = max(ans, dp[x]);
    }
    cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 14424 KB Output is correct
2 Correct 5 ms 14428 KB Output is correct
3 Correct 5 ms 14428 KB Output is correct
4 Correct 5 ms 14428 KB Output is correct
5 Correct 5 ms 14428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 315 ms 64976 KB Output is correct
2 Correct 313 ms 64848 KB Output is correct
3 Correct 1865 ms 64856 KB Output is correct
4 Execution timed out 6025 ms 62592 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 14428 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 14428 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 735 ms 21108 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 14428 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 14424 KB Output is correct
2 Correct 5 ms 14428 KB Output is correct
3 Correct 5 ms 14428 KB Output is correct
4 Correct 5 ms 14428 KB Output is correct
5 Correct 5 ms 14428 KB Output is correct
6 Correct 315 ms 64976 KB Output is correct
7 Correct 313 ms 64848 KB Output is correct
8 Correct 1865 ms 64856 KB Output is correct
9 Execution timed out 6025 ms 62592 KB Time limit exceeded
10 Halted 0 ms 0 KB -