#include "dreaming.h"
#include <iostream>
#include <vector>
#include <assert.h>
int component[100010] = { 0 };
int componentmin[100010] = { 0 };
int curmax[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 = pathSum, 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)
{
int val;
if (nextVal + curmax[next] == max1)
{
curmax[next] = std::max(curmax[next], nextVal + max2);
val = nextVal + max2;
}
else
{
curmax[next] = std::max(curmax[next], nextVal + max1);
val = nextVal + max1;
}
dfsout(next, index, val);
}
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
n = N;
m = M;
l = L;
assert(m == n - 1);
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 = 1; i < cnt; ++i)
{
componentmin[i] = 1e9 + 5;
}
for (int i = 0; i < n; ++i)
{
componentmin[component[i]] = std::min(componentmin[component[i]], curmax[i]);
std::cout << i << " " << curmax[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;
}
}
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, max2 = -1;
for (int i = 1; i < cnt; ++i)
{
if (i != maxindex)
{
if (componentmin[i] > max1)
{
max2 = max1;
max1 = componentmin[i];
}
else if (componentmin[i] > max2)
{
max2 = componentmin[i];
}
}
}
for (int i = 1; i < cnt; ++i)
{
if (i != maxindex)
{
if (componentmin[i] == max1)
{
if (max2 != -1)
{
ans = std::max(ans, max2 + 2 * l + componentmin[i]);
}
}
else
{
ans = std::max(ans, max1 + 2 * l + componentmin[i]);
}
}
}
return ans;
}
Compilation message
dreaming.cpp: In function 'void dfs(int, int, int)':
dreaming.cpp:15: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:33:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < g[index].size(); ++i)
~~^~~~~~~~~~~~~~~~~
dreaming.cpp:52: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:129:9: warning: 'maxindex' may be used uninitialized in this function [-Wmaybe-uninitialized]
if (i != maxindex)
^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
33 ms |
7416 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
33 ms |
7416 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
33 ms |
7416 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
16 ms |
5888 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
33 ms |
7416 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
33 ms |
7416 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |