#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define ins insert
#define pii pair<int, int>
#define fi first
#define se second
#define mpr make_pair
#define inf 0x3F3F3F3F
const int MXN = 1e5 + 5;
int n, m, l;
vector<pii> adj[MXN];
vector<vector<int>> comp;
int used[MXN];
int dist[2][MXN];
int c = -1;
int mx, node = -1;
int dia1, dia2;
void dfs(int a)
{
comp[c].pb(a);
used[a] = 1;
for (pii v : adj[a])
{
if (used[v.fi]) continue;
dfs(v.fi);
}
}
void dfs1(int a, int w = 0, int p = -1)
{
if (w > mx)
{
mx = w;
dia2 = a;
}
for (pii v : adj[a])
{
if (v.fi == p) continue;
dfs1(v.fi, w + v.se, a);
}
}
void dfs2(int a, int id, int w = 0, int p = -1)
{
dist[id][a] = w;
for (pii v : adj[a])
{
if (v.fi == p) continue;
dfs2(v.fi, id, w + v.se, a);
}
}
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++)
{
adj[A[i]].pb(mpr(B[i], T[i]));
adj[B[i]].pb(mpr(A[i], T[i]));
}
for (int i = 0; i < n; i++)
{
if (!used[i])
{
c++;
comp.pb(vector<int> (0));
dfs(i);
}
}
multiset<int> ms;
for (int i = 0; i <= c; i++)
{
mx = -inf;
dia1 = dia2 = -1;
dfs1(comp[i][0]);
mx = -inf;
dia1 = dia2;
dfs1(dia1);
dfs2(dia1, 0);
dfs2(dia2, 1);
int center = comp[i][0];
for (int u : comp[i])
{
if (max(dist[0][u], dist[1][u]) < max(dist[0][center], dist[1][center])) center = u;
}
ms.ins(max(dist[0][center], dist[1][center]));
}
if (!c) return *ms.begin();
auto it = ms.end();
it--;
it--;
auto it1 = it;
it1--;
if (c == 1) return *ms.rbegin() + *it + l;
return max(*ms.rbegin() + *it + l, *it + *it1 + 2*l);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
31 ms |
12496 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
5156 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
31 ms |
12496 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
13316 KB |
Output is correct |
2 |
Correct |
27 ms |
13324 KB |
Output is correct |
3 |
Correct |
31 ms |
13572 KB |
Output is correct |
4 |
Correct |
26 ms |
13828 KB |
Output is correct |
5 |
Correct |
34 ms |
13568 KB |
Output is correct |
6 |
Correct |
38 ms |
14156 KB |
Output is correct |
7 |
Correct |
28 ms |
14084 KB |
Output is correct |
8 |
Correct |
26 ms |
13572 KB |
Output is correct |
9 |
Correct |
26 ms |
13576 KB |
Output is correct |
10 |
Correct |
29 ms |
13948 KB |
Output is correct |
11 |
Correct |
1 ms |
4952 KB |
Output is correct |
12 |
Correct |
21 ms |
15116 KB |
Output is correct |
13 |
Correct |
20 ms |
15112 KB |
Output is correct |
14 |
Correct |
21 ms |
15112 KB |
Output is correct |
15 |
Correct |
21 ms |
15116 KB |
Output is correct |
16 |
Correct |
22 ms |
15104 KB |
Output is correct |
17 |
Correct |
21 ms |
15112 KB |
Output is correct |
18 |
Correct |
22 ms |
15112 KB |
Output is correct |
19 |
Correct |
23 ms |
15104 KB |
Output is correct |
20 |
Correct |
1 ms |
2908 KB |
Output is correct |
21 |
Correct |
1 ms |
2920 KB |
Output is correct |
22 |
Correct |
2 ms |
5208 KB |
Output is correct |
23 |
Correct |
21 ms |
15104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
5156 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
31 ms |
12496 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |