Submission #198090

# Submission time Handle Problem Language Result Execution time Memory
198090 2020-01-24T16:27:37 Z johutha Dreaming (IOI13_dreaming) C++14
18 / 100
90 ms 14584 KB
#include "dreaming.h"
#include <vector>
#include <iostream>
#include <algorithm>
#include "assert.h"

#define int int64_t

using namespace std;

struct graph
{
    int n;
    vector<vector<pair<int,int>>> adjlist;
    vector<int> comp;
    vector<int> dist;
    int nrcomp = 0;

    void compdfs(int curr, int par, int c)
    {
        comp[curr] = c;

        for (auto p : adjlist[curr])
        {
            int next = p.first;
            if (next == par) continue;
            compdfs(next, curr, c);
        }
    }

    void distdfs(int curr, int par, int d)
    {
        dist[curr] = max(dist[curr], d);

        for (auto p : adjlist[curr])
        {
            int next = p.first;
            int w = p.second;
            if (next == par) continue;
            assert(next != -1);
            distdfs(next, curr, d + w);
        }
    }

    void print()
    {
        for (int i = 0; i < n; i++)
        {
            if (i < 10) cout << " ";
            cout << i << " ";
        }
        cout << "\n";
        for (auto i : dist)
        {
            if (i < 10) cout << " ";
            cout << i << " ";
        }
        cout << "\n";
    }

    int calc(int l)
    {
        dist.resize(n, -1);
        comp.resize(n, -1);

        for (int i = 0; i < n; i++)
        {
            if (comp[i] == -1)
            {
                compdfs(i, -1, nrcomp);
                nrcomp++;
            }
        }

        if (nrcomp == 1)
        {
            distdfs(0, -1, 0);
            int mmax = -1;
            int mver = -1;

            for (int i = 0; i < n; i++)
            {
                if (mmax < dist[i])
                {
                    mmax = dist[i];
                    mver = i;
                }
            }
            dist.assign(n, -1);

            distdfs(mver, -1, 0);
            return *max_element(dist.begin(), dist.end());
        }

        for (int i = 0; i < n; i++)
        {
            if (dist[i] < 0)
            {
                distdfs(i, -1, 0);
            }
        }

        vector<int> md(nrcomp, -1);
        vector<int> mv(nrcomp, -1);

        for (int i = 0; i < n; i++)
        {
            if (dist[i] > md[comp[i]])
            {
                md[comp[i]] = dist[i];
                mv[comp[i]] = i;
            }
        }
        
        for (auto i : mv) distdfs(i, -1, 0);

        md.assign(nrcomp, -1);
        mv.assign(nrcomp, -1);

        for (int i = 0; i < n; i++)
        {
            if (dist[i] > md[comp[i]])
            {
                md[comp[i]] = dist[i];
                mv[comp[i]] = i;
            }
        }

        for (auto i : mv) distdfs(i, -1, 0);

        md.assign(nrcomp, 1e12);

        for (int i = 0; i < n; i++)
        {
            if (dist[i] < md[comp[i]])
            {
                md[comp[i]] = dist[i];
            }
        }

        sort(md.begin(), md.end());

        int mmax = md[nrcomp - 1] + md[nrcomp - 2] + l;
        if (nrcomp != 2) mmax = max(md[nrcomp - 2] + md[nrcomp - 3] + 2*l, mmax);

        return mmax;
    }
};

signed travelTime(signed N, signed M, signed L, signed A[], signed B[], signed T[])
{
    graph g;
    g.n = N;

    g.adjlist.resize(N);

    for (int i = 0; i < M; i++)
    {
        g.adjlist[A[i]].emplace_back(B[i], T[i]);
        g.adjlist[B[i]].emplace_back(A[i], T[i]);
    }

    return g.calc(L);
}
# Verdict Execution time Memory Grader output
1 Incorrect 90 ms 14584 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 90 ms 14584 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 90 ms 14584 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 46 ms 7800 KB Output is correct
2 Correct 51 ms 7928 KB Output is correct
3 Correct 44 ms 7672 KB Output is correct
4 Correct 47 ms 7800 KB Output is correct
5 Correct 53 ms 7756 KB Output is correct
6 Correct 56 ms 8312 KB Output is correct
7 Correct 50 ms 8028 KB Output is correct
8 Correct 51 ms 7672 KB Output is correct
9 Correct 50 ms 7544 KB Output is correct
10 Correct 53 ms 8056 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 13 ms 5824 KB Output is correct
13 Correct 13 ms 5884 KB Output is correct
14 Correct 12 ms 5880 KB Output is correct
15 Correct 13 ms 5880 KB Output is correct
16 Correct 12 ms 5880 KB Output is correct
17 Correct 13 ms 5880 KB Output is correct
18 Correct 13 ms 5880 KB Output is correct
19 Correct 12 ms 5884 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 256 KB Output is correct
22 Correct 2 ms 504 KB Output is correct
23 Correct 13 ms 5880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 90 ms 14584 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 90 ms 14584 KB Output isn't correct
2 Halted 0 ms 0 KB -