#include "dreaming.h"
#include <vector>
#include <iostream>
#include <algorithm>
#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;
pair<int,int> compdfs(int curr, int par, int c)
{
comp[curr] = c;
int mmax = 0;
int mver = curr;
for (auto p : adjlist[curr])
{
int next = p.first;
int w = p.second;
if (next == par) continue;
auto pn = compdfs(next, curr, c);
pn.second += w;
if (pn.second > mmax)
{
mmax = pn.second;
mver = pn.first;
}
}
return {mver, mmax};
}
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;
distdfs(next, curr, d + w);
}
}
int calc(int l)
{
dist.resize(n, -1);
comp.resize(n, -1);
for (int i = 0; i < n; i++)
{
if (comp[i] == -1)
{
comp[i] = nrcomp;
auto rp = compdfs(i, -1, nrcomp);
distdfs(rp.first, -1, 0);
nrcomp++;
}
}
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 j : mv)
{
distdfs(j, -1, 0);
}
md.assign(nrcomp, (int)1e12);
for (int i = 0; i < n; i++)
{
if (dist[i] < md[comp[i]])
{
md[comp[i]] = dist[i];
}
}
sort(md.begin(), md.end());
if (nrcomp <= 2)
{
int ssum = 0;
for (auto i : md) ssum += i;
if (nrcomp == 2) ssum += l;
return ssum;
}
return max(md[nrcomp - 1] + md[nrcomp - 2] + l, md[nrcomp - 2] + md[nrcomp - 3] + 2*l);
}
void print()
{
for (int i = 0; i < n; i++)
{
if (i < 10) cout << " ";
cout << i << " ";
}
cout << "\n";
for (auto i : comp)
{
if (i < 10) cout << " ";
cout << i << " ";
}
cout << "\n";
for (auto i : dist)
{
if (i < 10) cout << " ";
cout << i << " ";
}
cout << "\n";
}
};
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);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
90 ms |
14584 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
90 ms |
14584 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
90 ms |
14584 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
7928 KB |
Output is correct |
2 |
Correct |
44 ms |
7844 KB |
Output is correct |
3 |
Correct |
43 ms |
7672 KB |
Output is correct |
4 |
Correct |
43 ms |
7800 KB |
Output is correct |
5 |
Correct |
41 ms |
7672 KB |
Output is correct |
6 |
Correct |
43 ms |
8184 KB |
Output is correct |
7 |
Correct |
44 ms |
8052 KB |
Output is correct |
8 |
Correct |
41 ms |
7676 KB |
Output is correct |
9 |
Correct |
41 ms |
7672 KB |
Output is correct |
10 |
Correct |
46 ms |
8052 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
12 ms |
5880 KB |
Output is correct |
13 |
Correct |
12 ms |
5880 KB |
Output is correct |
14 |
Correct |
12 ms |
5880 KB |
Output is correct |
15 |
Correct |
11 ms |
5880 KB |
Output is correct |
16 |
Correct |
11 ms |
5908 KB |
Output is correct |
17 |
Correct |
11 ms |
5752 KB |
Output is correct |
18 |
Correct |
12 ms |
5880 KB |
Output is correct |
19 |
Correct |
12 ms |
5980 KB |
Output is correct |
20 |
Correct |
2 ms |
256 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
532 KB |
Output is correct |
23 |
Correct |
11 ms |
5880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
90 ms |
14584 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
90 ms |
14584 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |