Submission #768457

# Submission time Handle Problem Language Result Execution time Memory
768457 2023-06-28T07:40:12 Z boris_mihov Dreaming (IOI13_dreaming) C++17
0 / 100
33 ms 12140 KB
#include "dreaming.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>

typedef long long llong;
const int MAXN = 100000 + 10;
const int INF  = 1e9;

int n, m;
int dp[MAXN];
int dp2[MAXN];
int next[MAXN];
bool vis[MAXN];
std::vector <std::pair <int,int>> g[MAXN];
std::vector <int> dists;

void dfsDown(int node)
{
    vis[node] = true;
    for (const auto &[u, d] : g[node])
    {
        if (vis[u])
        {
            continue;
        }

        dfsDown(u);
        if (dp[u] + d > dp[node])
        {
            dp2[node] = dp[node];
            dp[node] = dp[u] + d;
            next[node] = u; 
        } else if (dp[u] + d > dp2[node])
        {
            dp2[node] = dp[u] + d; 
        }
    }
}

int dfsUp(int node, int p, int edge)
{
    if (p != 0)
    {
        if (next[p] != node)
        {
            if (dp[p] + edge > dp[node])
            {
                dp[node] = dp[p] + edge;
                next[node] = p;
            } else if (dp[p] + edge > dp2[node])
            {
                dp2[node] = dp[p] + edge;
            }
        } else
        { 
            if (dp2[p] + edge > dp[node])
            {
                dp[node] = dp2[p] + edge;
                next[node] = p;
            } else if (dp2[p] + edge > dp2[node])
            {
                dp2[node] = dp2[p] + edge;
            }
        }
    }

    int ans = dp[node];
    for (const auto &[u, d] : g[node])
    {
        if (u != p)
        {
            ans = std::min(ans, dfsUp(u, node, d));
        }
    }

    return ans;
}

int travelTime(int N, int M, int L, int A[], int B[], int T[]) 
{
    n = N;
    m = M;
    for (int i = 0 ; i < m ; ++i)
    {
        g[A[i] + 1].push_back({B[i] + 1, T[i]});
        g[B[i] + 1].push_back({A[i] + 1, T[i]});
    }

    for (int i = 1 ; i <= n ; ++i)
    {
        if (vis[i])
        {
            continue;
        }

        dfsDown(i);
        dists.push_back(dfsUp(i, 0, 0));
    }

    std::sort(dists.begin(), dists.end());
    if (dists.size() == 1) return dists[0];
    return dists[dists.size() - 2] + dists[dists.size() - 1] + L;
}
# Verdict Execution time Memory Grader output
1 Incorrect 33 ms 12140 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 33 ms 12140 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 6896 KB Output is correct
2 Incorrect 15 ms 7020 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 33 ms 12140 KB Output isn't correct
2 Halted 0 ms 0 KB -