답안 #67518

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
67518 2018-08-14T15:08:00 Z Kubalionzzale 꿈 (IOI13_dreaming) C++14
18 / 100
1000 ms 13560 KB
#include "dreaming.h"

#include <iostream>
#include <vector>
#include <assert.h>

int component[100010] = { 0 };
int best[100010] = { 0 };
int componentmin[100010] = { 0 };
int curmax[100010] = { 0 }, curmax2[100010] = { 0 };
int n, m, l;
std::vector< std::vector< std::pair<int, int> > > g(100010);

void dfs(int index, int parent, int mark)
{
    for (int i = 0; i < g[index].size(); ++i)
    {
        int next = g[index][i].second;
        int nextVal = g[index][i].first;

        if (next != parent)
        {
            dfs(next, index, mark);
            curmax[index] = std::max(curmax[index], curmax[next] + nextVal);
        }
    }

    component[index] = mark;
}

void dfsout(int index, int parent, int pathSum)
{
    int max1 = best[index], max2 = -1;
    for (int i = 0; i < g[index].size(); ++i)
    {
        int next = g[index][i].second;
        int nextVal = g[index][i].first;

        if (next != parent)
        {
            if (nextVal + curmax[next] > max1)
            {
                max2 = max1;
                max1 = nextVal + curmax[next];
            }
            else if (nextVal + curmax[next] > max2)
            {
                max2 = nextVal + curmax[next];
            }
        }
    }

    for (int i = 0; i < g[index].size(); ++i)
    {
        int next = g[index][i].second;
        int nextVal = g[index][i].first;

        if (next != parent)
        {
            if (nextVal + curmax[next] == max1)
            {
                curmax[next] = std::max(curmax[next], nextVal + max2);
                best[next] = max2 + nextVal;
            }
            else
            {
                curmax[next] = std::max(curmax[next], nextVal + max1);
                best[next] = max1 + nextVal;
            }

            dfsout(next, index, 0);
        }
    }
}

void dfs2(int index, int parent, int sum, int start)
{
    curmax2[start] = std::max(curmax2[start], sum);
    for (int i = 0; i < g[index].size(); ++i)
    {
        int next = g[index][i].second;
        int nextVal = g[index][i].first;

        if (next != parent)
        {
            dfs2(next, index, sum + nextVal, start);
        }
    }
}

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)
    {
        g[A[i]].push_back(std::make_pair(T[i], B[i]));
        g[B[i]].push_back(std::make_pair(T[i], A[i]));
    }

    int cnt = 1;
    for (int i = 0; i < n; ++i)
    {
        if (!component[i])
        {
            dfs(i, -1, cnt);
            dfsout(i, -1, 0);

            ++cnt;
        }
    }

    for (int i = 0; i < n; ++i)
    {
        dfs2(i, -1, 0, i);
    }

    for (int i = 1; i < cnt; ++i)
    {
        componentmin[i] = 1e9 + 5;
    }

    for (int i = 0; i < n; ++i)
    {
        componentmin[component[i]] = std::min(componentmin[component[i]], curmax2[i]);
  //      std::cout << i << " " << curmax[i] << " " << curmax2[i] << "\n";
    }

    int ans = 0;
    int max = -1;
    int maxindex;

    for (int i = 1; i < cnt; ++i)
    {
        if (componentmin[i] > max)
        {
            max = componentmin[i];
            maxindex = i;
        }
    }

    ans = max;

    if (m == n - 1)
        return componentmin[1];

    for (int i = 1; i < cnt; ++i)
    {
        if (i != maxindex)
        {
            ans = std::max(ans, l + max + componentmin[i]);
        }
    }

    int max1 = -1, max1index;
    for (int i = 1; i < cnt; ++i)
    {
        if (i != maxindex)
        {
            if (componentmin[i] > max1)
            {
                max1 = componentmin[i];
                max1index = i;
            }
        }
    }

    for (int i = 1; i < cnt; ++i)
    {
        if (i != maxindex && i != max1index)
        {
            ans = std::max(ans, max1 + 2 * l + componentmin[i]);
        }
    }

    return ans;
}

Compilation message

dreaming.cpp: In function 'void dfs(int, int, int)':
dreaming.cpp:16:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[index].size(); ++i)
                     ~~^~~~~~~~~~~~~~~~~
dreaming.cpp: In function 'void dfsout(int, int, int)':
dreaming.cpp:34:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[index].size(); ++i)
                     ~~^~~~~~~~~~~~~~~~~
dreaming.cpp:53:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[index].size(); ++i)
                     ~~^~~~~~~~~~~~~~~~~
dreaming.cpp: In function 'void dfs2(int, int, int, int)':
dreaming.cpp:79:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[index].size(); ++i)
                     ~~^~~~~~~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:171:32: warning: 'max1index' may be used uninitialized in this function [-Wmaybe-uninitialized]
         if (i != maxindex && i != max1index)
                              ~~^~~~~~~~~~~~
dreaming.cpp:150:9: warning: 'maxindex' may be used uninitialized in this function [-Wmaybe-uninitialized]
         if (i != maxindex)
         ^~
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1085 ms 13560 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1085 ms 13560 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1085 ms 13560 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 6648 KB Output is correct
2 Correct 28 ms 6756 KB Output is correct
3 Correct 35 ms 6708 KB Output is correct
4 Correct 32 ms 6720 KB Output is correct
5 Correct 26 ms 6784 KB Output is correct
6 Correct 31 ms 7032 KB Output is correct
7 Correct 34 ms 6904 KB Output is correct
8 Correct 30 ms 6648 KB Output is correct
9 Correct 31 ms 6648 KB Output is correct
10 Correct 34 ms 6904 KB Output is correct
11 Correct 4 ms 2688 KB Output is correct
12 Correct 7 ms 4608 KB Output is correct
13 Correct 8 ms 4608 KB Output is correct
14 Correct 7 ms 4608 KB Output is correct
15 Correct 8 ms 4608 KB Output is correct
16 Correct 7 ms 4480 KB Output is correct
17 Correct 7 ms 4224 KB Output is correct
18 Correct 8 ms 4608 KB Output is correct
19 Correct 7 ms 4608 KB Output is correct
20 Correct 3 ms 2688 KB Output is correct
21 Correct 5 ms 2688 KB Output is correct
22 Correct 4 ms 2816 KB Output is correct
23 Correct 7 ms 4608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1085 ms 13560 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1085 ms 13560 KB Time limit exceeded
2 Halted 0 ms 0 KB -