#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>
#include <set>
#include <cmath>
#include <climits>
#include <cstring>
#include <stdio.h>
#include <assert.h>
#include "dreaming.h"
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> ii;
ll MOD = 1e9 + 9;
int N = 1e5 + 1;
vector<vector<ii>> adjList(N, vector<ii> ());
vi visited(N, false), dist(N, -1);
ll maxNode, d;
void dfs(int curr, int prev, int time){
visited[curr] = true;
dist[curr] = time;
if (time > d){
maxNode = curr;
d = time;
}
for (int i = 0; i < adjList[curr].size(); i++){
int idx = adjList[curr][i].first, t = adjList[curr][i].second;
if (idx != prev)
dfs(idx, curr, time + t);
}
}
int travelTime(int n, int m, int l, int A[], int B[], int T[]){
for (int i = 0; i < m; i++){
adjList[A[i]].push_back(make_pair(B[i], T[i]));
adjList[B[i]].push_back(make_pair(A[i], T[i]));
}
vector<ll> r;
ll res = 0;
for (int i = 0; i < n; i++){
if (!visited[i]){
maxNode = -1; d = -1;
dfs(i, -1, 0);
int A = maxNode;
maxNode = -1; d = -1;
dfs(A, -1, 0);
int B = maxNode; res = max(res, d);
ll currD = d, prevD = -1, curr = B;
vi r;
while(true){
if (2*currD <= d)
break;
for (int i = 0; i < adjList[curr].size(); i++){
int k = adjList[curr][i].first;
if (dist[k] < dist[curr]){
curr = k;
prevD = currD;
currD = dist[curr];
break;
}
}
}
r.push_back(min(max(currD, d - currD), max(prevD, d - prevD)));
}
}
sort(r.begin(), r.end());
int k = r.size();
if (k == 1)
return res;
res = max(res, r[k - 1] + r[k - 2] + l);
if (k > 2)
res = max(res, r[k - 2] + r[k - 3] + 2*l);
return res;
}
Compilation message
dreaming.cpp: In function 'void dfs(int, int, int)':
dreaming.cpp:33:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < adjList[curr].size(); i++){
~~^~~~~~~~~~~~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:62:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < adjList[curr].size(); i++){
~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
71 ms |
29180 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
71 ms |
29180 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
71 ms |
29180 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
33 ms |
13560 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
71 ms |
29180 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
71 ms |
29180 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |