#include <bits/stdc++.h>
#include "dreaming.h"
using namespace std;
struct edge {
int to, cost;
/*edge(int _to, int _cost) {
to = _to;
cost = _cost;
}*/
};
const int MAX_N = 1e5+7;
vector<edge> adj[MAX_N]; vector<int> R;
bool visited[MAX_N];
int maxdist, one, two, point;
edge parent[1005];
void dfs1(int u, int par, int val) {
visited[u] = 1;
int i, mx = 0;
if(u == par && !adj[u].size()) {point = u; return;}
for(i = 0; i < (int) adj[u].size(); i++) {
edge v = adj[u][i];
if(visited[v.to] || v.to == par) continue;
if(v.cost + val > mx) point = v.to, mx = v.cost + val;
dfs1(v.to, u, v.cost + val);
}
}
void dfs2(int u, int par, int val) {
visited[u] = 0;
int i, mx = 0;
if(u == par && !adj[u].size()) {
point = u; maxdist = 0; edge v;
v.cost = 0; v.to = u; parent[u] = v;
return;}
for(i = 0; i < (int) adj[u].size(); i++) {
edge v = adj[u][i];
if(!visited[v.to] || v.to == par) continue;
if(v.cost + val > mx) point = v.to, mx = v.cost + val, maxdist = mx;
dfs2(v.to, u, v.cost + val);
parent[v.to] = v; parent[v.to].to = u;
} visited[u] = 1;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
int i, ans = 0;
for(i = 0; i < M; i++) {
edge v; v.to = B[i]; v.cost = T[i];
adj[A[i]].push_back(v); v.to = A[i];
adj[B[i]].push_back(v);
}
for(i = 0; i < N; i++) {
if(visited[i]) continue;
dfs1(i, i, 0); one = point;
dfs2(one, one, 0); two = point;
ans = max(ans, maxdist);
int able = maxdist/2, now = 0, d = 0;
if(one == two) {R.push_back(0); continue;}
if(parent[two].to == one) {R.push_back(parent[two].cost); continue;}
for(i = two; i != one; i = parent[i].to) {
if(now + parent[i].cost >= able) {
d = min(now, maxdist-parent[i].cost-now);
d += parent[i].cost; R.push_back(d);
break;
} now += parent[i].cost;
}
} sort(R.begin(), R.end(), greater<int>());
if(!M) return 2*L;
if(R.size() > 1) ans = max(ans, R[0]+L+R[1]);
if(R.size() > 2) ans = max(ans, R[1]+L+L+R[2]);
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
72 ms |
27512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
72 ms |
27512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
72 ms |
27512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
24 ms |
9984 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
72 ms |
27512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
72 ms |
27512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |