#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
int dist[100005], sz[100005], n, m, l;
vector<pair<int, int> > adj[100005];
vector<int> vec, centers;
bool visited[100005];
void dfs(int u, int prev)
{
visited[u]=1;
vec.push_back(u);
for (int i=0; i<adj[u].size(); i++)
{
int v=adj[u][i].first, w=adj[u][i].second;
if (v==prev)
continue;
dist[v]=dist[u]+w;
dfs(v, u);
sz[u]+=sz[v]+w;
}
}
void dfs2(int u, int prev)
{
for (int i=0; i<adj[u].size(); i++)
{
int v=adj[u][i].first, w=adj[u][i].second;
if (v==prev)
continue;
dist[v]=dist[u]+w;
dfs2(v, u);
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[])
{
n=N;
m=M;
l=L;
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 ans=0;
for (int i=0; i<n; i++)
{
if (visited[i])
continue;
dist[i]=0;
dfs(i, -1);
int corner, center=1e9, len=-1;
for (int j=0; j<vec.size(); j++)
{
int u=vec[j];
if (len<dist[u])
{
len=dist[u];
corner=u;
}
dist[u]=1e9;
}
dist[corner]=0;
dfs2(corner, -1);
for (int j=0; j<vec.size(); j++)
{
int u=vec[j];
ans=max(ans, dist[u]);
dist[u]=1e9;
}
for (int j=0; j<vec.size(); j++)
{
int u=vec[j];
int maxi=sz[i]-sz[u];
// cout << "j " << j << '\n';
// cout << "maxi " << maxi << '\n';
for (int k=0; k<adj[u].size(); k++)
{
int v=adj[u][k].first, w=adj[u][k].second;
if (sz[u]>sz[v])
maxi=max(maxi, sz[v]+w);
// cout << "maxi " << maxi << '\n';
}
center=min(center, maxi);
}
centers.push_back(center);
vec.clear();
// cout << "i center " << i << ' ' << center << '\n';
}
sort(centers.begin(), centers.end(), greater<int>());
if (centers.size()==1)
return ans;
else if (centers.size()==2)
return max(ans, centers[0]+centers[1]+l);
else
return max(ans, max(centers[0]+centers[1]+l, centers[1]+centers[2]+l*2));
}
Compilation message
dreaming.cpp: In function 'void dfs(int, int)':
dreaming.cpp:12:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
12 | for (int i=0; i<adj[u].size(); i++)
| ~^~~~~~~~~~~~~~
dreaming.cpp: In function 'void dfs2(int, int)':
dreaming.cpp:24:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for (int i=0; i<adj[u].size(); i++)
| ~^~~~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:51:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for (int j=0; j<vec.size(); j++)
| ~^~~~~~~~~~~
dreaming.cpp:63:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for (int j=0; j<vec.size(); j++)
| ~^~~~~~~~~~~
dreaming.cpp:69:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for (int j=0; j<vec.size(); j++)
| ~^~~~~~~~~~~
dreaming.cpp:75:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for (int k=0; k<adj[u].size(); k++)
| ~^~~~~~~~~~~~~~
dreaming.cpp:62:7: warning: 'corner' may be used uninitialized in this function [-Wmaybe-uninitialized]
62 | dfs2(corner, -1);
| ~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
14768 KB |
Output is correct |
2 |
Correct |
45 ms |
14688 KB |
Output is correct |
3 |
Correct |
29 ms |
11056 KB |
Output is correct |
4 |
Correct |
7 ms |
4420 KB |
Output is correct |
5 |
Correct |
6 ms |
3660 KB |
Output is correct |
6 |
Correct |
12 ms |
5356 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
29 ms |
6988 KB |
Output is correct |
9 |
Correct |
32 ms |
9236 KB |
Output is correct |
10 |
Correct |
2 ms |
2660 KB |
Output is correct |
11 |
Correct |
41 ms |
10680 KB |
Output is correct |
12 |
Correct |
56 ms |
12612 KB |
Output is correct |
13 |
Correct |
2 ms |
2764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2648 KB |
Output is correct |
2 |
Correct |
2 ms |
2656 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
14768 KB |
Output is correct |
2 |
Correct |
45 ms |
14688 KB |
Output is correct |
3 |
Correct |
29 ms |
11056 KB |
Output is correct |
4 |
Correct |
7 ms |
4420 KB |
Output is correct |
5 |
Correct |
6 ms |
3660 KB |
Output is correct |
6 |
Correct |
12 ms |
5356 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
29 ms |
6988 KB |
Output is correct |
9 |
Correct |
32 ms |
9236 KB |
Output is correct |
10 |
Correct |
2 ms |
2660 KB |
Output is correct |
11 |
Correct |
41 ms |
10680 KB |
Output is correct |
12 |
Correct |
56 ms |
12612 KB |
Output is correct |
13 |
Correct |
2 ms |
2764 KB |
Output is correct |
14 |
Correct |
2 ms |
2648 KB |
Output is correct |
15 |
Correct |
2 ms |
2656 KB |
Output is correct |
16 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
6504 KB |
Output is correct |
2 |
Correct |
21 ms |
6556 KB |
Output is correct |
3 |
Correct |
32 ms |
6512 KB |
Output is correct |
4 |
Correct |
19 ms |
6604 KB |
Output is correct |
5 |
Correct |
20 ms |
6572 KB |
Output is correct |
6 |
Correct |
29 ms |
7092 KB |
Output is correct |
7 |
Correct |
24 ms |
6684 KB |
Output is correct |
8 |
Correct |
20 ms |
6460 KB |
Output is correct |
9 |
Correct |
20 ms |
6452 KB |
Output is correct |
10 |
Correct |
20 ms |
6720 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
6 ms |
4056 KB |
Output is correct |
13 |
Correct |
6 ms |
4060 KB |
Output is correct |
14 |
Correct |
8 ms |
4040 KB |
Output is correct |
15 |
Correct |
6 ms |
4040 KB |
Output is correct |
16 |
Correct |
6 ms |
4000 KB |
Output is correct |
17 |
Correct |
9 ms |
3784 KB |
Output is correct |
18 |
Correct |
6 ms |
4040 KB |
Output is correct |
19 |
Correct |
6 ms |
4040 KB |
Output is correct |
20 |
Correct |
2 ms |
2636 KB |
Output is correct |
21 |
Correct |
2 ms |
2636 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
6 ms |
4040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2648 KB |
Output is correct |
2 |
Correct |
2 ms |
2656 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
14768 KB |
Output is correct |
2 |
Correct |
45 ms |
14688 KB |
Output is correct |
3 |
Correct |
29 ms |
11056 KB |
Output is correct |
4 |
Correct |
7 ms |
4420 KB |
Output is correct |
5 |
Correct |
6 ms |
3660 KB |
Output is correct |
6 |
Correct |
12 ms |
5356 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
29 ms |
6988 KB |
Output is correct |
9 |
Correct |
32 ms |
9236 KB |
Output is correct |
10 |
Correct |
2 ms |
2660 KB |
Output is correct |
11 |
Correct |
41 ms |
10680 KB |
Output is correct |
12 |
Correct |
56 ms |
12612 KB |
Output is correct |
13 |
Correct |
2 ms |
2764 KB |
Output is correct |
14 |
Correct |
2 ms |
2648 KB |
Output is correct |
15 |
Correct |
2 ms |
2656 KB |
Output is correct |
16 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |