#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 = 0;
void dfs4(int node, int parent) {
vis[node] = true;
if (mx[node] > nd)
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 = 0;
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
35 ms |
13916 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
35 ms |
13916 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
8272 KB |
Output is correct |
2 |
Correct |
29 ms |
8412 KB |
Output is correct |
3 |
Correct |
22 ms |
8160 KB |
Output is correct |
4 |
Correct |
22 ms |
8356 KB |
Output is correct |
5 |
Correct |
22 ms |
8156 KB |
Output is correct |
6 |
Correct |
24 ms |
9180 KB |
Output is correct |
7 |
Correct |
23 ms |
8412 KB |
Output is correct |
8 |
Correct |
21 ms |
8160 KB |
Output is correct |
9 |
Correct |
21 ms |
8152 KB |
Output is correct |
10 |
Correct |
26 ms |
8404 KB |
Output is correct |
11 |
Correct |
0 ms |
2392 KB |
Output is correct |
12 |
Correct |
12 ms |
6868 KB |
Output is correct |
13 |
Correct |
13 ms |
6864 KB |
Output is correct |
14 |
Correct |
13 ms |
6864 KB |
Output is correct |
15 |
Correct |
13 ms |
6868 KB |
Output is correct |
16 |
Correct |
13 ms |
6868 KB |
Output is correct |
17 |
Correct |
12 ms |
6612 KB |
Output is correct |
18 |
Correct |
13 ms |
6840 KB |
Output is correct |
19 |
Correct |
16 ms |
6868 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
13 ms |
6868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
35 ms |
13916 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |