#include <bits/stdc++.h>
#include "dreaming.h"
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 998244353;
const ll LOGN = 21;
const ll MAXN = 1e5 + 10000;
vector<vector<pair<int,int>>> graph;
int vis[MAXN], up[MAXN], mx[MAXN];
pair<int,int> st[MAXN];
int dfs(int node, int parent) {
for (auto u : graph[node]) {
if (u.f == parent)
continue;
int Q = u.s + dfs(u.f, node);
if (Q > st[node].f)
st[node] = {Q, st[node].f};
else if (Q > st[node].s)
st[node] = {st[node].f, Q};
}
return st[node].f;
}
void dfs2(int node, int parent, int par_len) {
vis[node] = true;
if (node != parent) {
int th = st[node].f + par_len;
up[node] = up[parent] + par_len;
if (th == st[parent].f)
up[node] = max(up[node], st[parent].s + par_len);
else
up[node] = max(up[node], st[parent].f + par_len);
}
mx[node] = max(up[node], st[node].f);
for (auto u : graph[node]) {
if (u.f == parent)
continue;
dfs2(u.f, node, u.s);
}
}
int mx_node = 0, mx_dist = -1;
void dfs3(int node, int parent, int dist) {
if (dist > mx_dist) {
mx_dist = dist;
mx_node = node;
}
for (auto u : graph[node]) {
if (u.f == parent)
continue;
dfs3(u.f, node, dist + u.s);
}
}
int diameter(int node) {
mx_dist = -1, mx_node = 0;
dfs3(node, node, 0);
int q = mx_node;
mx_dist = -1, mx_node = 0;
dfs3(q, q, 0);
return mx_dist;
}
int nd = 2e9;
void dfs4(int node, int parent) {
vis[node] = true;
if (nd > mx[node])
nd = mx[node];
for (auto u : graph[node]) {
if (u.f == parent)
continue;
dfs4(u.f, node);
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
graph = vector<vector<pair<int,int>>>(N+1, vector<pair<int,int>>());
for (int i = 0; i < M; i++) {
graph[A[i]].push_back({B[i], T[i]});
graph[B[i]].push_back({A[i], T[i]});
}
for (int i = 1; i <= N; i++) {
if (!vis[i]) {
dfs(i, i);
dfs2(i, i, 0);
}
}
for (int i = 1; i <= N; i++)
vis[i] = false;
priority_queue<pair<int,int>> pq;
for (int i = 1; i <= N; i++) {
if (!vis[i]) {
int dia = diameter(i);
nd = 2e9;
dfs4(i, i);
pq.push({nd, dia});
}
}
while (pq.size() >= 2) {
pair<int,int> A = pq.top();
pq.pop();
pair<int,int> B = pq.top();
pq.pop();
int new_dia = max(A.s, max(B.s, A.f + B.f + L));
int new_mx = min(max(A.f, B.f + L), max(A.f + L, B.f));
pq.push({new_mx, new_dia});
}
return pq.top().s;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
13916 KB |
Output is correct |
2 |
Correct |
35 ms |
16220 KB |
Output is correct |
3 |
Correct |
23 ms |
10272 KB |
Output is correct |
4 |
Correct |
5 ms |
4140 KB |
Output is correct |
5 |
Correct |
5 ms |
3420 KB |
Output is correct |
6 |
Correct |
10 ms |
5560 KB |
Output is correct |
7 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
13916 KB |
Output is correct |
2 |
Correct |
35 ms |
16220 KB |
Output is correct |
3 |
Correct |
23 ms |
10272 KB |
Output is correct |
4 |
Correct |
5 ms |
4140 KB |
Output is correct |
5 |
Correct |
5 ms |
3420 KB |
Output is correct |
6 |
Correct |
10 ms |
5560 KB |
Output is correct |
7 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
8160 KB |
Output is correct |
2 |
Correct |
22 ms |
8152 KB |
Output is correct |
3 |
Correct |
22 ms |
8160 KB |
Output is correct |
4 |
Correct |
22 ms |
8320 KB |
Output is correct |
5 |
Correct |
22 ms |
8152 KB |
Output is correct |
6 |
Correct |
26 ms |
9000 KB |
Output is correct |
7 |
Correct |
26 ms |
8584 KB |
Output is correct |
8 |
Correct |
22 ms |
8160 KB |
Output is correct |
9 |
Correct |
21 ms |
8152 KB |
Output is correct |
10 |
Correct |
23 ms |
8404 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
13 ms |
6868 KB |
Output is correct |
13 |
Correct |
13 ms |
6868 KB |
Output is correct |
14 |
Correct |
12 ms |
6868 KB |
Output is correct |
15 |
Correct |
12 ms |
6868 KB |
Output is correct |
16 |
Correct |
12 ms |
6868 KB |
Output is correct |
17 |
Correct |
12 ms |
6608 KB |
Output is correct |
18 |
Correct |
12 ms |
6868 KB |
Output is correct |
19 |
Correct |
13 ms |
6868 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
18 ms |
6868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
13916 KB |
Output is correct |
2 |
Correct |
35 ms |
16220 KB |
Output is correct |
3 |
Correct |
23 ms |
10272 KB |
Output is correct |
4 |
Correct |
5 ms |
4140 KB |
Output is correct |
5 |
Correct |
5 ms |
3420 KB |
Output is correct |
6 |
Correct |
10 ms |
5560 KB |
Output is correct |
7 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |