# include "dreaming.h"
# include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int fix[N];
vector < int > pts;
vector < pair < int , int > > edges[N];
void dfs(int x, int p, vector < int > &dist){
fix[x] = 1;
pts.push_back(x);
for(int i = 0; i < edges[x].size(); i ++){
if(edges[x][i].first != p){
dist[edges[x][i].first] = dist[x] + edges[x][i].second;
dfs(edges[x][i].first, x, dist);
}
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]){
vector < int > dist_a(N, 0), dist_b(N, 0), cen;
for(int i = 0; i < M; i ++){
edges[A[i]].push_back(make_pair(B[i], T[i]));
edges[B[i]].push_back(make_pair(A[i], T[i]));
}
for(int i = 0; i < N; i ++){
if(fix[i] != 1){
pts.clear();
dist_a[i] = 0;
dfs(i, 0, dist_a);
int a = pts[0];
for(int i = 0; i < pts.size(); i ++){
if(dist_a[pts[i]] > dist_a[a]) a = pts[i];
}
pts.clear();
dist_a[a] = 0;
dfs(a, 0, dist_a);
int b = pts[0];
for(int i = 0; i < pts.size(); i ++){
if(dist_a[pts[i]] > dist_a[b]) b = pts[i];
}
if(M == N - 1) return dist_a[b];
pts.clear();
dfs(b, 0, dist_b);
int res = 1e9;
for(int i = 0; i < pts.size(); i ++){
res = min(res, max(dist_a[pts[i]], dist_b[pts[i]]));
}
cen.push_back(res);
pts.clear();
}
}
if(cen.size() == 1) return cen.back();
else{
sort(cen.rbegin(), cen.rend());
int res = 0;
if(cen.size() >= 2) res = cen[0] + cen[1] + L;
if(cen.size() >= 3) res = max(res, cen[1] + cen[2] + 2 * L);
return res;
}
}
Compilation message
dreaming.cpp: In function 'void dfs(int, int, std::vector<int>&)':
dreaming.cpp:11:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < edges[x].size(); i ++){
~~^~~~~~~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:30:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < pts.size(); i ++){
~~^~~~~~~~~~~~
dreaming.cpp:37:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < pts.size(); i ++){
~~^~~~~~~~~~~~
dreaming.cpp:44:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < pts.size(); i ++){
~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
84 ms |
14836 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
84 ms |
14836 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
84 ms |
14836 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
8800 KB |
Output is correct |
2 |
Correct |
42 ms |
9320 KB |
Output is correct |
3 |
Correct |
42 ms |
9256 KB |
Output is correct |
4 |
Correct |
42 ms |
9336 KB |
Output is correct |
5 |
Correct |
43 ms |
9208 KB |
Output is correct |
6 |
Correct |
48 ms |
9720 KB |
Output is correct |
7 |
Correct |
45 ms |
9464 KB |
Output is correct |
8 |
Correct |
43 ms |
9224 KB |
Output is correct |
9 |
Correct |
41 ms |
9208 KB |
Output is correct |
10 |
Correct |
50 ms |
9352 KB |
Output is correct |
11 |
Correct |
6 ms |
4984 KB |
Output is correct |
12 |
Correct |
14 ms |
6900 KB |
Output is correct |
13 |
Correct |
14 ms |
6900 KB |
Output is correct |
14 |
Correct |
15 ms |
6900 KB |
Output is correct |
15 |
Correct |
15 ms |
6900 KB |
Output is correct |
16 |
Correct |
14 ms |
6944 KB |
Output is correct |
17 |
Correct |
14 ms |
6772 KB |
Output is correct |
18 |
Correct |
14 ms |
6900 KB |
Output is correct |
19 |
Correct |
14 ms |
6900 KB |
Output is correct |
20 |
Correct |
6 ms |
4984 KB |
Output is correct |
21 |
Correct |
6 ms |
4984 KB |
Output is correct |
22 |
Correct |
7 ms |
5112 KB |
Output is correct |
23 |
Correct |
13 ms |
6900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
84 ms |
14836 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
84 ms |
14836 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |