#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
struct Edge {
int to, w;
bool operator<(const Edge& other) const {
return w < other.w;
}
};
const int maxN = 1e5 + 5;
const int INF = 1e9 + 5;
vector<Edge> adj[maxN];
vector<int> comp[maxN];
vector<int> curr;
int ccs = 0;
bool vis[maxN];
int dist[maxN];
void dfs(int v) {
vis[v] = true;
comp[ccs].push_back(v);
for(Edge u : adj[v]) {
if (!vis[u.to]) {
dfs(u.to);
}
}
}
pair<int, int> mx(int from, int idx) {
priority_queue<Edge> q;
q.push({from, 0});
for(int i : comp[idx]) {
dist[i] = INF;
}
dist[from] = 0;
while(!q.empty()) {
Edge v = q.top();
q.pop();
if (v.w > dist[v.to]) continue;
for(Edge u : adj[v.to]) {
int d = dist[v.to] + u.w;
if (d < dist[u.to]) {
dist[u.to] = d;
q.push({u.to, dist[u.to]});
}
}
}
pair<int, int> ret = {-1, -1};
for(int i : comp[idx]) {
// cout << i << " " << dist[i] << "\n";
if (dist[i] > ret.second) {
ret = {i, dist[i]};
}
}
assert(ret.second != -1);
assert(ret.second != INF);
return ret;
}
int diameter(int idx) {
int y = mx(comp[idx][0], idx).first;
return mx(y, idx).second;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for(int i = 0; i < M; i++) {
adj[A[i]].push_back({B[i], T[i]});
adj[B[i]].push_back({A[i], T[i]});
}
int sum = 0;
for(int i = 0; i < N; i++) {
if (!vis[i]) {
dfs(i);
sum += diameter(ccs);
ccs++;
}
}
sum += L;
return sum;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
57 ms |
12740 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
5000 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
57 ms |
12740 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
10132 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
5000 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
57 ms |
12740 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |